Debian Codegraph @

src/python/grpcio_tests/tests/unit/framework/common/test_control.py

raw
1 # Copyright 2015 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Code for instructing systems under test to block or fail."""
15
16 import abc
17 import contextlib
18 import threading
19
20
21 class Defect(Exception):
22 """Simulates a programming defect raised into in a system under test.
23
24 Use of a standard exception type is too easily misconstrued as an actual
25 defect in either the test infrastructure or the system under test.
26 """
27
28
29 class Control(abc.ABC):
30 """An object that accepts program control from a system under test.
31
32 Systems under test passed a Control should call its control() method
33 frequently during execution. The control() method may block, raise an
34 exception, or do nothing, all according to the enclosing test's desire for
35 the system under test to simulate freezing, failing, or functioning.
36 """
37
38 @abc.abstractmethod
39 def control(self):
40 """Potentially does anything."""
41 raise NotImplementedError()
42
43
44 class PauseFailControl(Control):
45 """A Control that can be used to pause or fail code under control.
46
47 This object is only safe for use from two threads: one of the system under
48 test calling control and the other from the test system calling pause,
49 block_until_paused, and fail.
50 """
51
52 def __init__(self):
53 self._condition = threading.Condition()
54 self._pause = False
55 self._paused = False
56 self._fail = False
57
58 def control(self):
59 with self._condition:
60 if self._fail:
61 raise Defect()
62
63 while self._pause:
64 self._paused = True
65 self._condition.notify_all()
66 self._condition.wait()
67 self._paused = False
68
69 @contextlib.contextmanager
70 def pause(self):
71 """Pauses code under control while controlling code is in context."""
72 with self._condition:
73 self._pause = True
74 yield
75 with self._condition:
76 self._pause = False
77 self._condition.notify_all()
78
79 def block_until_paused(self):
80 """Blocks controlling code until code under control is paused.
81
82 May only be called within the context of a pause call.
83 """
84 with self._condition:
85 while not self._paused:
86 self._condition.wait()
87
88 @contextlib.contextmanager
89 def fail(self):
90 """Fails code under control while controlling code is in context."""
91 with self._condition:
92 self._fail = True
93 yield
94 with self._condition:
95 self._fail = False