44import resource
55from collections .abc import Iterator
66from contextlib import contextmanager
7- from time import process_time , sleep
7+ from time import process_time , sleep , time
88from unittest .mock import patch
99
1010import psutil
@@ -53,10 +53,13 @@ def busy_wait(self: WorkerManager) -> None:
5353
5454async def test_should_raise_cpu_timout_error (pool : WorkerPool ) -> None :
5555 with patch_worker_pool (pool , _make_get_manifest_busy_wait ):
56+ start_time = time ()
57+ # Change the timeout for faster testing.
5658 with pytest .raises (WorkerStartError ) as exc_info , patch .object (BaseWorker , "_init_worker_timeout" , 0.05 ):
5759 async with pool .get_worker (PACKAGE , 1 , 1 ):
5860 pass
5961 assert isinstance (exc_info .value .__cause__ , WorkerCPUTimeLimitExceededError )
62+ assert 0.05 < (time () - start_time ) < 0.5
6063
6164
6265@contextmanager
@@ -72,11 +75,14 @@ async def test_should_raise_real_timout_error(pool: WorkerPool) -> None:
7275 with patch_worker_pool (pool , _make_get_manifest_sleep ):
7376 # The timeout should not be too short, because the Python interpreter also needs some time to start up, which
7477 # is accounted for the init worker step. Otherwise, a WorkerCPUTimeLimitExceededError is raised.
78+ start_time = time ()
7579 with (
7680 pytest .raises (WorkerStartError ) as exc_info ,
81+ # Change the timeout and factor for faster testing.
7782 patch .object (BaseWorker , "_init_worker_timeout" , 0.6 ),
78- patch .object (LimitTimeUsageMixin , "_real_time_limit_factor" , 0.1 ),
83+ patch .object (LimitTimeUsageMixin , "_real_time_limit_factor" , 1.0 ),
7984 ):
8085 async with pool .get_worker (PACKAGE , 1 , 1 ) as worker :
8186 await worker .get_manifest ()
8287 assert isinstance (exc_info .value .__cause__ , WorkerRealTimeLimitExceededError )
88+ assert 0.6 < (time () - start_time ) < 2.0
0 commit comments