Skip to content

Commit 134dd58

Browse files
authored
Merge pull request #140 from altendky/disable_signal_handlers
2 parents 36ab2ba + d7be1da commit 134dd58

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

pytest_twisted.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import functools
22
import inspect
33
import itertools
4+
import signal
45
import sys
56
import warnings
67

@@ -199,6 +200,11 @@ def init_twisted_greenlet():
199200
return
200201

201202
if not _instances.reactor.running:
203+
if signal.getsignal(signal.SIGINT) == signal.default_int_handler:
204+
signal.signal(
205+
signal.SIGINT,
206+
functools.partial(signal.default_int_handler),
207+
)
202208
_instances.gr_twisted = greenlet.greenlet(_instances.reactor.run)
203209
# give me better tracebacks:
204210
failure.Failure.cleanFailure = lambda self: None

testing/test_basic.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import sys
34
import textwrap
45

@@ -13,6 +14,11 @@
1314

1415
timeout = 15
1516

17+
pytest_version = tuple(
18+
int(segment)
19+
for segment in pytest.__version__.split(".")[:3]
20+
)
21+
1622

1723
# https://github.com/pytest-dev/pytest/issues/6505
1824
def force_plural(name):
@@ -1179,3 +1185,73 @@ def test_succeed():
11791185
testdir.makepyfile(test_file)
11801186
rr = testdir.run(*cmd_opts, timeout=timeout)
11811187
rr.stdout.fnmatch_lines(lines2=[test_string])
1188+
1189+
1190+
def test_sigint_for_regular_tests(testdir, cmd_opts):
1191+
test_file = """
1192+
import os
1193+
import signal
1194+
import time
1195+
1196+
import twisted.internet
1197+
import twisted.internet.task
1198+
1199+
def test_self_cancel():
1200+
os.kill(os.getpid(), signal.SIGINT)
1201+
time.sleep(10)
1202+
1203+
def test_should_not_run():
1204+
assert False
1205+
"""
1206+
testdir.makepyfile(test_file)
1207+
rr = testdir.run(*cmd_opts, timeout=timeout)
1208+
if sys.platform != "win32":
1209+
# on Windows pytest isn't even reporting the status, just stopping...
1210+
assert_outcomes(rr, {})
1211+
rr.stdout.re_match_lines(lines2=[r".* no tests ran in .*"])
1212+
1213+
pattern = r".*test_should_not_run.*"
1214+
1215+
if pytest_version >= (5, 3, 0):
1216+
rr.stdout.no_re_match_line(pat=pattern)
1217+
else:
1218+
assert re.match(pattern, rr.stdout.str()) is None
1219+
1220+
1221+
def test_sigint_for_inline_callbacks_tests(testdir, cmd_opts):
1222+
test_file = """
1223+
import os
1224+
import signal
1225+
1226+
import twisted.internet
1227+
import twisted.internet.task
1228+
1229+
import pytest_twisted
1230+
1231+
@pytest_twisted.inlineCallbacks
1232+
def test_self_cancel():
1233+
os.kill(os.getpid(), signal.SIGINT)
1234+
yield twisted.internet.task.deferLater(
1235+
twisted.internet.reactor,
1236+
9999,
1237+
lambda: None,
1238+
)
1239+
1240+
@pytest_twisted.inlineCallbacks
1241+
def test_should_not_run():
1242+
assert False
1243+
yield
1244+
"""
1245+
testdir.makepyfile(test_file)
1246+
rr = testdir.run(*cmd_opts, timeout=timeout)
1247+
if sys.platform != "win32":
1248+
# on Windows pytest isn't even reporting the status, just stopping...
1249+
assert_outcomes(rr, {})
1250+
rr.stdout.re_match_lines(lines2=[r".* no tests ran in .*"])
1251+
1252+
pattern = r".*test_should_not_run.*"
1253+
1254+
if pytest_version >= (5, 3, 0):
1255+
rr.stdout.no_re_match_line(pat=pattern)
1256+
else:
1257+
assert re.match(pattern, rr.stdout.str()) is None

0 commit comments

Comments
 (0)