Skip to content

[3.6] bpo-31234: Join threads in tests #3589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/test/test_concurrent_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ def notification():
t.start()

self.assertEqual(f1.result(timeout=5), 42)
t.join()

def test_result_with_cancel(self):
# TODO([email protected]): This test is timing dependent.
Expand All @@ -789,6 +790,7 @@ def notification():
t.start()

self.assertRaises(futures.CancelledError, f1.result, timeout=5)
t.join()

def test_exception_with_timeout(self):
self.assertRaises(futures.TimeoutError,
Expand Down Expand Up @@ -817,6 +819,7 @@ def notification():
t.start()

self.assertTrue(isinstance(f1.exception(timeout=5), OSError))
t.join()

@test.support.reap_threads
def test_main():
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,9 @@ def test_threading(self):
for sig in Signals[self.decimal]:
self.assertFalse(DefaultContext.flags[sig])

th1.join()
th2.join()

DefaultContext.prec = save_prec
DefaultContext.Emax = save_emax
DefaultContext.Emin = save_emin
Expand Down
26 changes: 13 additions & 13 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,28 +750,28 @@ def test_threaded_hashing(self):
hasher = hashlib.sha1()
num_threads = 5
smallest_data = b'swineflu'
data = smallest_data*200000
data = smallest_data * 200000
expected_hash = hashlib.sha1(data*num_threads).hexdigest()

def hash_in_chunks(chunk_size, event):
def hash_in_chunks(chunk_size):
index = 0
while index < len(data):
hasher.update(data[index:index+chunk_size])
hasher.update(data[index:index + chunk_size])
index += chunk_size
event.set()

events = []
threads = []
for threadnum in range(num_threads):
chunk_size = len(data) // (10**threadnum)
chunk_size = len(data) // (10 ** threadnum)
self.assertGreater(chunk_size, 0)
self.assertEqual(chunk_size % len(smallest_data), 0)
event = threading.Event()
events.append(event)
threading.Thread(target=hash_in_chunks,
args=(chunk_size, event)).start()

for event in events:
event.wait()
thread = threading.Thread(target=hash_in_chunks,
args=(chunk_size,))
threads.append(thread)

for thread in threads:
thread.start()
for thread in threads:
thread.join()

self.assertEqual(expected_hash, hasher.hexdigest())

Expand Down
46 changes: 25 additions & 21 deletions Lib/test/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,27 @@ def run(self):

class BlockingTestMixin:

def tearDown(self):
self.t = None

def do_blocking_test(self, block_func, block_args, trigger_func, trigger_args):
self.t = _TriggerThread(trigger_func, trigger_args)
self.t.start()
self.result = block_func(*block_args)
# If block_func returned before our thread made the call, we failed!
if not self.t.startedEvent.is_set():
self.fail("blocking function '%r' appeared not to block" %
block_func)
self.t.join(10) # make sure the thread terminates
if self.t.is_alive():
self.fail("trigger function '%r' appeared to not return" %
trigger_func)
return self.result
thread = _TriggerThread(trigger_func, trigger_args)
thread.start()
try:
self.result = block_func(*block_args)
# If block_func returned before our thread made the call, we failed!
if not thread.startedEvent.is_set():
self.fail("blocking function '%r' appeared not to block" %
block_func)
return self.result
finally:
thread.join(10) # make sure the thread terminates
if thread.is_alive():
self.fail("trigger function '%r' appeared to not return" %
trigger_func)

# Call this instead if block_func is supposed to raise an exception.
def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
trigger_args, expected_exception_class):
self.t = _TriggerThread(trigger_func, trigger_args)
self.t.start()
thread = _TriggerThread(trigger_func, trigger_args)
thread.start()
try:
try:
block_func(*block_args)
Expand All @@ -77,11 +76,11 @@ def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
self.fail("expected exception of kind %r" %
expected_exception_class)
finally:
self.t.join(10) # make sure the thread terminates
if self.t.is_alive():
thread.join(10) # make sure the thread terminates
if thread.is_alive():
self.fail("trigger function '%r' appeared to not return" %
trigger_func)
if not self.t.startedEvent.is_set():
if not thread.startedEvent.is_set():
self.fail("trigger thread ended but event never set")


Expand Down Expand Up @@ -159,8 +158,11 @@ def worker(self, q):

def queue_join_test(self, q):
self.cum = 0
threads = []
for i in (0,1):
threading.Thread(target=self.worker, args=(q,)).start()
thread = threading.Thread(target=self.worker, args=(q,))
thread.start()
threads.append(thread)
for i in range(100):
q.put(i)
q.join()
Expand All @@ -169,6 +171,8 @@ def queue_join_test(self, q):
for i in (0,1):
q.put(-1) # instruct the threads to close
q.join() # verify that you can join twice
for thread in threads:
thread.join()

def test_queue_task_done(self):
# Test to make sure a queue task completed successfully.
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,9 @@ def setUp(self):
self.sock.settimeout(15)
self.port = support.bind_port(self.sock)
servargs = (self.evt, self.respdata, self.sock)
threading.Thread(target=server, args=servargs).start()
thread = threading.Thread(target=server, args=servargs)
thread.start()
self.addCleanup(thread.join)
self.evt.wait()
self.evt.clear()

Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ def f():
self.assertFalse(t.is_alive())
# And verify the thread disposed of _tstate_lock.
self.assertIsNone(t._tstate_lock)
t.join()

def test_repr_stopped(self):
# Verify that "stopped" shows up in repr(Thread) appropriately.
Expand All @@ -601,6 +602,7 @@ def f():
break
time.sleep(0.01)
self.assertIn(LOOKING_FOR, repr(t)) # we waited at least 5 seconds
t.join()

def test_BoundedSemaphore_limit(self):
# BoundedSemaphore should raise ValueError if released too often.
Expand Down Expand Up @@ -915,6 +917,7 @@ def test_start_thread_again(self):
thread = threading.Thread()
thread.start()
self.assertRaises(RuntimeError, thread.start)
thread.join()

def test_joining_current_thread(self):
current_thread = threading.current_thread()
Expand All @@ -928,6 +931,7 @@ def test_daemonize_active_thread(self):
thread = threading.Thread()
thread.start()
self.assertRaises(RuntimeError, setattr, thread, "daemon", True)
thread.join()

def test_releasing_unacquired_lock(self):
lock = threading.Lock()
Expand Down Expand Up @@ -1090,6 +1094,8 @@ def test_init_immutable_default_args(self):
self.callback_event.wait()
self.assertEqual(len(self.callback_args), 2)
self.assertEqual(self.callback_args, [((), {}), ((), {})])
timer1.join()
timer2.join()

def _callback_spy(self, *args, **kwargs):
self.callback_args.append((args[:], kwargs.copy()))
Expand Down
8 changes: 6 additions & 2 deletions Lib/test/test_xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,9 @@ def setUp(self):
self.evt = threading.Event()
# start server thread to handle requests
serv_args = (self.evt, self.request_count, self.requestHandler)
threading.Thread(target=self.threadFunc, args=serv_args).start()
thread = threading.Thread(target=self.threadFunc, args=serv_args)
thread.start()
self.addCleanup(thread.join)

# wait for the server to be ready
self.evt.wait()
Expand Down Expand Up @@ -1211,7 +1213,9 @@ def setUp(self):
self.evt = threading.Event()
# start server thread to handle requests
serv_args = (self.evt, 1)
threading.Thread(target=http_server, args=serv_args).start()
thread = threading.Thread(target=http_server, args=serv_args)
thread.start()
self.addCleanup(thread.join)

# wait for the server to be ready
self.evt.wait()
Expand Down