Skip to content

Commit 779e04f

Browse files
author
Stas Erema
committed
add support for asyncioreactor with --reactor=asyncio
1 parent d1c02e2 commit 779e04f

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

pytest_twisted.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,19 @@ def init_qt5_reactor():
171171
)
172172

173173

174+
def init_asyncio_reactor():
175+
from twisted.internet import asyncioreactor
176+
177+
_install_reactor(
178+
reactor_installer=asyncioreactor.install,
179+
reactor_type=asyncioreactor.AsyncioSelectorReactor,
180+
)
181+
182+
174183
reactor_installers = {
175184
"default": init_default_reactor,
176185
"qt5reactor": init_qt5_reactor,
186+
"asyncio": init_asyncio_reactor,
177187
}
178188

179189

testing/test_basic.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,49 @@ def main():
447447
assert_outcomes(rr, {"passed": 1, "failed": 1})
448448
# test embedded mode:
449449
assert testdir.run(sys.executable, "runner.py").ret == 0
450+
451+
452+
def test_blockon_in_hook_with_asyncio(testdir, cmd_opts, request):
453+
skip_if_reactor_not(request, "asyncio")
454+
conftest_file = """
455+
import pytest_twisted as pt
456+
from twisted.internet import defer
457+
458+
def pytest_configure(config):
459+
pt.init_asyncio_reactor()
460+
d = defer.Deferred()
461+
462+
from twisted.internet import reactor
463+
464+
reactor.callLater(0.01, d.callback, 1)
465+
pt.blockon(d)
466+
"""
467+
testdir.makeconftest(conftest_file)
468+
test_file = """
469+
from twisted.internet import reactor, defer
470+
471+
def test_succeed():
472+
d = defer.Deferred()
473+
reactor.callLater(0.01, d.callback, 1)
474+
return d
475+
"""
476+
testdir.makepyfile(test_file)
477+
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
478+
assert_outcomes(rr, {"passed": 1})
479+
480+
481+
def test_wrong_reactor_with_asyncio(testdir, cmd_opts, request):
482+
skip_if_reactor_not(request, "asyncio")
483+
conftest_file = """
484+
def pytest_addhooks():
485+
import twisted.internet.default
486+
twisted.internet.default.install()
487+
"""
488+
testdir.makeconftest(conftest_file)
489+
test_file = """
490+
def test_succeed():
491+
pass
492+
"""
493+
testdir.makepyfile(test_file)
494+
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
495+
assert "WrongReactorAlreadyInstalledError" in rr.stderr.str()

tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tox]
22
envlist=
33
py{27,34}-defaultreactor
4-
py{35,36,37}-{default,qt5}reactor
4+
py{35,36,37}-{default,qt5,asyncio}reactor
55
win-py{35,36,37}-qt5reactor
66
linting
77

@@ -18,6 +18,7 @@ deps=
1818
commands=
1919
defaultreactor: pytest --reactor=default
2020
qt5reactor: pytest --reactor=qt5reactor
21+
asyncio: pytest --reactor=asyncio
2122
sitepackages=False
2223

2324
[testenv:linting]

0 commit comments

Comments
 (0)