14 """Test of responsiveness to signals."""
31 if sys.executable
is not None:
32 _CLIENT_PATH = os.path.abspath(os.path.realpath(_signal_client.__file__))
35 if len(sys.argv) != 2:
36 raise RuntimeError(
"Must supply path to executable client.")
37 client_name = sys.argv[1].
split(
"/")[-1]
39 _CLIENT_PATH = os.path.realpath(
40 os.path.join(os.path.dirname(os.path.abspath(__file__)), client_name))
46 _GEVENT_ARG = (
"--gevent",)
if test_common.running_under_gevent()
else ()
73 """Blocks until a client connects to the server."""
77 """Handles a unary RPC.
79 Blocks until the client disconnects and then echoes.
81 stop_event = threading.Event()
87 servicer_context.add_callback(on_rpc_end)
93 """Handles a server streaming RPC.
95 Blocks until the client disconnects and then echoes.
97 stop_event = threading.Event()
103 servicer_context.add_callback(on_rpc_end)
109 if handler_call_details.method == _signal_client.UNARY_UNARY:
111 elif handler_call_details.method == _signal_client.UNARY_STREAM:
124 if sys.executable
is not None:
125 invocation = (sys.executable, _CLIENT_PATH) + tuple(args)
127 invocation = (_CLIENT_PATH,) + tuple(args)
128 return subprocess.Popen(invocation, stdout=stdout, stderr=stderr)
143 @unittest.skipIf(os.name ==
'nt',
'SIGINT not supported on windows')
145 """Tests that the server unary code path does not stall signal handlers."""
147 with tempfile.TemporaryFile(mode=
'r')
as client_stdout:
148 with tempfile.TemporaryFile(mode=
'r')
as client_stderr:
149 client =
_start_client((server_target,
'unary') + _GEVENT_ARG,
150 client_stdout, client_stderr)
151 self.
_handler.await_connected_client()
152 client.send_signal(signal.SIGINT)
153 self.assertFalse(client.wait(), msg=
_read_stream(client_stderr))
154 client_stdout.seek(0)
155 self.assertIn(_signal_client.SIGTERM_MESSAGE,
156 client_stdout.read())
158 @unittest.skipIf(os.name ==
'nt',
'SIGINT not supported on windows')
160 """Tests that the server streaming code path does not stall signal handlers."""
162 with tempfile.TemporaryFile(mode=
'r')
as client_stdout:
163 with tempfile.TemporaryFile(mode=
'r')
as client_stderr:
165 (server_target,
'streaming') + _GEVENT_ARG, client_stdout,
167 self.
_handler.await_connected_client()
168 client.send_signal(signal.SIGINT)
169 self.assertFalse(client.wait(), msg=
_read_stream(client_stderr))
170 client_stdout.seek(0)
171 self.assertIn(_signal_client.SIGTERM_MESSAGE,
172 client_stdout.read())
174 @unittest.skipIf(os.name ==
'nt',
'SIGINT not supported on windows')
177 with tempfile.TemporaryFile(mode=
'r')
as client_stdout:
178 with tempfile.TemporaryFile(mode=
'r')
as client_stderr:
180 (
'--exception', server_target,
'unary') + _GEVENT_ARG,
181 client_stdout, client_stderr)
182 self.
_handler.await_connected_client()
183 client.send_signal(signal.SIGINT)
185 self.assertEqual(0, client.returncode)
187 @unittest.skipIf(os.name ==
'nt',
'SIGINT not supported on windows')
190 with tempfile.TemporaryFile(mode=
'r')
as client_stdout:
191 with tempfile.TemporaryFile(mode=
'r')
as client_stderr:
193 (
'--exception', server_target,
'streaming') + _GEVENT_ARG,
194 client_stdout, client_stderr)
195 self.
_handler.await_connected_client()
196 client.send_signal(signal.SIGINT)
199 self.assertEqual(0, client.returncode)
202 if __name__ ==
'__main__':
203 logging.basicConfig()
204 unittest.main(verbosity=2)