Commit a0867aa0 authored by dieter's avatar dieter

add `TestGroup` tests; minor cleanup

parent b90d90ce
...@@ -200,7 +200,7 @@ class RaceTests(object): ...@@ -200,7 +200,7 @@ class RaceTests(object):
for i in range(N): for i in range(N):
# print('%s.%d' % (f.__name__, i)) # print('%s.%d' % (f.__name__, i))
f(tg) f(tg)
if tg.failed.is_set(): if tg.failed():
break break
# loop verify and modify concurrently. # loop verify and modify concurrently.
...@@ -308,7 +308,7 @@ class RaceTests(object): ...@@ -308,7 +308,7 @@ class RaceTests(object):
for i in range(N): for i in range(N):
# print('T%s.%d' % (tx, i)) # print('T%s.%d' % (tx, i))
t_() t_()
if tg.failed.is_set(): if tg.failed():
break break
finally: finally:
db.close() db.close()
...@@ -392,7 +392,7 @@ class RaceTests(object): ...@@ -392,7 +392,7 @@ class RaceTests(object):
db = self.dbopen() db = self.dbopen()
try: try:
for i in range(4): for i in range(4):
if tg.failed.is_set(): if tg.failed():
break break
work1(db) work1(db)
finally: finally:
...@@ -400,7 +400,7 @@ class RaceTests(object): ...@@ -400,7 +400,7 @@ class RaceTests(object):
for i in range(N): for i in range(N):
# print('T%s.%d' % (tx, i)) # print('T%s.%d' % (tx, i))
if tg.failed.is_set(): if tg.failed():
break break
t_() t_()
...@@ -490,12 +490,13 @@ class TestGroup(object): ...@@ -490,12 +490,13 @@ class TestGroup(object):
- .go() adds test thread to the group. - .go() adds test thread to the group.
- .wait() waits for all spawned threads to finish and reports all - .wait() waits for all spawned threads to finish and reports all
collected failures to containing testcase. collected failures to containing testcase.
- a test should indicate failure by call to .fail() - a test should indicate failure by call to .fail(), it
can check for a failure with .failed()
""" """
def __init__(self, testcase): def __init__(self, testcase):
self.testcase = testcase self.testcase = testcase
self.failed = threading.Event() self.failed_event = threading.Event()
self.fail_mu = threading.Lock() self.fail_mu = threading.Lock()
self.failv = [] # failures registerd by .fail self.failv = [] # failures registerd by .fail
self.threadv = [] # spawned threads self.threadv = [] # spawned threads
...@@ -505,7 +506,11 @@ class TestGroup(object): ...@@ -505,7 +506,11 @@ class TestGroup(object):
"""fail adds failure to test result.""" """fail adds failure to test result."""
with self.fail_mu: with self.fail_mu:
self.failv.append(msg) self.failv.append(msg)
self.failed.set() self.failed_event.set()
def failed(self):
"""did the thest already fail."""
return self.failed_event.is_set()
def go(self, f, *argv, **kw): def go(self, f, *argv, **kw):
"""go spawns f(self, #thread, *argv, **kw) in new test thread.""" """go spawns f(self, #thread, *argv, **kw) in new test thread."""
...@@ -517,11 +522,12 @@ class TestGroup(object): ...@@ -517,11 +522,12 @@ class TestGroup(object):
t.start() t.start()
def _run(self, f, tx, argv, kw): def _run(self, f, tx, argv, kw):
tname = self.threadv[tx].name
try: try:
f(self, tx, *argv, **kw) f(self, tx, *argv, **kw)
except Exception as e: except Exception as e:
self.fail("Unhandled exception %r in thread %s" self.fail("Unhandled exception %r in thread %s"
% (e, self.threadv[tx].name)) % (e, tname))
raise raise
finally: finally:
self.waitg.done() self.waitg.done()
...@@ -537,13 +543,14 @@ class TestGroup(object): ...@@ -537,13 +543,14 @@ class TestGroup(object):
try: try:
t.join(1) t.join(1)
except AssertionError: except AssertionError:
self.failed.set() self.failed_event.set()
failed_to_finish.append(t.name) failed_to_finish.append(t.name)
if failed_to_finish: if failed_to_finish:
self.fail("threads did not finish: %s" % failed_to_finish) self.fail("threads did not finish: %s" % failed_to_finish)
del self.threadv # avoid cyclic garbage
if self.failed.is_set(): if self.failed():
self.testcase.fail('\n\n'.join([_ for _ in self.failv if _])) self.testcase.fail('\n\n'.join(self.failv))
class Daemon(threading.Thread): class Daemon(threading.Thread):
...@@ -616,7 +623,7 @@ class WaitGroup(object): ...@@ -616,7 +623,7 @@ class WaitGroup(object):
if self.n < 0: if self.n < 0:
raise AssertionError("#workers is negative") raise AssertionError("#workers is negative")
if self.n == 0: if self.n == 0:
self.condition.notify() self.condition.notify_all()
def done(self): def done(self):
self.add(-1) self.add(-1)
......
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