xref: /aosp_15_r20/external/grpc-grpc/src/python/grpcio_tests/tests/unit/framework/common/test_control.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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
16import abc
17import contextlib
18import threading
19
20
21class 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
29class NestedDefect(Exception):
30    """Simulates a nested programming defect raised into in a system under test."""
31
32    def __str__(self):
33        raise Exception("Nested Exception")
34
35
36class Control(abc.ABC):
37    """An object that accepts program control from a system under test.
38
39    Systems under test passed a Control should call its control() method
40    frequently during execution. The control() method may block, raise an
41    exception, or do nothing, all according to the enclosing test's desire for
42    the system under test to simulate freezing, failing, or functioning.
43    """
44
45    @abc.abstractmethod
46    def control(self):
47        """Potentially does anything."""
48        raise NotImplementedError()
49
50
51class PauseFailControl(Control):
52    """A Control that can be used to pause or fail code under control.
53
54    This object is only safe for use from two threads: one of the system under
55    test calling control and the other from the test system calling pause,
56    block_until_paused, and fail.
57    """
58
59    def __init__(self):
60        self._condition = threading.Condition()
61        self._pause = False
62        self._paused = False
63        self._fail = False
64
65    def control(self):
66        with self._condition:
67            if self._fail:
68                raise Defect()
69
70            while self._pause:
71                self._paused = True
72                self._condition.notify_all()
73                self._condition.wait()
74            self._paused = False
75
76    @contextlib.contextmanager
77    def pause(self):
78        """Pauses code under control while controlling code is in context."""
79        with self._condition:
80            self._pause = True
81        yield
82        with self._condition:
83            self._pause = False
84            self._condition.notify_all()
85
86    def block_until_paused(self):
87        """Blocks controlling code until code under control is paused.
88
89        May only be called within the context of a pause call.
90        """
91        with self._condition:
92            while not self._paused:
93                self._condition.wait()
94
95    @contextlib.contextmanager
96    def fail(self):
97        """Fails code under control while controlling code is in context."""
98        with self._condition:
99            self._fail = True
100        yield
101        with self._condition:
102            self._fail = False
103