14 """Client for testing responsiveness to signals."""
16 from __future__
import print_function
26 SIGTERM_MESSAGE =
"Handling sigterm!"
28 UNARY_UNARY =
"/test/Unary"
29 UNARY_STREAM =
"/test/ServerStreaming"
31 _MESSAGE = b
'\x00\x00\x00'
33 _ASSERTION_MESSAGE =
"Control flow should never reach here."
40 per_process_rpc_future =
None
44 print(SIGTERM_MESSAGE)
45 if per_process_rpc_future
is not None:
46 per_process_rpc_future.cancel()
53 """Initiate a unary RPC to be interrupted by a SIGINT."""
54 global per_process_rpc_future
56 multicallable = channel.unary_unary(UNARY_UNARY)
57 signal.signal(signal.SIGINT, handle_sigint)
58 per_process_rpc_future = multicallable.future(_MESSAGE,
60 result = per_process_rpc_future.result()
61 assert False, _ASSERTION_MESSAGE
65 """Initiate a streaming RPC to be interrupted by a SIGINT."""
66 global per_process_rpc_future
68 signal.signal(signal.SIGINT, handle_sigint)
69 per_process_rpc_future = channel.unary_stream(UNARY_STREAM)(
70 _MESSAGE, wait_for_ready=
True)
71 for result
in per_process_rpc_future:
73 assert False, _ASSERTION_MESSAGE
77 """Initiate a unary RPC with a signal handler that will raise."""
80 channel.unary_unary(UNARY_UNARY)(_MESSAGE, wait_for_ready=
True)
81 except KeyboardInterrupt:
82 sys.stderr.write(
"Running signal handler.\n")
90 """Initiate a streaming RPC with a signal handler that will raise."""
93 for _
in channel.unary_stream(UNARY_STREAM)(_MESSAGE,
96 except KeyboardInterrupt:
97 sys.stderr.write(
"Running signal handler.\n")
104 if __name__ ==
'__main__':
105 parser = argparse.ArgumentParser(description=
'Signal test client.')
106 parser.add_argument(
'server', help=
'Server target')
107 parser.add_argument(
'arity', help=
'Arity', choices=(
'unary',
'streaming'))
108 parser.add_argument(
'--exception',
109 help=
'Whether the signal throws an exception',
111 parser.add_argument(
'--gevent',
112 help=
'Whether to run under gevent.',
114 args = parser.parse_args()
116 from gevent
import monkey
124 if args.arity ==
'unary' and not args.exception:
126 elif args.arity ==
'streaming' and not args.exception:
128 elif args.arity ==
'unary' and args.exception: