Skip to content

Commit

Permalink
Use default SIGINT handler if no tests are running. (google#897)
Browse files Browse the repository at this point in the history
* Move signal.signal() registration from package __init__ to Test.execute

* Revert "Move signal.signal() registration from package __init__ to Test.execute"

This reverts commit 6f6729c.

* Use default SIGINT handler if no tests are running.
  • Loading branch information
glados-verma authored and arsharma1 committed Sep 6, 2019
1 parent 05b2a06 commit 3dcdc2c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion openhtf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ def get_version():
__version__ = get_version()

# Register signal handler to stop all tests on SIGINT.
signal.signal(signal.SIGINT, Test.handle_sig_int)
Test.DEFAULT_SIGINT_HANDLER = signal.signal(signal.SIGINT, Test.handle_sig_int)
14 changes: 9 additions & 5 deletions openhtf/core/test_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def PhaseTwo(test):

TEST_INSTANCES = weakref.WeakValueDictionary()
HANDLED_SIGINT_ONCE = False
DEFAULT_SIGINT_HANDLER = None

def __init__(self, *phases, **metadata):
# Some sanity checks on special metadata keys we automatically fill in.
Expand Down Expand Up @@ -228,11 +229,14 @@ def configure(self, **kwargs):
setattr(self._test_options, key, value)

@classmethod
def handle_sig_int(cls, *_):
if cls.TEST_INSTANCES:
_LOG.error('Received SIGINT, stopping all tests.')
for test in cls.TEST_INSTANCES.values():
test.abort_from_sig_int()
def handle_sig_int(cls, signalnum, handler):
if not cls.TEST_INSTANCES:
cls.DEFAULT_SIGINT_HANDLER(signalnum, handler)
return

_LOG.error('Received SIGINT, stopping all tests.')
for test in cls.TEST_INSTANCES.values():
test.abort_from_sig_int()
if not cls.HANDLED_SIGINT_ONCE:
cls.HANDLED_SIGINT_ONCE = True
raise KeyboardInterrupt
Expand Down

0 comments on commit 3dcdc2c

Please sign in to comment.