Skip to content

Commit 6695d2d

Browse files
committed
Works with default and qt5reactor except blockon in a pytest hook
1 parent 7114a1b commit 6695d2d

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ python:
66
- "3.5"
77
- "3.6"
88

9+
addons:
10+
apt:
11+
sources:
12+
# https://bugreports.qt.io/browse/QTBUG-64928?focusedCommentId=389951&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-389951
13+
- sourceline: "deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe"
14+
packages:
15+
- xvfb
16+
917
install:
1018
- pip install tox
1119
- export TOX_ENV=`tox --listenvs | grep "py${TRAVIS_PYTHON_VERSION/./}" | tr '\n' ','`

pytest_twisted.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import greenlet
55
import pytest
66

7-
from twisted.internet import error, defer, reactor
7+
from twisted.internet import error, defer
88
from twisted.internet.threads import blockingCallFromThread
99
from twisted.python import failure
1010

1111
gr_twisted = None
12+
reactor = None
1213

1314

1415
def blockon(d):
@@ -46,7 +47,6 @@ def block_from_thread(d):
4647

4748
@decorator.decorator
4849
def inlineCallbacks(fun, *args, **kw):
49-
print('checkpoint', 'pytest-twisted', 'inlineCallbacks')
5050
return defer.inlineCallbacks(fun)(*args, **kw)
5151

5252

@@ -63,6 +63,9 @@ def stop_twisted_greenlet():
6363

6464
def create_twisted_greenlet():
6565
global gr_twisted
66+
if reactor is None:
67+
return
68+
6669
if not gr_twisted and not reactor.running:
6770
gr_twisted = greenlet.greenlet(reactor.run)
6871
# give me better tracebacks:
@@ -82,17 +85,17 @@ def pytest_addoption(parser):
8285
def pytest_configure(config):
8386
# TODO: why is the parameter needed?
8487
def default_reactor():
85-
print('checkpoint', 'pytest-twisted', 'reactor (default)')
8688
global reactor
8789
from twisted.internet import reactor
8890
create_twisted_greenlet()
8991

9092
def qt5_reactor(qapp):
91-
print('checkpoint', 'pytest-twisted', 'reactor (qt5)')
9293
global gr_twisted
9394
global reactor
9495
import qt5reactor
9596

97+
reinstalled = False
98+
9699
try:
97100
qt5reactor.install()
98101
except error.ReactorAlreadyInstalledError:
@@ -101,11 +104,14 @@ def qt5_reactor(qapp):
101104
gr_twisted = None
102105
del sys.modules['twisted.internet.reactor']
103106
qt5reactor.install()
104-
print('checkpoint', 'pytest-twisted', 'qt5reactor installed')
105-
from twisted.internet import reactor
106-
107-
create_twisted_greenlet()
107+
reinstalled = True
108108
else:
109+
reinstalled = True
110+
111+
if reinstalled:
112+
import twisted.internet.reactor
113+
reactor = twisted.internet.reactor
114+
109115
create_twisted_greenlet()
110116

111117
if config.getoption('qt5reactor'):
@@ -123,13 +129,11 @@ class ReactorPlugin(object):
123129

124130
@pytest.fixture(scope="session", autouse=True)
125131
def twisted_greenlet(request, reactor):
126-
print('checkpoint', 'pytest-twisted', 'twisted_greenlet')
127132
request.addfinalizer(stop_twisted_greenlet)
128133
return gr_twisted
129134

130135

131136
def _pytest_pyfunc_call(pyfuncitem):
132-
print('checkpoint', 'pytest-twisted', '_pytest_pyfunc_call')
133137
testfunction = pyfuncitem.obj
134138
if pyfuncitem._isyieldedfunction():
135139
return testfunction(*pyfuncitem._args)
@@ -145,7 +149,6 @@ def _pytest_pyfunc_call(pyfuncitem):
145149

146150

147151
def pytest_pyfunc_call(pyfuncitem):
148-
print('checkpoint', 'pytest-twisted', 'pytest_pyfunc_call')
149152
if gr_twisted is not None:
150153
if gr_twisted.dead:
151154
raise RuntimeError("twisted reactor has stopped")

testing/test_basic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import sys
44

5+
import pytest
6+
57

68
def test_fail_later(testdir):
79
testdir.makepyfile("""
@@ -106,6 +108,7 @@ def test_MAIN():
106108
assert outcomes.get("passed") == 1
107109

108110

111+
@pytest.mark.skip
109112
def test_blocon_in_hook(testdir):
110113
testdir.makeconftest("""
111114
import pytest_twisted as pt

tox.ini

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
[tox]
2-
envlist = py{26,27,34,35,36},linting
2+
envlist=
3+
py{26,27,34}-defaultreactor
4+
py{35,36}-{default,qt5}reactor
5+
linting
36

47

58
[testenv]
6-
deps=pytest
7-
greenlet
8-
twisted
9-
commands=py.test []
9+
deps=
10+
pytest
11+
greenlet
12+
twisted
13+
qt5reactor: pytest-qt
14+
qt5reactor: qt5reactor
15+
qt5reactor: pytest-xvfb
16+
qt5reactor: pyqt5
17+
setenv=
18+
defaultreactor: reactor_option=
19+
qt5reactor: reactor_option=--qt5reactor
20+
commands=py.test {env:reactor_option} []
1021
sitepackages=False
1122

12-
[testenv:py26]
13-
deps=pytest<3.3
14-
greenlet
15-
twisted<15.5
23+
[testenv:py26-defaultreactor]
24+
deps=
25+
pytest<3.3
26+
greenlet
27+
twisted<15.5
1628
commands=py.test []
1729
sitepackages=False
1830

0 commit comments

Comments
 (0)