Commit 075050f8 authored by Antoine Pitrou's avatar Antoine Pitrou

test_threaded_import must clean up after itself

parent f26ad714
...@@ -11,8 +11,8 @@ import sys ...@@ -11,8 +11,8 @@ import sys
import time import time
import shutil import shutil
import unittest import unittest
from test.support import verbose, import_module, run_unittest, TESTFN from test.support import (
thread = import_module('_thread') verbose, import_module, run_unittest, TESTFN, reap_threads)
threading = import_module('threading') threading = import_module('threading')
def task(N, done, done_tasks, errors): def task(N, done, done_tasks, errors):
...@@ -62,7 +62,7 @@ class Finder: ...@@ -62,7 +62,7 @@ class Finder:
def __init__(self): def __init__(self):
self.numcalls = 0 self.numcalls = 0
self.x = 0 self.x = 0
self.lock = thread.allocate_lock() self.lock = threading.Lock()
def find_module(self, name, path=None): def find_module(self, name, path=None):
# Simulate some thread-unsafe behaviour. If calls to find_module() # Simulate some thread-unsafe behaviour. If calls to find_module()
...@@ -113,7 +113,9 @@ class ThreadedImportTests(unittest.TestCase): ...@@ -113,7 +113,9 @@ class ThreadedImportTests(unittest.TestCase):
done_tasks = [] done_tasks = []
done.clear() done.clear()
for i in range(N): for i in range(N):
thread.start_new_thread(task, (N, done, done_tasks, errors,)) t = threading.Thread(target=task,
args=(N, done, done_tasks, errors,))
t.start()
done.wait(60) done.wait(60)
self.assertFalse(errors) self.assertFalse(errors)
if verbose: if verbose:
...@@ -203,6 +205,7 @@ class ThreadedImportTests(unittest.TestCase): ...@@ -203,6 +205,7 @@ class ThreadedImportTests(unittest.TestCase):
self.assertEqual(set(results), {'a', 'b'}) self.assertEqual(set(results), {'a', 'b'})
@reap_threads
def test_main(): def test_main():
run_unittest(ThreadedImportTests) run_unittest(ThreadedImportTests)
......
...@@ -35,8 +35,12 @@ for name, func, args in [ ...@@ -35,8 +35,12 @@ for name, func, args in [
("os.path.abspath", os.path.abspath, ('.',)), ("os.path.abspath", os.path.abspath, ('.',)),
]: ]:
t = Worker(func, args) try:
t.start() t = Worker(func, args)
t.join(TIMEOUT) t.start()
if t.is_alive(): t.join(TIMEOUT)
errors.append("%s appeared to hang" % name) if t.is_alive():
errors.append("%s appeared to hang" % name)
finally:
del t
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