1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import unittest
16-
1715import frontend
16+ import pytest
1817
1918
2019class FakeTime (object ):
@@ -40,30 +39,33 @@ def busy_wait(self):
4039 self .cpu_time += self .cpu_time_step
4140
4241
43- class TestHandlers (unittest .TestCase ):
44- def setUp (self ):
45- self .fake_time = FakeTime ()
46- self .cpu_burner = frontend .CpuBurner ()
47- self .cpu_burner .get_user_cputime = self .fake_time .get_user_cputime
48- self .cpu_burner .get_walltime = self .fake_time .get_walltime
49- self .cpu_burner .busy_wait = self .fake_time .busy_wait
42+ @pytest .fixture
43+ def faketime ():
44+ return FakeTime ()
45+
46+
47+ @pytest .fixture
48+ def cpuburner (faketime ):
49+ cpuburner = frontend .CpuBurner ()
50+ cpuburner .get_user_cputime = faketime .get_user_cputime
51+ cpuburner .get_walltime = faketime .get_walltime
52+ cpuburner .busy_wait = faketime .busy_wait
53+ return cpuburner
5054
51- # In this test scenario CPU time advances at 25% of the wall time speed.
52- # Given the request requires 1 CPU core second, we expect it to finish
53- # within the timeout (5 seconds) and return success.
54- def test_ok_response (self ):
55- self .fake_time .cpu_time_step = 0.25
56- (code , _ ) = self .cpu_burner .handle_http_request ()
57- self .assertEqual (200 , code )
5855
59- # In this test scenario CPU time advances at 15 % of the wall time speed.
60- # Given the request requires 1 CPU core second, we expect it to timeout
61- # after 5 simulated wall time seconds and return error 500 .
62- def test_timeout ( self ):
63- self . fake_time . cpu_time_step = 0.15
64- (code , _ ) = self . cpu_burner .handle_http_request ()
65- self . assertEqual ( 500 , code )
56+ # In this test scenario CPU time advances at 25 % of the wall time speed.
57+ # Given the request requires 1 CPU core second, we expect it to finish
58+ # within the timeout (5 seconds) and return success .
59+ def test_ok_response ( faketime , cpuburner ):
60+ faketime . cpu_time_step = 0.25
61+ (code , _ ) = cpuburner .handle_http_request ()
62+ assert code == 200
6663
6764
68- if __name__ == '__main__' :
69- unittest .main ()
65+ # In this test scenario CPU time advances at 15% of the wall time speed.
66+ # Given the request requires 1 CPU core second, we expect it to timeout
67+ # after 5 simulated wall time seconds and return error 500.
68+ def test_timeout (faketime , cpuburner ):
69+ faketime .cpu_time_step = 0.15
70+ (code , _ ) = cpuburner .handle_http_request ()
71+ assert code == 500
0 commit comments