OLD | NEW |
(Empty) | |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Unittests for reraiser_thread.py.""" |
| 6 |
| 7 import unittest |
| 8 |
| 9 import reraiser_thread |
| 10 |
| 11 |
| 12 class TestException(Exception): |
| 13 pass |
| 14 |
| 15 |
| 16 class TestReraiserThread(unittest.TestCase): |
| 17 """Tests for reraiser_thread.ReraiserThread.""" |
| 18 def testNominal(self): |
| 19 result = [None, None] |
| 20 |
| 21 def f(a, b=None): |
| 22 result[0] = a |
| 23 result[1] = b |
| 24 |
| 25 thread = reraiser_thread.ReraiserThread(f, [1], {'b': 2}) |
| 26 thread.start() |
| 27 thread.join() |
| 28 self.assertEqual(result[0], 1) |
| 29 self.assertEqual(result[1], 2) |
| 30 |
| 31 def testRaise(self): |
| 32 def f(): |
| 33 raise TestException |
| 34 |
| 35 thread = reraiser_thread.ReraiserThread(f) |
| 36 thread.start() |
| 37 thread.join() |
| 38 with self.assertRaises(TestException): |
| 39 thread.ReraiseIfException() |
| 40 |
| 41 |
| 42 class TestReraiserThreadGroup(unittest.TestCase): |
| 43 """Tests for reraiser_thread.ReraiserThreadGroup.""" |
| 44 def testInit(self): |
| 45 ran = [False] * 5 |
| 46 def f(i): |
| 47 ran[i] = True |
| 48 |
| 49 group = reraiser_thread.ReraiserThreadGroup( |
| 50 [reraiser_thread.ReraiserThread(f, args=[i]) for i in range(5)]) |
| 51 group.StartAll() |
| 52 group.JoinAll() |
| 53 for v in ran: |
| 54 self.assertTrue(v) |
| 55 |
| 56 def testAdd(self): |
| 57 ran = [False] * 5 |
| 58 def f(i): |
| 59 ran[i] = True |
| 60 |
| 61 group = reraiser_thread.ReraiserThreadGroup() |
| 62 for i in xrange(5): |
| 63 group.Add(reraiser_thread.ReraiserThread(f, args=[i])) |
| 64 group.StartAll() |
| 65 group.JoinAll() |
| 66 for v in ran: |
| 67 self.assertTrue(v) |
| 68 |
| 69 def testJoinRaise(self): |
| 70 def f(): |
| 71 raise TestException |
| 72 group = reraiser_thread.ReraiserThreadGroup( |
| 73 [reraiser_thread.ReraiserThread(f) for _ in xrange(5)]) |
| 74 group.StartAll() |
| 75 with self.assertRaises(TestException): |
| 76 group.JoinAll() |
| 77 |
| 78 |
| 79 if __name__ == '__main__': |
| 80 unittest.main() |
OLD | NEW |