| 1 |
# Copyright 2016 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 logging |
| 16 |
import unittest |
| 17 |
|
| 18 |
import grpc |
| 19 |
|
| 20 |
from tests.unit import test_common |
| 21 |
from tests.unit.framework.common import test_constants |
| 22 |
|
| 23 |
_REQUEST = b'' |
| 24 |
_RESPONSE = b'' |
| 25 |
|
| 26 |
_UNARY_UNARY = '/test/UnaryUnary' |
| 27 |
_UNARY_STREAM = '/test/UnaryStream' |
| 28 |
_STREAM_UNARY = '/test/StreamUnary' |
| 29 |
_STREAM_STREAM = '/test/StreamStream' |
| 30 |
|
| 31 |
|
| 32 |
def handle_unary_unary(request, servicer_context): |
| 33 |
return _RESPONSE |
| 34 |
|
| 35 |
|
| 36 |
def handle_unary_stream(request, servicer_context): |
| 37 |
for _ in range(test_constants.STREAM_LENGTH): |
| 38 |
yield _RESPONSE |
| 39 |
|
| 40 |
|
| 41 |
def handle_stream_unary(request_iterator, servicer_context): |
| 42 |
for request in request_iterator: |
| 43 |
pass |
| 44 |
return _RESPONSE |
| 45 |
|
| 46 |
|
| 47 |
def handle_stream_stream(request_iterator, servicer_context): |
| 48 |
for request in request_iterator: |
| 49 |
yield _RESPONSE |
| 50 |
|
| 51 |
|
| 52 |
class _MethodHandler(grpc.RpcMethodHandler): |
| 53 |
|
| 54 |
def __init__(self, request_streaming, response_streaming): |
| 55 |
self.request_streaming = request_streaming |
| 56 |
self.response_streaming = response_streaming |
| 57 |
self.request_deserializer = None |
| 58 |
self.response_serializer = None |
| 59 |
self.unary_unary = None |
| 60 |
self.unary_stream = None |
| 61 |
self.stream_unary = None |
| 62 |
self.stream_stream = None |
| 63 |
if self.request_streaming and self.response_streaming: |
| 64 |
self.stream_stream = handle_stream_stream |
| 65 |
elif self.request_streaming: |
| 66 |
self.stream_unary = handle_stream_unary |
| 67 |
elif self.response_streaming: |
| 68 |
self.unary_stream = handle_unary_stream |
| 69 |
else: |
| 70 |
self.unary_unary = handle_unary_unary |
| 71 |
|
| 72 |
|
| 73 |
class _GenericHandler(grpc.GenericRpcHandler): |
| 74 |
|
| 75 |
def service(self, handler_call_details): |
| 76 |
if handler_call_details.method == _UNARY_UNARY: |
| 77 |
return _MethodHandler(False, False) |
| 78 |
elif handler_call_details.method == _UNARY_STREAM: |
| 79 |
return _MethodHandler(False, True) |
| 80 |
elif handler_call_details.method == _STREAM_UNARY: |
| 81 |
return _MethodHandler(True, False) |
| 82 |
elif handler_call_details.method == _STREAM_STREAM: |
| 83 |
return _MethodHandler(True, True) |
| 84 |
else: |
| 85 |
return None |
| 86 |
|
| 87 |
|
| 88 |
class EmptyMessageTest(unittest.TestCase): |
| 89 |
|
| 90 |
def setUp(self): |
| 91 |
self._server = test_common.test_server() |
| 92 |
self._server.add_generic_rpc_handlers((_GenericHandler(),)) |
| 93 |
port = self._server.add_insecure_port('[::]:0') |
| 94 |
self._server.start() |
| 95 |
self._channel = grpc.insecure_channel('localhost:%d' % port) |
| 96 |
|
| 97 |
def tearDown(self): |
| 98 |
self._server.stop(0) |
| 99 |
self._channel.close() |
| 100 |
|
| 101 |
def testUnaryUnary(self): |
| 102 |
response = self._channel.unary_unary(_UNARY_UNARY)(_REQUEST) |
| 103 |
self.assertEqual(_RESPONSE, response) |
| 104 |
|
| 105 |
def testUnaryStream(self): |
| 106 |
response_iterator = self._channel.unary_stream(_UNARY_STREAM)(_REQUEST) |
| 107 |
self.assertSequenceEqual([_RESPONSE] * test_constants.STREAM_LENGTH, |
| 108 |
list(response_iterator)) |
| 109 |
|
| 110 |
def testStreamUnary(self): |
| 111 |
response = self._channel.stream_unary(_STREAM_UNARY)(iter( |
| 112 |
[_REQUEST] * test_constants.STREAM_LENGTH)) |
| 113 |
self.assertEqual(_RESPONSE, response) |
| 114 |
|
| 115 |
def testStreamStream(self): |
| 116 |
response_iterator = self._channel.stream_stream(_STREAM_STREAM)(iter( |
| 117 |
[_REQUEST] * test_constants.STREAM_LENGTH)) |
| 118 |
self.assertSequenceEqual([_RESPONSE] * test_constants.STREAM_LENGTH, |
| 119 |
list(response_iterator)) |
| 120 |
|
| 121 |
|
| 122 |
if __name__ == '__main__': |
| 123 |
logging.basicConfig() |
| 124 |
unittest.main(verbosity=2) |