Debian Codegraph @

src/python/grpcio_tests/tests_py3_only/unit/_leak_test.py

raw
1 # Copyright 2020 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 """A smoke test for memory leaks on short-lived channels without close.
15
16 This test doesn't guarantee all resources are cleaned if `Channel.close` is not
17 explicitly invoked. The recommended way of using Channel object is using `with`
18 clause, and let context manager automatically close the channel.
19 """
20
21 from concurrent.futures import ThreadPoolExecutor
22 import logging
23 import os
24 import resource
25 import sys
26 import unittest
27
28 import grpc
29
30 _TEST_METHOD = '/test/Test'
31 _REQUEST = b'\x23\x33'
32 _LARGE_NUM_OF_ITERATIONS = 5000
33
34 # If MAX_RSS inflated more than this size, the test is failed.
35 _FAIL_THRESHOLD = 25 * 1024 * 1024 # 25 MiB
36
37
38 def _get_max_rss():
39 return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
40
41
42 def _pretty_print_bytes(x):
43 if x > 1024 * 1024 * 1024:
44 return "%.2f GiB" % (x / 1024.0 / 1024 / 1024)
45 elif x > 1024 * 1024:
46 return "%.2f MiB" % (x / 1024.0 / 1024)
47 elif x > 1024:
48 return "%.2f KiB" % (x / 1024.0)
49 else:
50 return "%d B" % x
51
52
53 class _GenericHandler(grpc.GenericRpcHandler):
54
55 def service(self, handler_call_details):
56 if handler_call_details.method == _TEST_METHOD:
57 return grpc.unary_unary_rpc_method_handler(lambda x, _: x)
58
59
60 def _start_a_test_server():
61 server = grpc.server(ThreadPoolExecutor(max_workers=1),
62 options=(('grpc.so_reuseport', 0),))
63 server.add_generic_rpc_handlers((_GenericHandler(),))
64 port = server.add_insecure_port('localhost:0')
65 server.start()
66 return 'localhost:%d' % port, server
67
68
69 def _perform_an_rpc(address):
70 channel = grpc.insecure_channel(address)
71 multicallable = channel.unary_unary(_TEST_METHOD)
72 response = multicallable(_REQUEST)
73 assert _REQUEST == response
74
75
76 class TestLeak(unittest.TestCase):
77
78 def test_leak_with_single_shot_rpcs(self):
79 address, server = _start_a_test_server()
80
81 # Records memory before experiment.
82 before = _get_max_rss()
83
84 # Amplifies the leak.
85 for n in range(_LARGE_NUM_OF_ITERATIONS):
86 _perform_an_rpc(address)
87
88 # Fails the test if memory leak detected.
89 diff = _get_max_rss() - before
90 if diff > _FAIL_THRESHOLD:
91 self.fail("Max RSS inflated {} > {}".format(
92 _pretty_print_bytes(diff),
93 _pretty_print_bytes(_FAIL_THRESHOLD)))
94
95
96 if __name__ == "__main__":
97 logging.basicConfig(level=logging.DEBUG)
98 unittest.main(verbosity=2)