@@ -193,64 +193,62 @@ def pytest_runtest_setup(item):
193
193
# to marked test functions
194
194
_markers_2_fixtures = {
195
195
'asyncio' : 'event_loop' ,
196
- 'asyncio_clock' : 'clock_event_loop' ,
197
196
}
198
197
199
198
200
- @pytest .yield_fixture
201
- def event_loop (request ):
202
- """Create an instance of the default event loop for each test case."""
203
- loop = asyncio .get_event_loop_policy ().new_event_loop ()
204
- yield loop
205
- loop .close ()
206
-
207
-
208
- def _clock_event_loop_class ():
199
+ def _event_loop_policy ():
209
200
"""
210
201
Create a new class for ClockEventLoop based on the current
211
202
class-type produced by `asyncio.new_event_loop()`. This is important
212
203
for instances in which the enent-loop-policy has been changed.
213
204
"""
214
- class ClockEventLoop (asyncio .new_event_loop ().__class__ ):
205
+ class ClockEventLoopPolicy (asyncio .get_event_loop_policy ().__class__ ):
215
206
"""
216
207
A custom event loop that explicitly advances time when requested. Otherwise,
217
208
this event loop advances time as expected.
218
209
"""
219
- def __init__ (self , * args , ** kwargs ):
220
- super ().__init__ (* args , ** kwargs )
221
- self ._offset = 0
222
-
223
- def time (self ):
224
- """
225
- Return the time according the event loop's clock.
226
-
227
- This time is adjusted by the stored offset that allows for advancement
228
- with `advance_time`.
229
- """
230
- return super ().time () + self ._offset
231
-
232
- def advance_time (self , seconds ):
233
- '''
234
- Advance time by a given offset in seconds. Returns an awaitable
235
- that will complete after all tasks scheduled for after advancement
236
- of time are proceeding.
237
- '''
238
- if seconds > 0 :
239
- # advance the clock by the given offset
240
- self ._offset += seconds
241
-
242
- # Once the clock is adjusted, new tasks may have just been
243
- # scheduled for running in the next pass through the event loop
244
- return self .create_task (asyncio .sleep (0 ))
245
-
246
- return ClockEventLoop
210
+ def new_event_loop (self ):
211
+ parent_loop = super ().new_event_loop ()
212
+ parent_loop .close ()
213
+
214
+ class ClockEventLoop (parent_loop .__class__ ):
215
+ def __init__ (self , * args , ** kwargs ):
216
+ super ().__init__ (* args , ** kwargs )
217
+ self ._clockoffset = 0
218
+
219
+ def time (self ):
220
+ """
221
+ Return the time according the event loop's clock.
222
+
223
+ This time is adjusted by the stored offset that allows for advancement
224
+ with `advance_time`.
225
+ """
226
+ return super ().time () + self ._clockoffset
227
+
228
+ def advance_time (self , seconds ):
229
+ '''
230
+ Advance time by a given offset in seconds. Returns an awaitable
231
+ that will complete after all tasks scheduled for after advancement
232
+ of time are proceeding.
233
+ '''
234
+ if seconds > 0 :
235
+ # advance the clock by the given offset
236
+ self ._clockoffset += seconds
237
+
238
+ # Once the clock is adjusted, new tasks may have just been
239
+ # scheduled for running in the next pass through the event loop
240
+ return self .create_task (asyncio .sleep (0 ))
241
+
242
+ return ClockEventLoop ()
243
+ return ClockEventLoopPolicy ()
247
244
248
245
249
246
@pytest .yield_fixture
250
- def clock_event_loop (request ):
247
+ def event_loop (request ):
251
248
"""Create an instance of the default event loop for each test case."""
252
- loop = _clock_event_loop_class ()()
253
- asyncio .get_event_loop_policy ().set_event_loop (loop )
249
+ # reset the event loop policy: modify existing policy to give time advancement
250
+ asyncio .set_event_loop_policy (_event_loop_policy ())
251
+ loop = asyncio .get_event_loop_policy ().new_event_loop ()
254
252
yield loop
255
253
loop .close ()
256
254
0 commit comments