From c2f9013ab24ce94889a7eefb4a16f7ff6bb1c044 Mon Sep 17 00:00:00 2001 From: Yuriy Bilopolov Date: Thu, 8 Mar 2018 13:30:09 +0200 Subject: [PATCH] fix blockon lock forrever --- pytest_twisted.py | 12 +++++++++--- testing/test_basic.py | 4 +++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pytest_twisted.py b/pytest_twisted.py index 1641bc8..284acb6 100644 --- a/pytest_twisted.py +++ b/pytest_twisted.py @@ -13,6 +13,10 @@ class WrongReactorAlreadyInstalledError(Exception): pass +class _config: + external_reactor = False + + class _instances: gr_twisted = None reactor = None @@ -23,7 +27,7 @@ def pytest_namespace(): def blockon(d): - if _instances.reactor.running: + if _config.external_reactor: return block_from_thread(d) return blockon_default(d) @@ -61,13 +65,15 @@ def inlineCallbacks(fun, *args, **kw): def init_twisted_greenlet(): - if _instances.reactor is None: + if _instances.reactor is None or _instances.gr_twisted: return - if not _instances.gr_twisted and not _instances.reactor.running: + if not _instances.reactor.running: _instances.gr_twisted = greenlet.greenlet(_instances.reactor.run) # give me better tracebacks: failure.Failure.cleanFailure = lambda self: None + else: + _config.external_reactor = True def stop_twisted_greenlet(): diff --git a/testing/test_basic.py b/testing/test_basic.py index 8e1c34a..2eec78b 100755 --- a/testing/test_basic.py +++ b/testing/test_basic.py @@ -146,8 +146,10 @@ def test_blockon_in_hook(testdir): def pytest_configure(config): pt.init_default_reactor() - d = defer.Deferred() + d, d2 = defer.Deferred(), defer.Deferred() reactor.callLater(0.01, d.callback, 1) + reactor.callLater(0.02, d2.callback, 1) + pt.blockon(d) pt.blockon(d) """) testdir.makepyfile("""