Skip to content

Commit b10cfd3

Browse files
committed
deCAMP
1 parent 408a9ce commit b10cfd3

File tree

1 file changed

+21
-56
lines changed

1 file changed

+21
-56
lines changed

pytest_twisted.py

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def decorator(f):
152152
return fixture
153153

154154

155-
_mark_attribute_name = '_pytest_twisted_coroutine_wrapper'
155+
_mark_attribute_name = '_pytest_twisted_mark'
156156
async_fixture = _marked_async_fixture('async_fixture')
157157
async_yield_fixture = _marked_async_fixture('async_yield_fixture')
158158

@@ -164,24 +164,7 @@ def pytest_fixture_setup(fixturedef, request):
164164

165165
mark = maybe_mark
166166

167-
if _instances.gr_twisted is not None:
168-
if _instances.gr_twisted.dead:
169-
raise RuntimeError("twisted reactor has stopped")
170-
171-
def in_reactor(d, f, *args):
172-
return defer.maybeDeferred(f, *args).chainDeferred(d)
173-
174-
d = defer.Deferred()
175-
_instances.reactor.callLater(
176-
0.0, in_reactor, d, _pytest_fixture_setup, fixturedef, request, mark
177-
)
178-
blockon_default(d)
179-
else:
180-
if not _instances.reactor.running:
181-
raise RuntimeError("twisted reactor is not running")
182-
blockingCallFromThread(
183-
_instances.reactor, _pytest_fixture_setup, fixturedef, request, mark
184-
)
167+
run_inline_callbacks(_pytest_fixture_setup, fixturedef, request, mark)
185168

186169
return True
187170

@@ -252,31 +235,30 @@ def tear_it_down(deferred):
252235
# claims it should also take a nextItem but that triggers a direct error
253236

254237

238+
def run_inline_callbacks(f, *args):
239+
if _instances.gr_twisted is not None:
240+
if _instances.gr_twisted.dead:
241+
raise RuntimeError("twisted reactor has stopped")
242+
243+
def in_reactor(d, f, *args):
244+
return defer.maybeDeferred(f, *args).chainDeferred(d)
245+
246+
d = defer.Deferred()
247+
_instances.reactor.callLater(0.0, in_reactor, d, f, *args)
248+
blockon_default(d)
249+
else:
250+
if not _instances.reactor.running:
251+
raise RuntimeError("twisted reactor is not running")
252+
blockingCallFromThread(_instances.reactor, f, *args)
253+
254+
255255
@pytest.hookimpl(hookwrapper=True)
256256
def pytest_runtest_teardown(item):
257257
yield
258258

259259
while len(to_be_torn_down) > 0:
260260
deferred = to_be_torn_down.pop(0)
261-
if _instances.gr_twisted is not None:
262-
if _instances.gr_twisted.dead:
263-
raise RuntimeError("twisted reactor has stopped")
264-
265-
def in_reactor(d, f, *args):
266-
# return f.chainDeferred(d)
267-
return defer.maybeDeferred(f, *args).chainDeferred(d)
268-
269-
d = defer.Deferred()
270-
_instances.reactor.callLater(
271-
0.0, in_reactor, d, tear_it_down, deferred
272-
)
273-
blockon_default(d)
274-
else:
275-
if not _instances.reactor.running:
276-
raise RuntimeError("twisted reactor is not running")
277-
blockingCallFromThread(
278-
_instances.reactor, tear_it_down, deferred,
279-
)
261+
run_inline_callbacks(tear_it_down, deferred)
280262

281263

282264
@defer.inlineCallbacks
@@ -287,24 +269,7 @@ def _pytest_pyfunc_call(pyfuncitem):
287269

288270

289271
def pytest_pyfunc_call(pyfuncitem):
290-
if _instances.gr_twisted is not None:
291-
if _instances.gr_twisted.dead:
292-
raise RuntimeError("twisted reactor has stopped")
293-
294-
def in_reactor(d, f, *args):
295-
return defer.maybeDeferred(f, *args).chainDeferred(d)
296-
297-
d = defer.Deferred()
298-
_instances.reactor.callLater(
299-
0.0, in_reactor, d, _pytest_pyfunc_call, pyfuncitem
300-
)
301-
blockon_default(d)
302-
else:
303-
if not _instances.reactor.running:
304-
raise RuntimeError("twisted reactor is not running")
305-
blockingCallFromThread(
306-
_instances.reactor, _pytest_pyfunc_call, pyfuncitem
307-
)
272+
run_inline_callbacks(_pytest_pyfunc_call, pyfuncitem)
308273
return True
309274

310275

0 commit comments

Comments
 (0)