@@ -70,6 +70,7 @@ def __init__(
7070 target_tracking_rater : TargetTrackingRater = _BRISQUE_TRACKING_RATER ,
7171 real_http : bool = False ,
7272 response_delay_seconds : float = 0.0 ,
73+ sleep_fn : Callable [[float ], None ] = time .sleep ,
7374 ) -> None :
7475 """Route requests to Vuforia's Web Service APIs to fakes of those
7576 APIs.
@@ -91,13 +92,18 @@ def __init__(
9192 target_tracking_rater: A callable for rating targets for tracking.
9293 response_delay_seconds: The number of seconds to delay each
9394 response by. This can be used to test timeout handling.
95+ sleep_fn: The function to use for sleeping during response
96+ delays. Defaults to ``time.sleep``. Inject a custom
97+ function to control virtual time in tests without
98+ monkey-patching.
9499
95100 Raises:
96101 MissingSchemeError: There is no scheme in a given URL.
97102 """
98103 super ().__init__ ()
99104 self ._real_http = real_http
100105 self ._response_delay_seconds = response_delay_seconds
106+ self ._sleep_fn = sleep_fn
101107 self ._mock : RequestsMock
102108 self ._target_manager = TargetManager ()
103109
@@ -136,6 +142,7 @@ def add_database(self, database: VuforiaDatabase) -> None:
136142 def _wrap_callback (
137143 callback : _Callback ,
138144 delay_seconds : float ,
145+ sleep_fn : Callable [[float ], None ],
139146 ) -> _Callback :
140147 """Wrap a callback to add a response delay."""
141148
@@ -159,11 +166,11 @@ def wrapped(
159166 effective = float (timeout )
160167
161168 if effective is not None and delay_seconds > effective :
162- time . sleep (effective )
169+ sleep_fn (effective )
163170 raise requests .exceptions .Timeout
164171
165172 result = callback (request )
166- time . sleep (delay_seconds )
173+ sleep_fn (delay_seconds )
167174 return result
168175
169176 return wrapped
@@ -195,6 +202,7 @@ def __enter__(self) -> Self:
195202 callback = self ._wrap_callback (
196203 callback = original_callback ,
197204 delay_seconds = self ._response_delay_seconds ,
205+ sleep_fn = self ._sleep_fn ,
198206 ),
199207 content_type = None ,
200208 )
0 commit comments