55
55
raise MyException()
57
tt = thread.CatchingExceptionThread(target=raise_my_exception)
57
tt = cethread.CatchingExceptionThread(target=raise_my_exception)
60
60
self.assertIs(None, tt.exception)
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 = cethread.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_switch_and_set(self):
87
"""Caller can precisely control a thread."""
88
control1 = threading.Event()
89
control2 = threading.Event()
90
control3 = threading.Event()
92
class TestThread(cethread.CatchingExceptionThread):
95
super(TestThread, self).__init__(target=self.step_by_step)
96
self.current_step = 'starting'
97
self.step1 = threading.Event()
98
self.set_sync_event(self.step1)
99
self.step2 = threading.Event()
100
self.final = threading.Event()
102
def step_by_step(self):
104
self.current_step = 'step1'
105
self.switch_and_set(self.step2)
107
self.current_step = 'step2'
108
self.switch_and_set(self.final)
110
self.current_step = 'done'
114
self.assertEquals('starting', tt.current_step)
117
self.assertEquals('step1', tt.current_step)
120
self.assertEquals('step2', tt.current_step)
122
# We don't wait on tt.final
124
self.assertEquals('done', tt.current_step)
126
def test_exception_while_switch_and_set(self):
127
control1 = threading.Event()
129
class MyException(Exception):
132
class TestThread(cethread.CatchingExceptionThread):
134
def __init__(self, *args, **kwargs):
135
self.step1 = threading.Event()
136
self.step2 = threading.Event()
137
super(TestThread, self).__init__(target=self.step_by_step,
138
sync_event=self.step1)
139
self.current_step = 'starting'
140
self.set_sync_event(self.step1)
142
def step_by_step(self):
144
self.current_step = 'step1'
145
self.switch_and_set(self.step2)
147
def set_sync_event(self, event):
148
# We force an exception while trying to set step2
149
if event is self.step2:
151
super(TestThread, self).set_sync_event(event)
155
self.assertEquals('starting', tt.current_step)
157
# We now wait on step1 which will be set when catching the exception
159
self.assertRaises(MyException, tt.pending_exception)
160
self.assertIs(tt.step1, tt.sync_event)
161
self.assertTrue(tt.step1.isSet())