|
| 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 | +"""Tests server and client side compression.""" |
| 15 | + |
| 16 | +import threading |
| 17 | +import time |
| 18 | +import unittest |
| 19 | + |
| 20 | +import grpc |
| 21 | + |
| 22 | +from tests.unit import test_common |
| 23 | +from tests.unit.framework.common import test_constants |
| 24 | + |
| 25 | +_BEAT = 0.5 |
| 26 | +_SOME_TIME = 5 |
| 27 | +_MORE_TIME = 10 |
| 28 | + |
| 29 | + |
| 30 | +class _MethodHandler(grpc.RpcMethodHandler): |
| 31 | + |
| 32 | + request_streaming = True |
| 33 | + response_streaming = True |
| 34 | + request_deserializer = None |
| 35 | + response_serializer = None |
| 36 | + |
| 37 | + def stream_stream(self, request_iterator, servicer_context): |
| 38 | + for request in request_iterator: |
| 39 | + yield request * 2 |
| 40 | + |
| 41 | + |
| 42 | +_METHOD_HANDLER = _MethodHandler() |
| 43 | + |
| 44 | + |
| 45 | +class _GenericHandler(grpc.GenericRpcHandler): |
| 46 | + |
| 47 | + def service(self, handler_call_details): |
| 48 | + return _METHOD_HANDLER |
| 49 | + |
| 50 | + |
| 51 | +_GENERIC_HANDLER = _GenericHandler() |
| 52 | + |
| 53 | + |
| 54 | +class _Pipe(object): |
| 55 | + |
| 56 | + def __init__(self, values): |
| 57 | + self._condition = threading.Condition() |
| 58 | + self._values = list(values) |
| 59 | + self._open = True |
| 60 | + |
| 61 | + def __iter__(self): |
| 62 | + return self |
| 63 | + |
| 64 | + def _next(self): |
| 65 | + with self._condition: |
| 66 | + while not self._values and self._open: |
| 67 | + self._condition.wait() |
| 68 | + if self._values: |
| 69 | + return self._values.pop(0) |
| 70 | + else: |
| 71 | + raise StopIteration() |
| 72 | + |
| 73 | + def next(self): |
| 74 | + return self._next() |
| 75 | + |
| 76 | + def __next__(self): |
| 77 | + return self._next() |
| 78 | + |
| 79 | + def add(self, value): |
| 80 | + with self._condition: |
| 81 | + self._values.append(value) |
| 82 | + self._condition.notify() |
| 83 | + |
| 84 | + def close(self): |
| 85 | + with self._condition: |
| 86 | + self._open = False |
| 87 | + self._condition.notify() |
| 88 | + |
| 89 | + def __enter__(self): |
| 90 | + return self |
| 91 | + |
| 92 | + def __exit__(self, type, value, traceback): |
| 93 | + self.close() |
| 94 | + |
| 95 | + |
| 96 | +class ChannelCloseTest(unittest.TestCase): |
| 97 | + |
| 98 | + def setUp(self): |
| 99 | + self._server = test_common.test_server( |
| 100 | + max_workers=test_constants.THREAD_CONCURRENCY) |
| 101 | + self._server.add_generic_rpc_handlers((_GENERIC_HANDLER,)) |
| 102 | + self._port = self._server.add_insecure_port('[::]:0') |
| 103 | + self._server.start() |
| 104 | + |
| 105 | + def tearDown(self): |
| 106 | + self._server.stop(None) |
| 107 | + |
| 108 | + def test_close_immediately_after_call_invocation(self): |
| 109 | + channel = grpc.insecure_channel('localhost:{}'.format(self._port)) |
| 110 | + multi_callable = channel.stream_stream('Meffod') |
| 111 | + request_iterator = _Pipe(()) |
| 112 | + response_iterator = multi_callable(request_iterator) |
| 113 | + channel.close() |
| 114 | + request_iterator.close() |
| 115 | + |
| 116 | + self.assertIs(response_iterator.code(), grpc.StatusCode.CANCELLED) |
| 117 | + |
| 118 | + def test_close_while_call_active(self): |
| 119 | + channel = grpc.insecure_channel('localhost:{}'.format(self._port)) |
| 120 | + multi_callable = channel.stream_stream('Meffod') |
| 121 | + request_iterator = _Pipe((b'abc',)) |
| 122 | + response_iterator = multi_callable(request_iterator) |
| 123 | + next(response_iterator) |
| 124 | + channel.close() |
| 125 | + request_iterator.close() |
| 126 | + |
| 127 | + self.assertIs(response_iterator.code(), grpc.StatusCode.CANCELLED) |
| 128 | + |
| 129 | + def test_context_manager_close_while_call_active(self): |
| 130 | + with grpc.insecure_channel('localhost:{}'.format( |
| 131 | + self._port)) as channel: # pylint: disable=bad-continuation |
| 132 | + multi_callable = channel.stream_stream('Meffod') |
| 133 | + request_iterator = _Pipe((b'abc',)) |
| 134 | + response_iterator = multi_callable(request_iterator) |
| 135 | + next(response_iterator) |
| 136 | + request_iterator.close() |
| 137 | + |
| 138 | + self.assertIs(response_iterator.code(), grpc.StatusCode.CANCELLED) |
| 139 | + |
| 140 | + def test_context_manager_close_while_many_calls_active(self): |
| 141 | + with grpc.insecure_channel('localhost:{}'.format( |
| 142 | + self._port)) as channel: # pylint: disable=bad-continuation |
| 143 | + multi_callable = channel.stream_stream('Meffod') |
| 144 | + request_iterators = tuple( |
| 145 | + _Pipe((b'abc',)) |
| 146 | + for _ in range(test_constants.THREAD_CONCURRENCY)) |
| 147 | + response_iterators = [] |
| 148 | + for request_iterator in request_iterators: |
| 149 | + response_iterator = multi_callable(request_iterator) |
| 150 | + next(response_iterator) |
| 151 | + response_iterators.append(response_iterator) |
| 152 | + for request_iterator in request_iterators: |
| 153 | + request_iterator.close() |
| 154 | + |
| 155 | + for response_iterator in response_iterators: |
| 156 | + self.assertIs(response_iterator.code(), grpc.StatusCode.CANCELLED) |
| 157 | + |
| 158 | + def test_many_concurrent_closes(self): |
| 159 | + channel = grpc.insecure_channel('localhost:{}'.format(self._port)) |
| 160 | + multi_callable = channel.stream_stream('Meffod') |
| 161 | + request_iterator = _Pipe((b'abc',)) |
| 162 | + response_iterator = multi_callable(request_iterator) |
| 163 | + next(response_iterator) |
| 164 | + start = time.time() |
| 165 | + end = start + _MORE_TIME |
| 166 | + |
| 167 | + def sleep_some_time_then_close(): |
| 168 | + time.sleep(_SOME_TIME) |
| 169 | + channel.close() |
| 170 | + |
| 171 | + for _ in range(test_constants.THREAD_CONCURRENCY): |
| 172 | + close_thread = threading.Thread(target=sleep_some_time_then_close) |
| 173 | + close_thread.start() |
| 174 | + while True: |
| 175 | + request_iterator.add(b'def') |
| 176 | + time.sleep(_BEAT) |
| 177 | + if end < time.time(): |
| 178 | + break |
| 179 | + request_iterator.close() |
| 180 | + |
| 181 | + self.assertIs(response_iterator.code(), grpc.StatusCode.CANCELLED) |
| 182 | + |
| 183 | + |
| 184 | +if __name__ == '__main__': |
| 185 | + unittest.main(verbosity=2) |
0 commit comments