File tree 1 file changed +15
-5
lines changed
1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -125,24 +125,34 @@ def pytest_pyfunc_call(pyfuncitem):
125
125
if 'asyncio' in pyfuncitem .keywords :
126
126
if getattr (pyfuncitem .obj , 'is_hypothesis_test' , False ):
127
127
pyfuncitem .obj .hypothesis .inner_test = wrap_in_sync (
128
- pyfuncitem .obj .hypothesis .inner_test
128
+ pyfuncitem .obj .hypothesis .inner_test ,
129
+ _loop = pyfuncitem .funcargs ['event_loop' ]
129
130
)
130
131
else :
131
- pyfuncitem .obj = wrap_in_sync (pyfuncitem .obj )
132
+ pyfuncitem .obj = wrap_in_sync (
133
+ pyfuncitem .obj ,
134
+ _loop = pyfuncitem .funcargs ['event_loop' ]
135
+ )
132
136
yield
133
137
134
138
135
- def wrap_in_sync (func ):
139
+ def wrap_in_sync (func , _loop ):
136
140
"""Return a sync wrapper around an async function executing it in the
137
141
current event loop."""
138
142
139
143
@functools .wraps (func )
140
144
def inner (** kwargs ):
141
145
coro = func (** kwargs )
142
146
if coro is not None :
143
- task = asyncio .ensure_future (coro )
144
147
try :
145
- asyncio .get_event_loop ().run_until_complete (task )
148
+ loop = asyncio .get_event_loop ()
149
+ except RuntimeError as exc :
150
+ if 'no current event loop' not in str (exc ):
151
+ raise
152
+ loop = _loop
153
+ task = asyncio .ensure_future (coro , loop = loop )
154
+ try :
155
+ loop .run_until_complete (task )
146
156
except BaseException :
147
157
# run_until_complete doesn't get the result from exceptions
148
158
# that are not subclasses of `Exception`. Consume all
You can’t perform that action at this time.
0 commit comments