tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
 
import threading
import unittest
 
import stress
 
 
class StopThreadForTesting(Exception):
    pass
 
 
class StressorTest(unittest.TestCase):
 
    def testEscalateExceptions(self):
        def stress_event():
            raise StopThreadForTesting
 
        stressor = stress.CountedStressor(stress_event)
        stressor.start(1)
        self.assertRaises(StopThreadForTesting, stressor.wait)
 
 
    def testDontEscalateExceptions(self):
        event = threading.Event()
        def stress_event():
            event.set()
            raise StopThreadForTesting
 
        stressor = stress.CountedStressor(stress_event,
                                          escalate_exceptions=False)
        stressor.start(1)
        stressor.wait()
        self.assertTrue(event.is_set(), 'The stress event did not run')
 
 
    def testOnExit(self):
        def stress_event():
            pass
 
        event = threading.Event()
        def on_exit():
            event.set()
 
        stressor = stress.CountedStressor(stress_event, on_exit=on_exit)
        stressor.start(1)
        stressor.wait()
        self.assertTrue(event.is_set())
 
 
    def testOnExitWithException(self):
        def stress_event():
            raise StopThreadForTesting
 
        event = threading.Event()
        def on_exit():
            event.set()
 
        stressor = stress.CountedStressor(stress_event, on_exit=on_exit)
        stressor.start(1)
        self.assertRaises(StopThreadForTesting, stressor.wait)
        self.assertTrue(event.is_set())
 
 
    def testCountedStressorStartCondition(self):
        event = threading.Event()
 
        def start_condition():
            if event.is_set():
                return True
            event.set()
            return False
 
        def stress_event():
            raise StopThreadForTesting
 
        stressor = stress.CountedStressor(stress_event)
        stressor.start(1, start_condition=start_condition)
        self.assertRaises(StopThreadForTesting, stressor.wait)
        self.assertTrue(event.is_set(),
                        'Stress event ran despite a False start condition')
 
 
    def testControlledStressorStartCondition(self):
        start_event = threading.Event()
        ran_event = threading.Event()
 
        def start_condition():
            if start_event.is_set():
                return True
            start_event.set()
            return False
 
        def stress_event():
            ran_event.set()
            raise StopThreadForTesting
 
        stressor = stress.ControlledStressor(stress_event)
        stressor.start(start_condition=start_condition)
        ran_event.wait()
        self.assertRaises(StopThreadForTesting, stressor.stop)
        self.assertTrue(start_event.is_set(),
                        'Stress event ran despite a False start condition')
 
 
    def testCountedStressorIterations(self):
        # This is a list to get around scoping rules in Python 2.x. See
        # 'nonlocal' for the Python 3 remedy.
        count = [0]
 
        def stress_event():
            count[0] += 1
 
        stressor = stress.CountedStressor(stress_event)
        stressor.start(10)
        stressor.wait()
        self.assertEqual(10, count[0], 'Stress event did not run the expected '
                                       'number of iterations')
 
 
if __name__ == '__main__':
    unittest.main()