~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_thread.py

  • Committer: Vincent Ladeuil
  • Date: 2011-02-10 11:49:48 UTC
  • mto: This revision was merged to the branch mainline in revision 5661.
  • Revision ID: v.ladeuil+lp@free.fr-20110210114948-hktri36s9tk0kdj0
Add CatchingExceptionThread.set_and_switch() to avoid race conditions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
        tt.start()
44
44
        self.assertRaises(MyException, tt.join)
45
45
 
46
 
    def test_join_when_no_exception(self):
 
46
    def test_join_around_exception(self):
47
47
        resume = threading.Event()
48
48
        class MyException(Exception):
49
49
            pass
61
61
        resume.set()
62
62
        self.assertRaises(MyException, tt.join)
63
63
 
64
 
 
 
64
    def test_sync_event(self):
 
65
        control = threading.Event()
 
66
        in_thread = threading.Event()
 
67
        class MyException(Exception):
 
68
            pass
 
69
 
 
70
        def raise_my_exception():
 
71
            # Wait for the test to tell us to resume
 
72
            control.wait()
 
73
            # Now we can raise
 
74
            raise MyException()
 
75
 
 
76
        tt = thread.CatchingExceptionThread(target=raise_my_exception,
 
77
                                            sync_event=in_thread)
 
78
        tt.start()
 
79
        tt.join(timeout=0)
 
80
        self.assertIs(None, tt.exception)
 
81
        self.assertIs(in_thread, tt.sync_event)
 
82
        control.set()
 
83
        self.assertRaises(MyException, tt.join)
 
84
        self.assertEquals(True, tt.sync_event.isSet())
 
85
 
 
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()
 
91
 
 
92
        class TestThread(thread.CatchingExceptionThread):
 
93
 
 
94
            def __init__(self, *args, **kwargs):
 
95
                super(TestThread, self).__init__(*args,
 
96
                                                 target=self.step_by_step,
 
97
                                                 **kwargs)
 
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()
 
103
 
 
104
            def step_by_step(self):
 
105
                control1.wait()
 
106
                self.current_step = 'step1'
 
107
                self.set_and_switch(self.step2)
 
108
                control2.wait()
 
109
                self.current_step = 'step2'
 
110
                self.set_and_switch(self.final)
 
111
                control3.wait()
 
112
                self.current_step = 'done'
 
113
 
 
114
        tt = TestThread()
 
115
        tt.start()
 
116
        self.assertEquals('starting', tt.current_step)
 
117
        control1.set()
 
118
        tt.step1.wait()
 
119
        self.assertEquals('step1', tt.current_step)
 
120
        control2.set()
 
121
        tt.step2.wait()
 
122
        self.assertEquals('step2', tt.current_step)
 
123
        control3.set()
 
124
        # We don't wait on tt.final
 
125
        tt.join()
 
126
        self.assertEquals('done', tt.current_step)
 
127
 
 
128
    def test_exception_while_set_and_switch(self):
 
129
        control1 = threading.Event()
 
130
 
 
131
        class MyException(Exception):
 
132
            pass
 
133
 
 
134
        class TestThread(thread.CatchingExceptionThread):
 
135
 
 
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,
 
142
                                                 **kwargs)
 
143
                self.current_step = 'starting'
 
144
                self.set_sync_event(self.step1)
 
145
 
 
146
            def step_by_step(self):
 
147
                control1.wait()
 
148
                self.current_step = 'step1'
 
149
                self.set_and_switch(self.step2)
 
150
 
 
151
            def set_sync_event(self, event):
 
152
                # We force an exception while trying to set step2
 
153
                if event is self.step2:
 
154
                    raise MyException()
 
155
                super(TestThread, self).set_sync_event(event)
 
156
 
 
157
        tt = TestThread()
 
158
        tt.start()
 
159
        self.assertEquals('starting', tt.current_step)
 
160
        control1.set()
 
161
        # We now wait on step1 which will be set when catching the exception
 
162
        tt.step1.wait()
 
163
        self.assertRaises(MyException, tt.pending_exception)
 
164
        self.assertIs(tt.step1, tt.sync_event)
 
165
        self.assertTrue(tt.step1.isSet())