_cancellation_example_test.py
Go to the documentation of this file.
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 """Test for cancellation example."""
15 
16 import contextlib
17 import os
18 import signal
19 import socket
20 import subprocess
21 import unittest
22 
23 _BINARY_DIR = os.path.realpath(
24  os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
25 _SERVER_PATH = os.path.join(_BINARY_DIR, 'server')
26 _CLIENT_PATH = os.path.join(_BINARY_DIR, 'client')
27 
28 
29 @contextlib.contextmanager
30 def _get_port():
31  sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
32  sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
33  if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 0:
34  raise RuntimeError("Failed to set SO_REUSEPORT.")
35  sock.bind(('', 0))
36  try:
37  yield sock.getsockname()[1]
38  finally:
39  sock.close()
40 
41 
42 def _start_client(server_port,
43  desired_string,
44  ideal_distance,
45  interesting_distance=None):
46  interesting_distance_args = () if interesting_distance is None else (
47  '--show-inferior', interesting_distance)
48  return subprocess.Popen((_CLIENT_PATH, desired_string, '--server',
49  'localhost:{}'.format(server_port),
50  '--ideal-distance', str(ideal_distance)) +
51  interesting_distance_args)
52 
53 
54 class CancellationExampleTest(unittest.TestCase):
55 
57  with _get_port() as test_port:
58  server_process = subprocess.Popen(
59  (_SERVER_PATH, '--port', str(test_port)))
60  try:
61  client_process = _start_client(test_port, 'aa', 0)
62  client_return_code = client_process.wait()
63  self.assertEqual(0, client_return_code)
64  self.assertIsNone(server_process.poll())
65  finally:
66  server_process.kill()
67  server_process.wait()
68 
70  with _get_port() as test_port:
71  server_process = subprocess.Popen(
72  (_SERVER_PATH, '--port', str(test_port)))
73  try:
74  client_process1 = _start_client(test_port, 'aaaaaaaaaa', 0)
75  client_process1.send_signal(signal.SIGINT)
76  client_process1.wait()
77  client_process2 = _start_client(test_port, 'aa', 0)
78  client_return_code = client_process2.wait()
79  self.assertEqual(0, client_return_code)
80  self.assertIsNone(server_process.poll())
81  finally:
82  server_process.kill()
83  server_process.wait()
84 
85 
86 if __name__ == '__main__':
87  unittest.main(verbosity=2)
xds_interop_client.str
str
Definition: xds_interop_client.py:487
http2_test_server.format
format
Definition: http2_test_server.py:118
_cancellation_example_test.CancellationExampleTest.test_graceful_sigint
def test_graceful_sigint(self)
Definition: _cancellation_example_test.py:69
_cancellation_example_test._start_client
def _start_client(server_port, desired_string, ideal_distance, interesting_distance=None)
Definition: _cancellation_example_test.py:42
_cancellation_example_test.CancellationExampleTest.test_successful_run
def test_successful_run(self)
Definition: _cancellation_example_test.py:56
_cancellation_example_test.CancellationExampleTest
Definition: _cancellation_example_test.py:54
_cancellation_example_test._get_port
def _get_port()
Definition: _cancellation_example_test.py:30


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:38