| OLD | NEW |
| 1 # Very rudimentary test of threading module | 1 # Very rudimentary test of threading module |
| 2 | 2 |
| 3 import test.test_support | 3 import test.test_support |
| 4 from test.test_support import verbose | 4 from test.test_support import verbose |
| 5 import random | 5 import random |
| 6 import re | 6 import re |
| 7 import sys | 7 import sys |
| 8 import threading | 8 import threading |
| 9 import thread | 9 import thread |
| 10 import time | 10 import time |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 self.assertRaises(RuntimeError, current_thread.join); | 429 self.assertRaises(RuntimeError, current_thread.join); |
| 430 | 430 |
| 431 def test_joining_inactive_thread(self): | 431 def test_joining_inactive_thread(self): |
| 432 thread = threading.Thread() | 432 thread = threading.Thread() |
| 433 self.assertRaises(RuntimeError, thread.join) | 433 self.assertRaises(RuntimeError, thread.join) |
| 434 | 434 |
| 435 def test_daemonize_active_thread(self): | 435 def test_daemonize_active_thread(self): |
| 436 thread = threading.Thread() | 436 thread = threading.Thread() |
| 437 thread.start() | 437 thread.start() |
| 438 self.assertRaises(RuntimeError, setattr, thread, "daemon", True) | 438 self.assertRaises(RuntimeError, setattr, thread, "daemon", True) |
| 439 |
| 440 |
| 441 # This test came from Ben Cottrell in http://bugs.python.org/issue1868 |
| 442 class TestThreadingLocal(unittest.TestCase): |
| 443 def setUp(self): |
| 444 self._failed = "No error message set or cleared." |
| 445 |
| 446 def _test_one_class(self, c): |
| 447 obj = c() |
| 448 e1 = threading.Event() |
| 449 e2 = threading.Event() |
| 450 |
| 451 def f1(): |
| 452 obj.x = 'foo' |
| 453 obj.y = 'bar' |
| 454 del obj.y |
| 455 e1.set() |
| 456 e2.wait() |
| 457 |
| 458 def f2(): |
| 459 try: |
| 460 foo = obj.x |
| 461 except AttributeError: |
| 462 # This is expected -- we haven't set obj.x in this thread yet! |
| 463 self._failed = "" # passed |
| 464 else: |
| 465 self._failed = ('Incorrectly got value %r from class %r\n' % |
| 466 (foo, c)) |
| 467 sys.stderr.write(self._failed) |
| 468 |
| 469 t1 = threading.Thread(target=f1) |
| 470 t1.start() |
| 471 e1.wait() |
| 472 t2 = threading.Thread(target=f2) |
| 473 t2.start() |
| 474 t2.join() |
| 475 |
| 476 # The test is done; just let t1 know it can exit, and wait for it. |
| 477 e2.set() |
| 478 t1.join() |
| 479 |
| 480 def test_threading_local(self): |
| 481 self._test_one_class(threading.local) |
| 482 self.assert_(not self._failed, self._failed) |
| 483 |
| 484 def test_threading_local_subclass(self): |
| 485 class LocalSubclass(threading.local): |
| 486 """To test that subclasses behave properly.""" |
| 487 self._test_one_class(LocalSubclass) |
| 488 self.assert_(not self._failed, self._failed) |
| 439 | 489 |
| 440 | 490 |
| 441 def test_main(): | 491 def test_main(): |
| 442 test.test_support.run_unittest(ThreadTests, | 492 test.test_support.run_unittest(ThreadTests, |
| 443 ThreadJoinOnShutdown, | 493 ThreadJoinOnShutdown, |
| 444 ThreadingExceptionTests, | 494 ThreadingExceptionTests, |
| 495 TestThreadingLocal, |
| 445 ) | 496 ) |
| 446 | 497 |
| 447 if __name__ == "__main__": | 498 if __name__ == "__main__": |
| 448 test_main() | 499 test_main() |
| OLD | NEW |