|
| 1 | +from __future__ import print_function |
| 2 | +import Tkinter as tkinter |
| 3 | +import random |
| 4 | +import string |
| 5 | +import sys |
| 6 | +import threading |
| 7 | +import time |
| 8 | + |
| 9 | +import unittest |
| 10 | + |
| 11 | + |
| 12 | +class TestThreads(unittest.TestCase): |
| 13 | + def setUp(self): |
| 14 | + self.stderr = "" |
| 15 | + |
| 16 | + def test_threads(self): |
| 17 | + import subprocess |
| 18 | + p = subprocess.Popen([sys.executable, __file__, "run"], |
| 19 | + stdout=subprocess.PIPE, |
| 20 | + stderr=subprocess.STDOUT) |
| 21 | + |
| 22 | + rt = threading.Thread(target=reader_thread, args=(p.stdout, self.stderr)) |
| 23 | + rt.start() |
| 24 | + |
| 25 | + try: |
| 26 | + t = time.time() |
| 27 | + # Test code is designed to complete in a few seconds |
| 28 | + while time.time() - t < 10: |
| 29 | + if p.poll() is not None: break |
| 30 | + time.sleep(0.2) |
| 31 | + else: |
| 32 | + p.kill() |
| 33 | + setattr(self, 'killed', True) |
| 34 | + finally: |
| 35 | + p.stdout.close() |
| 36 | + rt.join() |
| 37 | + rc = p.returncode |
| 38 | + if hasattr(self, 'killed'): |
| 39 | + self.fail("Test code hang. Stderr: " + repr(self.stderr)) |
| 40 | + self.assertTrue(rc == 0, |
| 41 | + "Nonzero exit status: " + str(rc) + "; stderr:" + repr(self.stderr)) |
| 42 | + self.assertTrue(len(self.stderr) == 0, "stderr: " + repr(self.stderr)) |
| 43 | + |
| 44 | + |
| 45 | +def reader_thread(stream, output): |
| 46 | + while True: |
| 47 | + s = stream.read() |
| 48 | + if not s: break |
| 49 | + output += s |
| 50 | + |
| 51 | + |
| 52 | +running = True |
| 53 | + |
| 54 | + |
| 55 | +class EventThread(threading.Thread): |
| 56 | + def __init__(self, target): |
| 57 | + threading.Thread.__init__(self) |
| 58 | + self.target = target |
| 59 | + |
| 60 | + def run(self): |
| 61 | + while running: |
| 62 | + time.sleep(0.02) |
| 63 | + c = random.choice(string.ascii_letters) |
| 64 | + self.target.event_generate(c) |
| 65 | + |
| 66 | + |
| 67 | +class Main(object): |
| 68 | + def __init__(self): |
| 69 | + self.root = tkinter.Tk() |
| 70 | + self.root.bind('<Key>', dummy_handler) |
| 71 | + self.threads = [] |
| 72 | + |
| 73 | + self.t_cleanup = threading.Thread(target=self.tf_stop) |
| 74 | + |
| 75 | + def go(self): |
| 76 | + self.root.after(0, self.add_threads) |
| 77 | + self.root.after(500, self.stop) |
| 78 | + self.root.protocol("WM_DELETE_WINDOW", self.stop) |
| 79 | + self.root.mainloop() |
| 80 | + self.t_cleanup.join() |
| 81 | + |
| 82 | + def stop(self): |
| 83 | + self.t_cleanup.start() |
| 84 | + |
| 85 | + def tf_stop(self): |
| 86 | + global running |
| 87 | + running = False |
| 88 | + for t in self.threads: t.join() |
| 89 | + self.root.after(0, self.root.destroy) |
| 90 | + |
| 91 | + def add_threads(self): |
| 92 | + for _ in range(20): |
| 93 | + t = EventThread(self.root) |
| 94 | + self.threads.append(t) |
| 95 | + t.start() |
| 96 | + |
| 97 | + |
| 98 | +def dummy_handler(event): |
| 99 | + pass |
| 100 | + |
| 101 | + |
| 102 | +tests_gui = (TestThreads,) |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + import os |
| 106 | + |
| 107 | + if sys.argv[1:] == ['run']: |
| 108 | + if os.name == 'nt': |
| 109 | + import ctypes |
| 110 | + |
| 111 | + # the bug causes crashes |
| 112 | + ctypes.windll.kernel32.SetErrorMode(3) |
| 113 | + Main().go() |
| 114 | + else: |
| 115 | + from test.support import run_unittest |
| 116 | + |
| 117 | + run_unittest(*tests_gui) |
0 commit comments