Debian Codegraph @

src/python/grpcio_tests/tests/unit/_grpc_shutdown_test.py

raw
1 # Copyright 2019 The 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 """Tests the gRPC Core shutdown path."""
15
16 import datetime
17 import threading
18 import time
19 import unittest
20
21 import grpc
22
23 _TIMEOUT_FOR_SEGFAULT = datetime.timedelta(seconds=10)
24
25
26 class GrpcShutdownTest(unittest.TestCase):
27
28 def test_channel_close_with_connectivity_watcher(self):
29 """Originated by https://github.com/grpc/grpc/issues/20299.
30
31 The grpc_shutdown happens synchronously, but there might be Core object
32 references left in Cython which might lead to ABORT or SIGSEGV.
33 """
34 connection_failed = threading.Event()
35
36 def on_state_change(state):
37 if state in (grpc.ChannelConnectivity.TRANSIENT_FAILURE,
38 grpc.ChannelConnectivity.SHUTDOWN):
39 connection_failed.set()
40
41 # Connects to an void address, and subscribes state changes
42 channel = grpc.insecure_channel("0.1.1.1:12345")
43 channel.subscribe(on_state_change, True)
44
45 deadline = datetime.datetime.now() + _TIMEOUT_FOR_SEGFAULT
46
47 while datetime.datetime.now() < deadline:
48 time.sleep(0.1)
49 if connection_failed.is_set():
50 channel.close()
51
52
53 if __name__ == '__main__':
54 unittest.main(verbosity=2)