/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_cethread.py

  • Committer: John Arbash Meinel
  • Date: 2006-04-25 15:05:42 UTC
  • mfrom: (1185.85.85 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060425150542-c7b518dca9928691
[merge] the old bzr-encoding changes, reparenting them on bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2011, 2016 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
import threading
18
 
 
19
 
from .. import (
20
 
    cethread,
21
 
    tests,
22
 
    )
23
 
 
24
 
 
25
 
class TestCatchingExceptionThread(tests.TestCase):
26
 
 
27
 
    def test_start_and_join_smoke_test(self):
28
 
        def do_nothing():
29
 
            pass
30
 
 
31
 
        tt = cethread.CatchingExceptionThread(target=do_nothing)
32
 
        tt.start()
33
 
        tt.join()
34
 
 
35
 
    def test_exception_is_re_raised(self):
36
 
        class MyException(Exception):
37
 
            pass
38
 
 
39
 
        def raise_my_exception():
40
 
            raise MyException()
41
 
 
42
 
        tt = cethread.CatchingExceptionThread(target=raise_my_exception)
43
 
        tt.start()
44
 
        self.assertRaises(MyException, tt.join)
45
 
 
46
 
    def test_join_around_exception(self):
47
 
        resume = threading.Event()
48
 
 
49
 
        class MyException(Exception):
50
 
            pass
51
 
 
52
 
        def raise_my_exception():
53
 
            # Wait for the test to tell us to resume
54
 
            resume.wait()
55
 
            # Now we can raise
56
 
            raise MyException()
57
 
 
58
 
        tt = cethread.CatchingExceptionThread(target=raise_my_exception)
59
 
        tt.start()
60
 
        tt.join(timeout=0)
61
 
        self.assertIs(None, tt.exception)
62
 
        resume.set()
63
 
        self.assertRaises(MyException, tt.join)
64
 
 
65
 
    def test_sync_event(self):
66
 
        control = threading.Event()
67
 
        in_thread = threading.Event()
68
 
 
69
 
        class MyException(Exception):
70
 
            pass
71
 
 
72
 
        def raise_my_exception():
73
 
            # Wait for the test to tell us to resume
74
 
            control.wait()
75
 
            # Now we can raise
76
 
            raise MyException()
77
 
 
78
 
        tt = cethread.CatchingExceptionThread(target=raise_my_exception,
79
 
                                              sync_event=in_thread)
80
 
        tt.start()
81
 
        tt.join(timeout=0)
82
 
        self.assertIs(None, tt.exception)
83
 
        self.assertIs(in_thread, tt.sync_event)
84
 
        control.set()
85
 
        self.assertRaises(MyException, tt.join)
86
 
        self.assertEqual(True, tt.sync_event.isSet())
87
 
 
88
 
    def test_switch_and_set(self):
89
 
        """Caller can precisely control a thread."""
90
 
        control1 = threading.Event()
91
 
        control2 = threading.Event()
92
 
        control3 = threading.Event()
93
 
 
94
 
        class TestThread(cethread.CatchingExceptionThread):
95
 
 
96
 
            def __init__(self):
97
 
                super(TestThread, self).__init__(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()
103
 
 
104
 
            def step_by_step(self):
105
 
                control1.wait()
106
 
                self.current_step = 'step1'
107
 
                self.switch_and_set(self.step2)
108
 
                control2.wait()
109
 
                self.current_step = 'step2'
110
 
                self.switch_and_set(self.final)
111
 
                control3.wait()
112
 
                self.current_step = 'done'
113
 
 
114
 
        tt = TestThread()
115
 
        tt.start()
116
 
        self.assertEqual('starting', tt.current_step)
117
 
        control1.set()
118
 
        tt.step1.wait()
119
 
        self.assertEqual('step1', tt.current_step)
120
 
        control2.set()
121
 
        tt.step2.wait()
122
 
        self.assertEqual('step2', tt.current_step)
123
 
        control3.set()
124
 
        # We don't wait on tt.final
125
 
        tt.join()
126
 
        self.assertEqual('done', tt.current_step)
127
 
 
128
 
    def test_exception_while_switch_and_set(self):
129
 
        control1 = threading.Event()
130
 
 
131
 
        class MyException(Exception):
132
 
            pass
133
 
 
134
 
        class TestThread(cethread.CatchingExceptionThread):
135
 
 
136
 
            def __init__(self, *args, **kwargs):
137
 
                self.step1 = threading.Event()
138
 
                self.step2 = threading.Event()
139
 
                super(TestThread, self).__init__(target=self.step_by_step,
140
 
                                                 sync_event=self.step1)
141
 
                self.current_step = 'starting'
142
 
                self.set_sync_event(self.step1)
143
 
 
144
 
            def step_by_step(self):
145
 
                control1.wait()
146
 
                self.current_step = 'step1'
147
 
                self.switch_and_set(self.step2)
148
 
 
149
 
            def set_sync_event(self, event):
150
 
                # We force an exception while trying to set step2
151
 
                if event is self.step2:
152
 
                    raise MyException()
153
 
                super(TestThread, self).set_sync_event(event)
154
 
 
155
 
        tt = TestThread()
156
 
        tt.start()
157
 
        self.assertEqual('starting', tt.current_step)
158
 
        control1.set()
159
 
        # We now wait on step1 which will be set when catching the exception
160
 
        tt.step1.wait()
161
 
        self.assertRaises(MyException, tt.pending_exception)
162
 
        self.assertIs(tt.step1, tt.sync_event)
163
 
        self.assertTrue(tt.step1.isSet())