30
30
)
31
31
32
32
import pytest
33
+ import pytest_asyncio
33
34
34
35
from cylc .flow .config import WorkflowConfig
35
36
from cylc .flow .id import Tokens
@@ -112,15 +113,15 @@ def _pytest_passed(request: pytest.FixtureRequest) -> bool:
112
113
))
113
114
114
115
115
- @pytest .fixture (scope = 'session' )
116
+ @pytest_asyncio .fixture (scope = 'session' )
116
117
def run_dir ():
117
118
"""The cylc run directory for this host."""
118
119
path = Path (get_cylc_run_dir ())
119
120
path .mkdir (exist_ok = True )
120
121
yield path
121
122
122
123
123
- @pytest .fixture (scope = 'session' )
124
+ @pytest_asyncio .fixture (scope = 'session' )
124
125
def ses_test_dir (request , run_dir ):
125
126
"""The root run dir for test flows in this test session."""
126
127
timestamp = get_current_time_string (use_basic_format = True )
@@ -131,7 +132,7 @@ def ses_test_dir(request, run_dir):
131
132
_rm_if_empty (path )
132
133
133
134
134
- @pytest .fixture (scope = 'module' )
135
+ @pytest_asyncio .fixture (scope = 'module' )
135
136
def mod_test_dir (request , ses_test_dir ):
136
137
"""The root run dir for test flows in this test module."""
137
138
path = Path (
@@ -163,7 +164,7 @@ def test_dir(request, mod_test_dir):
163
164
_rm_if_empty (path )
164
165
165
166
166
- @pytest .fixture (scope = 'module' )
167
+ @pytest_asyncio .fixture (scope = 'module' )
167
168
def mod_flow (run_dir , mod_test_dir ):
168
169
"""A function for creating module-level flows."""
169
170
yield partial (_make_flow , run_dir , mod_test_dir )
@@ -175,7 +176,7 @@ def flow(run_dir, test_dir):
175
176
yield partial (_make_flow , run_dir , test_dir )
176
177
177
178
178
- @pytest .fixture (scope = 'module' )
179
+ @pytest_asyncio .fixture (scope = 'module' )
179
180
def mod_scheduler ():
180
181
"""Return a Scheduler object for a flow.
181
182
@@ -197,7 +198,7 @@ def scheduler():
197
198
yield _scheduler
198
199
199
200
200
- @pytest .fixture (scope = 'module' )
201
+ @pytest_asyncio .fixture (scope = 'module' )
201
202
def mod_start ():
202
203
"""Start a scheduler but don't set it running (module scope)."""
203
204
return partial (_start_flow , None )
@@ -209,7 +210,7 @@ def start(caplog: pytest.LogCaptureFixture):
209
210
return partial (_start_flow , caplog )
210
211
211
212
212
- @pytest .fixture (scope = 'module' )
213
+ @pytest_asyncio .fixture (scope = 'module' )
213
214
def mod_run ():
214
215
"""Start a scheduler and set it running (module scope)."""
215
216
return partial (_run_flow , None )
@@ -235,7 +236,7 @@ def one_conf():
235
236
}
236
237
237
238
238
- @pytest .fixture (scope = 'module' )
239
+ @pytest_asyncio .fixture (scope = 'module' )
239
240
def mod_one_conf ():
240
241
return {
241
242
'scheduler' : {
@@ -257,36 +258,37 @@ def one(one_conf, flow, scheduler):
257
258
return schd
258
259
259
260
260
- @pytest .fixture (scope = 'module' )
261
+ @pytest_asyncio .fixture (scope = 'module' )
261
262
def mod_one (mod_one_conf , mod_flow , mod_scheduler ):
262
263
id_ = mod_flow (mod_one_conf )
263
264
schd = mod_scheduler (id_ )
264
265
return schd
265
266
266
267
267
- @pytest .fixture (scope = 'module' )
268
- def event_loop ():
269
- """This fixture defines the event loop used for each test.
270
-
271
- The default scoping for this fixture is "function" which means that all
272
- async fixtures must have "function" scoping.
273
-
274
- Defining `event_loop` as a module scoped fixture opens the door to
275
- module scoped fixtures but means all tests in a module will run in the same
276
- event loop. This is fine, it's actually an efficiency win but also
277
- something to be aware of.
278
-
279
- See: https://github.com/pytest-dev/pytest-asyncio/issues/171
280
-
281
- """
282
- loop = asyncio .get_event_loop_policy ().new_event_loop ()
283
- yield loop
284
- # gracefully exit async generators
285
- loop .run_until_complete (loop .shutdown_asyncgens ())
286
- # cancel any tasks still running in this event loop
287
- for task in asyncio .all_tasks (loop ):
288
- task .cancel ()
289
- loop .close ()
268
+ if pytest_asyncio .__version__ .startswith ('0.21' ):
269
+ # BACK COMPAT: event_loop
270
+ # FROM: python 3
271
+ # TO: python 3.7
272
+ # URL: https://github.com/cylc/cylc-flow/pull/6726
273
+ @pytest_asyncio .fixture (scope = 'module' )
274
+ def event_loop ():
275
+ """This fixture defines the event loop used for each test.
276
+ The default scoping for this fixture is "function" which means that all
277
+ async fixtures must have "function" scoping.
278
+ Defining `event_loop` as a module scoped fixture opens the door to
279
+ module scoped fixtures but means all tests in a module will run in the same
280
+ event loop. This is fine, it's actually an efficiency win but also
281
+ something to be aware of.
282
+ See: https://github.com/pytest-dev/pytest-asyncio/issues/171
283
+ """
284
+ loop = asyncio .get_event_loop_policy ().new_event_loop ()
285
+ yield loop
286
+ # gracefully exit async generators
287
+ loop .run_until_complete (loop .shutdown_asyncgens ())
288
+ # cancel any tasks still running in this event loop
289
+ for task in asyncio .all_tasks (loop ):
290
+ task .cancel ()
291
+ loop .close ()
290
292
291
293
292
294
@pytest .fixture
@@ -382,7 +384,7 @@ def _validate(id_: Union[str, Path], **kwargs) -> WorkflowConfig:
382
384
return _validate
383
385
384
386
385
- @pytest .fixture (scope = 'module' )
387
+ @pytest_asyncio .fixture (scope = 'module' )
386
388
def mod_validate (run_dir ):
387
389
"""Provides a function for validating workflow configurations.
388
390
@@ -465,7 +467,7 @@ def run_job_cmd(
465
467
return _disable_polling
466
468
467
469
468
- @pytest .fixture (scope = 'module' )
470
+ @pytest_asyncio .fixture (scope = 'module' )
469
471
def mod_workflow_source (mod_flow , tmp_path_factory ):
470
472
"""Create a workflow source directory.
471
473
@@ -686,7 +688,7 @@ def complete():
686
688
return _complete
687
689
688
690
689
- @pytest .fixture (scope = 'module' )
691
+ @pytest_asyncio .fixture (scope = 'module' )
690
692
def mod_complete ():
691
693
return _complete
692
694
0 commit comments