diff options
author | Bernát Gábor <bgabor8@bloomberg.net> | 2020-12-30 23:52:33 +0000 |
---|---|---|
committer | Bernát Gábor <bgabor8@bloomberg.net> | 2020-12-31 11:35:16 +0000 |
commit | aca8b8f8e05d1a434999b1a9eb17b6766924d0fa (patch) | |
tree | 543171dae16fadcbf93b2fc444a2f26d9401fa56 | |
parent | d92f110a38f8f53fae3d481ea517f24490738d4d (diff) | |
download | tox-git-aca8b8f8e05d1a434999b1a9eb17b6766924d0fa.tar.gz |
Add coverage for the delayed signal handler
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
-rw-r--r-- | src/tox/util/signal.py | 3 | ||||
-rw-r--r-- | tests/execute/local_subprocess/test_local_subprocess.py | 1 | ||||
-rw-r--r-- | tests/util/test_signal.py | 43 |
3 files changed, 45 insertions, 2 deletions
diff --git a/src/tox/util/signal.py b/src/tox/util/signal.py index e0cd8523..6d413a3b 100644 --- a/src/tox/util/signal.py +++ b/src/tox/util/signal.py @@ -12,10 +12,11 @@ class DelayedSignal: self._frame: Optional[FrameType] = None self._old_handler: Union[Callable[[Signals, FrameType], None], int, Handlers, None] = None - def __enter__(self) -> None: + def __enter__(self) -> "DelayedSignal": self._signal, self._frame = None, None if threading.current_thread() == threading.main_thread(): # signals are always handled on the main thread only self._old_handler = signal(self._of, self._handler) + return self def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] diff --git a/tests/execute/local_subprocess/test_local_subprocess.py b/tests/execute/local_subprocess/test_local_subprocess.py index f7bbde03..b3864a25 100644 --- a/tests/execute/local_subprocess/test_local_subprocess.py +++ b/tests/execute/local_subprocess/test_local_subprocess.py @@ -205,7 +205,6 @@ def test_command_does_not_exist(capsys: CaptureFixture, caplog: LogCaptureFixtur @pytest.mark.skipif(sys.platform == "win32", reason="You need a conhost shell for keyboard interrupt") -# @pytest.mark.timeout(10) def test_command_keyboard_interrupt(tmp_path: Path, monkeypatch: MonkeyPatch, capfd: CaptureFixture) -> None: monkeypatch.chdir(tmp_path) process_up_signal = tmp_path / "signal" diff --git a/tests/util/test_signal.py b/tests/util/test_signal.py new file mode 100644 index 00000000..6b2640fb --- /dev/null +++ b/tests/util/test_signal.py @@ -0,0 +1,43 @@ +import logging +import os +import sys +from threading import Thread +from time import sleep + +import pytest + +from tox.execute.local_sub_process import SIG_INTERRUPT +from tox.pytest import LogCaptureFixture +from tox.util.signal import DelayedSignal + + +@pytest.mark.skipif(sys.platform == "win32", reason="You need a conhost shell for keyboard interrupt") +def test_signal_delayed(caplog: LogCaptureFixture) -> None: + caplog.set_level(level=logging.DEBUG) + with pytest.raises(KeyboardInterrupt): + with DelayedSignal() as handler: + try: + os.kill(os.getpid(), SIG_INTERRUPT) + while True: + sleep(0.05) + if handler._signal is not None: # pragma: no branch + break + except KeyboardInterrupt: # pragma: no cover + assert False # pragma: no cover + assert caplog.messages == [ + "Received 2, delaying it", + "Handling delayed 2", + ] + + +def test_signal_no_op_background(caplog: LogCaptureFixture) -> None: + caplog.set_level(level=logging.DEBUG) + + def _run() -> None: + with DelayedSignal(): + pass + + thread = Thread(target=_run) + thread.start() + thread.join() + assert caplog.messages == [] |