Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS 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 | import threading |
| 6 | import unittest |
| 7 | |
| 8 | import stress |
| 9 | |
| 10 | |
| 11 | class StopThreadForTesting(Exception): |
| 12 | pass |
| 13 | |
| 14 | |
| 15 | class StressorTest(unittest.TestCase): |
| 16 | |
| 17 | def testEscalateExceptions(self): |
| 18 | def stress_event(): |
| 19 | raise StopThreadForTesting |
| 20 | |
| 21 | stressor = stress.CountedStressor(stress_event) |
| 22 | stressor.start(1) |
| 23 | self.assertRaises(StopThreadForTesting, stressor.wait) |
| 24 | |
| 25 | |
| 26 | def testDontEscalateExceptions(self): |
| 27 | event = threading.Event() |
| 28 | def stress_event(): |
| 29 | event.set() |
| 30 | raise StopThreadForTesting |
| 31 | |
| 32 | stressor = stress.CountedStressor(stress_event, |
| 33 | escalate_exceptions=False) |
| 34 | stressor.start(1) |
| 35 | stressor.wait() |
| 36 | self.assertTrue(event.is_set(), 'The stress event did not run') |
| 37 | |
| 38 | |
| 39 | def testCountedStressorStartCondition(self): |
| 40 | event = threading.Event() |
| 41 | |
| 42 | def start_condition(): |
| 43 | if event.is_set(): |
| 44 | return True |
| 45 | event.set() |
| 46 | return False |
| 47 | |
| 48 | def stress_event(): |
| 49 | raise StopThreadForTesting |
| 50 | |
| 51 | stressor = stress.CountedStressor(stress_event) |
| 52 | stressor.start(1, start_condition=start_condition) |
| 53 | self.assertRaises(StopThreadForTesting, stressor.wait) |
| 54 | self.assertTrue(event.is_set(), |
| 55 | 'Stress event ran despite a False start condition') |
| 56 | |
| 57 | |
| 58 | def testControlledStressorStartCondition(self): |
| 59 | start_event = threading.Event() |
| 60 | ran_event = threading.Event() |
| 61 | |
| 62 | def start_condition(): |
| 63 | if start_event.is_set(): |
| 64 | return True |
| 65 | start_event.set() |
| 66 | return False |
| 67 | |
| 68 | def stress_event(): |
| 69 | ran_event.set() |
| 70 | raise StopThreadForTesting |
| 71 | |
| 72 | stressor = stress.ControlledStressor(stress_event) |
| 73 | stressor.start(start_condition=start_condition) |
| 74 | ran_event.wait() |
| 75 | self.assertRaises(StopThreadForTesting, stressor.stop) |
| 76 | self.assertTrue(start_event.is_set(), |
| 77 | 'Stress event ran despite a False start condition') |
| 78 | |
| 79 | |
| 80 | def testCountedStressorIterations(self): |
| 81 | # This is a list to get around scoping rules in Python 2.x. See |
| 82 | # 'nonlocal' for the Python 3 remedy. |
| 83 | count = [0] |
| 84 | |
| 85 | def stress_event(): |
| 86 | count[0] += 1 |
| 87 | |
| 88 | stressor = stress.CountedStressor(stress_event) |
| 89 | stressor.start(10) |
| 90 | stressor.wait() |
| 91 | self.assertEqual(10, count[0], 'Stress event did not run the expected ' |
| 92 | 'number of iterations') |
| 93 | |
| 94 | |
| 95 | if __name__ == '__main__': |
| 96 | unittest.main() |