_server_shutdown_scenarios.py
Go to the documentation of this file.
1 # Copyright 2018 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 """Defines a number of module-scope gRPC scenarios to test server shutdown."""
15 
16 import argparse
17 from concurrent import futures
18 import logging
19 import os
20 import threading
21 import time
22 
23 import grpc
24 from six.moves import queue
25 
26 from tests.unit import test_common
27 
28 WAIT_TIME = 1000
29 
30 REQUEST = b'request'
31 RESPONSE = b'response'
32 
33 SERVER_RAISES_EXCEPTION = 'server_raises_exception'
34 SERVER_DEALLOCATED = 'server_deallocated'
35 SERVER_FORK_CAN_EXIT = 'server_fork_can_exit'
36 
37 FORK_EXIT = '/test/ForkExit'
38 
39 
40 def fork_and_exit(request, servicer_context):
41  pid = os.fork()
42  if pid == 0:
43  os._exit(0)
44  return RESPONSE
45 
46 
48 
49  def service(self, handler_call_details):
50  if handler_call_details.method == FORK_EXIT:
51  return grpc.unary_unary_rpc_method_handler(fork_and_exit)
52  else:
53  return None
54 
55 
56 def run_server(port_queue):
57  server = test_common.test_server()
58  port = server.add_insecure_port('[::]:0')
59  port_queue.put(port)
60  server.add_generic_rpc_handlers((GenericHandler(),))
61  server.start()
62  # threading.Event.wait() does not exhibit the bug identified in
63  # https://github.com/grpc/grpc/issues/17093, sleep instead
64  time.sleep(WAIT_TIME)
65 
66 
67 def run_test(args):
68  if args.scenario == SERVER_RAISES_EXCEPTION:
69  server = test_common.test_server()
70  server.start()
71  raise Exception()
72  elif args.scenario == SERVER_DEALLOCATED:
73  server = test_common.test_server()
74  server.start()
75  server.__del__()
76  while server._state.stage != grpc._server._ServerStage.STOPPED:
77  pass
78  elif args.scenario == SERVER_FORK_CAN_EXIT:
79  port_queue = queue.Queue()
80  thread = threading.Thread(target=run_server, args=(port_queue,))
81  thread.daemon = True
82  thread.start()
83  port = port_queue.get()
84  channel = grpc.insecure_channel('localhost:%d' % port)
85  multi_callable = channel.unary_unary(FORK_EXIT)
86  result, call = multi_callable.with_call(REQUEST, wait_for_ready=True)
87  os.wait()
88  else:
89  raise ValueError('unknown test scenario')
90 
91 
92 if __name__ == '__main__':
93  logging.basicConfig()
94  parser = argparse.ArgumentParser()
95  parser.add_argument('scenario', type=str)
96  args = parser.parse_args()
97  run_test(args)
grpc.unary_unary_rpc_method_handler
def unary_unary_rpc_method_handler(behavior, request_deserializer=None, response_serializer=None)
Definition: src/python/grpcio/grpc/__init__.py:1510
grpc.insecure_channel
def insecure_channel(target, options=None, compression=None)
Definition: src/python/grpcio/grpc/__init__.py:1962
tests.unit._server_shutdown_scenarios.run_server
def run_server(port_queue)
Definition: _server_shutdown_scenarios.py:56
tests.unit._server_shutdown_scenarios.GenericHandler
Definition: _server_shutdown_scenarios.py:47
tests.unit._server_shutdown_scenarios.GenericHandler.service
def service(self, handler_call_details)
Definition: _server_shutdown_scenarios.py:49
grpc.GenericRpcHandler
Definition: src/python/grpcio/grpc/__init__.py:1333
tests.unit._server_shutdown_scenarios.run_test
def run_test(args)
Definition: _server_shutdown_scenarios.py:67
tests.unit
Definition: src/python/grpcio_tests/tests/unit/__init__.py:1
tests.unit._server_shutdown_scenarios.fork_and_exit
def fork_and_exit(request, servicer_context)
Definition: _server_shutdown_scenarios.py:40


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