diff options
| author | Damian Johnson <atagar@torproject.org> | 2019-04-10 11:16:38 -0700 |
|---|---|---|
| committer | Damian Johnson <atagar@torproject.org> | 2019-04-10 11:21:57 -0700 |
| commit | 854338ca172b5a3fbeb588a905f20878556fe816 (patch) | |
| tree | 1731209469156c87259d97a5cfb8df8e29656ca7 | |
| parent | cb774063b50e0909b9c34645d12bd6a89bcdd9df (diff) | |
Move stacktrace signaling into test invocation
Functionally teor's patch is fine as test/__init__.py is only imported
in testing contexts. That said, as a general practice it's better to
invoke functional code (like signal listeners) when a module is invoked
rather than imported.
All testing is invoked through run_tests.py's main method, so we can
register our signal handlers there instead.
| -rwxr-xr-x | run_tests.py | 16 | ||||
| -rw-r--r-- | test/__init__.py | 18 |
2 files changed, 16 insertions, 18 deletions
diff --git a/run_tests.py b/run_tests.py index c31e7b48..f3959d01 100755 --- a/run_tests.py +++ b/run_tests.py @@ -7,6 +7,7 @@ Runs unit and integration tests. For usage information run this with '--help'. """ import os +import signal import sys import threading import time @@ -70,6 +71,18 @@ New capabilities are: """ +def log_traceback(sig, frame): + """ + Signal handler that logs the present traceback, and aborts our process with + exit status -1 in the case of SIGABRT. + """ + + print('Signal %s received.\nTraceback:\n%s' % (sig, traceback.format_stack(frame))) + + if sig == signal.SIGABRT: + sys.exit(-1) + + def get_unit_tests(module_prefix = None): """ Provides the classes for our unit tests. @@ -124,6 +137,9 @@ def main(): println('%s\n' % exc) sys.exit(1) + signal.signal(signal.SIGABRT, log_traceback) + signal.signal(signal.SIGUSR1, log_traceback) + test_config = stem.util.conf.get_config('test') test_config.load(os.path.join(test.STEM_BASE, 'test', 'settings.cfg')) diff --git a/test/__init__.py b/test/__init__.py index dc1ab559..fa9678af 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -16,28 +16,10 @@ Unit and integration tests for the stem library. Helpers include... import collections import itertools import os -import signal -import sys -import traceback import stem.util.enum import stem.version -# Install signal handlers before doing anything else -def log_traceback(sig, frame): - """Log a stack trace. exit(-1) if the signal was SIGABRT.""" - message = "Signal {} received.\nTraceback:\n".format(sig) - message += ''.join(traceback.format_stack(frame)) - print(message) - if sig == signal.SIGABRT: - sys.exit(-1) - -# Register handlers -# Log stack trace and exit -signal.signal(signal.SIGABRT, log_traceback) -# Log stack trace and continue -signal.signal(signal.SIGUSR1, log_traceback) - __all__ = [ 'network', 'output', |
