Debian Codegraph @

bazel/_gevent_test_main.py

raw
1 # Copyright 2021 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
15 import gevent
16 from gevent import monkey
17
18 monkey.patch_all()
19 threadpool = gevent.hub.get_hub().threadpool
20
21 # Currently, each channel corresponds to a single native thread in the
22 # gevent threadpool. Thus, when the unit test suite spins up hundreds of
23 # channels concurrently, some will be starved out, causing the test to
24 # increase in duration. We increase the max size here so this does not
25 # happen.
26 threadpool.maxsize = 1024
27 threadpool.size = 32
28
29 import traceback, signal
30 from typing import Sequence
31
32
33 import grpc.experimental.gevent
34 grpc.experimental.gevent.init_gevent()
35
36 import gevent
37 import greenlet
38 import datetime
39
40 import grpc
41 import unittest
42 import sys
43 import os
44 import pkgutil
45
46 def trace_callback(event, args):
47 if event in ("switch", "throw"):
48 origin, target = args
49 sys.stderr.write("{} Transfer from {} to {} with {}\n".format(datetime.datetime.now(), origin, target, event))
50 else:
51 sys.stderr.write("Unknown event {}.\n".format(event))
52 sys.stderr.flush()
53
54 if os.getenv("GREENLET_TRACE") is not None:
55 greenlet.settrace(trace_callback)
56
57 def debug(sig, frame):
58 d={'_frame':frame}
59 d.update(frame.f_globals)
60 d.update(frame.f_locals)
61
62 sys.stderr.write("Traceback:\n{}".format("\n".join(traceback.format_stack(frame))))
63 import gevent.util; gevent.util.print_run_info()
64 sys.stderr.flush()
65
66 signal.signal(signal.SIGTERM, debug)
67
68
69 class SingleLoader(object):
70 def __init__(self, pattern: str):
71 loader = unittest.TestLoader()
72 self.suite = unittest.TestSuite()
73 tests = []
74 for importer, module_name, is_package in pkgutil.walk_packages([os.path.dirname(os.path.relpath(__file__))]):
75 if pattern in module_name:
76 module = importer.find_module(module_name).load_module(module_name)
77 tests.append(loader.loadTestsFromModule(module))
78 if len(tests) != 1:
79 raise AssertionError("Expected only 1 test module. Found {}".format(tests))
80 self.suite.addTest(tests[0])
81
82
83 def loadTestsFromNames(self, names: Sequence[str], module: str = None) -> unittest.TestSuite:
84 return self.suite
85
86 if __name__ == "__main__":
87
88 if len(sys.argv) != 2:
89 print(f"USAGE: {sys.argv[0]} TARGET_MODULE", file=sys.stderr)
90
91 target_module = sys.argv[1]
92
93 loader = SingleLoader(target_module)
94 runner = unittest.TextTestRunner()
95
96 result = gevent.spawn(runner.run, loader.suite)
97 result.join()
98 if not result.value.wasSuccessful():
99 sys.exit("Test failure.")