Commit b90d90ce authored by dieter's avatar dieter

Adhere in `TestGroup` to the Python standard to name the method target `self`

Add the thread name to failure reports due to exceptions.
First update `TestGroup.threadv` before the thread is started.
parent f2335127
...@@ -493,56 +493,57 @@ class TestGroup(object): ...@@ -493,56 +493,57 @@ class TestGroup(object):
- a test should indicate failure by call to .fail() - a test should indicate failure by call to .fail()
""" """
def __init__(tg, testcase): def __init__(self, testcase):
tg.testcase = testcase self.testcase = testcase
tg.failed = threading.Event() self.failed = threading.Event()
tg.fail_mu = threading.Lock() self.fail_mu = threading.Lock()
tg.failv = [None] # failures registerd by .fail self.failv = [] # failures registerd by .fail
tg.threadv = [] # spawned threads self.threadv = [] # spawned threads
tg.waitg = WaitGroup() # to wait for spawned threads self.waitg = WaitGroup() # to wait for spawned threads
def fail(tg, msg): def fail(self, msg):
"""fail adds failure to test result.""" """fail adds failure to test result."""
with tg.fail_mu: with self.fail_mu:
tg.failv.append(msg) self.failv.append(msg)
tg.failed.set() self.failed.set()
def go(tg, f, *argv, **kw): def go(self, f, *argv, **kw):
"""go spawns f(tg, #thread, *argv, **kw) in new test thread.""" """go spawns f(self, #thread, *argv, **kw) in new test thread."""
tg.waitg.add(1) self.waitg.add(1)
tx = len(tg.threadv) tx = len(self.threadv)
tname = kw.pop('name', 'T%d' % tx) tname = kw.pop('name', 'T%d' % tx)
t = Daemon(name=tname, target=tg._run, args=(f, tx, argv, kw)) t = Daemon(name=tname, target=self._run, args=(f, tx, argv, kw))
self.threadv.append(t)
t.start() t.start()
tg.threadv.append(t)
def _run(tg, f, tx, argv, kw): def _run(self, f, tx, argv, kw):
try: try:
f(tg, tx, *argv, **kw) f(self, tx, *argv, **kw)
except Exception as e: except Exception as e:
tg.fail("Unhandled exception %r" % (e,)) self.fail("Unhandled exception %r in thread %s"
% (e, self.threadv[tx].name))
raise raise
finally: finally:
tg.waitg.done() self.waitg.done()
def wait(tg, timeout): def wait(self, timeout):
"""wait waits for all test threads to complete and reports all """wait waits for all test threads to complete and reports all
collected failures to containing testcase.""" collected failures to containing testcase."""
if not tg.waitg.wait(timeout): if not self.waitg.wait(timeout):
tg.fail("test did not finish within %s seconds" % timeout) self.fail("test did not finish within %s seconds" % timeout)
failed_to_finish = [] failed_to_finish = []
for t in tg.threadv: for t in self.threadv:
try: try:
t.join(1) t.join(1)
except AssertionError: except AssertionError:
tg.failed.set() self.failed.set()
failed_to_finish.append(t.name) failed_to_finish.append(t.name)
if failed_to_finish: if failed_to_finish:
tg.fail("threads did not finish: %s" % failed_to_finish) self.fail("threads did not finish: %s" % failed_to_finish)
if tg.failed.is_set(): if self.failed.is_set():
tg.testcase.fail('\n\n'.join([_ for _ in tg.failv if _])) self.testcase.fail('\n\n'.join([_ for _ in self.failv if _]))
class Daemon(threading.Thread): class Daemon(threading.Thread):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment