62
62
self.assertRaises(MyException, tt.join)
64
def test_sync_event(self):
65
control = threading.Event()
66
in_thread = threading.Event()
67
class MyException(Exception):
70
def raise_my_exception():
71
# Wait for the test to tell us to resume
76
tt = thread.CatchingExceptionThread(target=raise_my_exception,
80
self.assertIs(None, tt.exception)
81
self.assertIs(in_thread, tt.sync_event)
83
self.assertRaises(MyException, tt.join)
84
self.assertEquals(True, tt.sync_event.isSet())
86
def test_set_and_switch(self):
87
"""Caller can precisely control a thread."""
88
control1 = threading.Event()
89
control2 = threading.Event()
90
control3 = threading.Event()
92
class TestThread(thread.CatchingExceptionThread):
94
def __init__(self, *args, **kwargs):
95
super(TestThread, self).__init__(*args,
96
target=self.step_by_step,
98
self.current_step = 'starting'
99
self.step1 = threading.Event()
100
self.set_sync_event(self.step1)
101
self.step2 = threading.Event()
102
self.final = threading.Event()
104
def step_by_step(self):
106
self.current_step = 'step1'
107
self.set_and_switch(self.step2)
109
self.current_step = 'step2'
110
self.set_and_switch(self.final)
112
self.current_step = 'done'
116
self.assertEquals('starting', tt.current_step)
119
self.assertEquals('step1', tt.current_step)
122
self.assertEquals('step2', tt.current_step)
124
# We don't wait on tt.final
126
self.assertEquals('done', tt.current_step)
128
def test_exception_while_set_and_switch(self):
129
control1 = threading.Event()
131
class MyException(Exception):
134
class TestThread(thread.CatchingExceptionThread):
136
def __init__(self, *args, **kwargs):
137
self.step1 = threading.Event()
138
self.step2 = threading.Event()
139
super(TestThread, self).__init__(*args,
140
target=self.step_by_step,
141
sync_event=self.step1,
143
self.current_step = 'starting'
144
self.set_sync_event(self.step1)
146
def step_by_step(self):
148
self.current_step = 'step1'
149
self.set_and_switch(self.step2)
151
def set_sync_event(self, event):
152
# We force an exception while trying to set step2
153
if event is self.step2:
155
super(TestThread, self).set_sync_event(event)
159
self.assertEquals('starting', tt.current_step)
161
# We now wait on step1 which will be set when catching the exception
163
self.assertRaises(MyException, tt.pending_exception)
164
self.assertIs(tt.step1, tt.sync_event)
165
self.assertTrue(tt.step1.isSet())