4
4
import resource
5
5
from collections .abc import Iterator
6
6
from contextlib import contextmanager
7
- from time import process_time , sleep
7
+ from time import process_time , sleep , time
8
8
from unittest .mock import patch
9
9
10
10
import psutil
@@ -53,10 +53,13 @@ def busy_wait(self: WorkerManager) -> None:
53
53
54
54
async def test_should_raise_cpu_timout_error (pool : WorkerPool ) -> None :
55
55
with patch_worker_pool (pool , _make_get_manifest_busy_wait ):
56
+ start_time = time ()
57
+ # Change the timeout for faster testing.
56
58
with pytest .raises (WorkerStartError ) as exc_info , patch .object (BaseWorker , "_init_worker_timeout" , 0.05 ):
57
59
async with pool .get_worker (PACKAGE , 1 , 1 ):
58
60
pass
59
61
assert isinstance (exc_info .value .__cause__ , WorkerCPUTimeLimitExceededError )
62
+ assert 0.05 < (time () - start_time ) < 0.5
60
63
61
64
62
65
@contextmanager
@@ -72,11 +75,14 @@ async def test_should_raise_real_timout_error(pool: WorkerPool) -> None:
72
75
with patch_worker_pool (pool , _make_get_manifest_sleep ):
73
76
# The timeout should not be too short, because the Python interpreter also needs some time to start up, which
74
77
# is accounted for the init worker step. Otherwise, a WorkerCPUTimeLimitExceededError is raised.
78
+ start_time = time ()
75
79
with (
76
80
pytest .raises (WorkerStartError ) as exc_info ,
81
+ # Change the timeout and factor for faster testing.
77
82
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 ),
79
84
):
80
85
async with pool .get_worker (PACKAGE , 1 , 1 ) as worker :
81
86
await worker .get_manifest ()
82
87
assert isinstance (exc_info .value .__cause__ , WorkerRealTimeLimitExceededError )
88
+ assert 0.6 < (time () - start_time ) < 2.0
0 commit comments