~bzr-pqm/bzr/bzr.dev

5652.1.1 by Vincent Ladeuil
Split ThreadWithException out of the tests hierarchy.
1
# Copyright (C) 2010, 2011 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 bzrlib import (
20
    tests,
21
    thread,
22
    )
23
24
25
class TestThreadWithException(tests.TestCase):
26
27
    def test_start_and_join_smoke_test(self):
28
        def do_nothing():
29
            pass
30
31
        tt = thread.ThreadWithException(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 = thread.ThreadWithException(target=raise_my_exception)
43
        tt.start()
44
        self.assertRaises(MyException, tt.join)
45
46
    def test_join_when_no_exception(self):
47
        resume = threading.Event()
48
        class MyException(Exception):
49
            pass
50
51
        def raise_my_exception():
52
            # Wait for the test to tell us to resume
53
            resume.wait()
54
            # Now we can raise
55
            raise MyException()
56
57
        tt = thread.ThreadWithException(target=raise_my_exception)
58
        tt.start()
59
        tt.join(timeout=0)
60
        self.assertIs(None, tt.exception)
61
        resume.set()
62
        self.assertRaises(MyException, tt.join)
63
64