Debian Codegraph @

src/python/grpcio_tests/tests/unit/_rpc_part_1_test.py

raw
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 """Test of RPCs made against gRPC Python's application-layer API."""
15
16 from concurrent import futures
17 import itertools
18 import logging
19 import threading
20 import unittest
21
22 import grpc
23 from grpc.framework.foundation import logging_pool
24
25 from tests.unit._rpc_test_helpers import BaseRPCTest
26 from tests.unit._rpc_test_helpers import Callback
27 from tests.unit._rpc_test_helpers import TIMEOUT_SHORT
28 from tests.unit._rpc_test_helpers import \
29 stream_stream_non_blocking_multi_callable
30 from tests.unit._rpc_test_helpers import \
31 unary_stream_non_blocking_multi_callable
32 from tests.unit._rpc_test_helpers import stream_stream_multi_callable
33 from tests.unit._rpc_test_helpers import stream_unary_multi_callable
34 from tests.unit._rpc_test_helpers import unary_stream_multi_callable
35 from tests.unit._rpc_test_helpers import unary_unary_multi_callable
36 from tests.unit.framework.common import test_constants
37
38
39 class RPCPart1Test(BaseRPCTest, unittest.TestCase):
40
41 def testExpiredStreamRequestBlockingUnaryResponse(self):
42 requests = tuple(
43 b'\x07\x08' for _ in range(test_constants.STREAM_LENGTH))
44 request_iterator = iter(requests)
45
46 multi_callable = stream_unary_multi_callable(self._channel)
47 with self._control.pause():
48 with self.assertRaises(grpc.RpcError) as exception_context:
49 multi_callable(
50 request_iterator,
51 timeout=TIMEOUT_SHORT,
52 metadata=(('test',
53 'ExpiredStreamRequestBlockingUnaryResponse'),))
54
55 self.assertIsInstance(exception_context.exception, grpc.RpcError)
56 self.assertIsInstance(exception_context.exception, grpc.Call)
57 self.assertIsNotNone(exception_context.exception.initial_metadata())
58 self.assertIs(grpc.StatusCode.DEADLINE_EXCEEDED,
59 exception_context.exception.code())
60 self.assertIsNotNone(exception_context.exception.details())
61 self.assertIsNotNone(exception_context.exception.trailing_metadata())
62
63 def testExpiredStreamRequestFutureUnaryResponse(self):
64 requests = tuple(
65 b'\x07\x18' for _ in range(test_constants.STREAM_LENGTH))
66 request_iterator = iter(requests)
67 callback = Callback()
68
69 multi_callable = stream_unary_multi_callable(self._channel)
70 with self._control.pause():
71 response_future = multi_callable.future(
72 request_iterator,
73 timeout=TIMEOUT_SHORT,
74 metadata=(('test', 'ExpiredStreamRequestFutureUnaryResponse'),))
75 with self.assertRaises(grpc.FutureTimeoutError):
76 response_future.result(timeout=TIMEOUT_SHORT / 2.0)
77 response_future.add_done_callback(callback)
78 value_passed_to_callback = callback.value()
79
80 with self.assertRaises(grpc.RpcError) as exception_context:
81 response_future.result()
82 self.assertIs(grpc.StatusCode.DEADLINE_EXCEEDED, response_future.code())
83 self.assertIs(grpc.StatusCode.DEADLINE_EXCEEDED,
84 exception_context.exception.code())
85 self.assertIsInstance(response_future.exception(), grpc.RpcError)
86 self.assertIsNotNone(response_future.traceback())
87 self.assertIs(response_future, value_passed_to_callback)
88 self.assertIsNotNone(response_future.initial_metadata())
89 self.assertIs(grpc.StatusCode.DEADLINE_EXCEEDED, response_future.code())
90 self.assertIsNotNone(response_future.details())
91 self.assertIsNotNone(response_future.trailing_metadata())
92
93 def testExpiredStreamRequestStreamResponse(self):
94 self._expired_stream_request_stream_response(
95 stream_stream_multi_callable(self._channel))
96
97 def testExpiredStreamRequestStreamResponseNonBlocking(self):
98 self._expired_stream_request_stream_response(
99 stream_stream_non_blocking_multi_callable(self._channel))
100
101 def testFailedUnaryRequestBlockingUnaryResponse(self):
102 request = b'\x37\x17'
103
104 multi_callable = unary_unary_multi_callable(self._channel)
105 with self._control.fail():
106 with self.assertRaises(grpc.RpcError) as exception_context:
107 multi_callable.with_call(
108 request,
109 metadata=(('test',
110 'FailedUnaryRequestBlockingUnaryResponse'),))
111
112 self.assertIs(grpc.StatusCode.UNKNOWN,
113 exception_context.exception.code())
114 # sanity checks on to make sure returned string contains default members
115 # of the error
116 debug_error_string = exception_context.exception.debug_error_string()
117 self.assertIn('grpc_status', debug_error_string)
118 self.assertIn('grpc_message', debug_error_string)
119
120 def testFailedUnaryRequestFutureUnaryResponse(self):
121 request = b'\x37\x17'
122 callback = Callback()
123
124 multi_callable = unary_unary_multi_callable(self._channel)
125 with self._control.fail():
126 response_future = multi_callable.future(
127 request,
128 metadata=(('test', 'FailedUnaryRequestFutureUnaryResponse'),))
129 response_future.add_done_callback(callback)
130 value_passed_to_callback = callback.value()
131
132 self.assertIsInstance(response_future, grpc.Future)
133 self.assertIsInstance(response_future, grpc.Call)
134 with self.assertRaises(grpc.RpcError) as exception_context:
135 response_future.result()
136 self.assertIs(grpc.StatusCode.UNKNOWN,
137 exception_context.exception.code())
138 self.assertIsInstance(response_future.exception(), grpc.RpcError)
139 self.assertIsNotNone(response_future.traceback())
140 self.assertIs(grpc.StatusCode.UNKNOWN,
141 response_future.exception().code())
142 self.assertIs(response_future, value_passed_to_callback)
143
144 def testFailedUnaryRequestStreamResponse(self):
145 self._failed_unary_request_stream_response(
146 unary_stream_multi_callable(self._channel))
147
148 def testFailedUnaryRequestStreamResponseNonBlocking(self):
149 self._failed_unary_request_stream_response(
150 unary_stream_non_blocking_multi_callable(self._channel))
151
152 def testFailedStreamRequestBlockingUnaryResponse(self):
153 requests = tuple(
154 b'\x47\x58' for _ in range(test_constants.STREAM_LENGTH))
155 request_iterator = iter(requests)
156
157 multi_callable = stream_unary_multi_callable(self._channel)
158 with self._control.fail():
159 with self.assertRaises(grpc.RpcError) as exception_context:
160 multi_callable(
161 request_iterator,
162 metadata=(('test',
163 'FailedStreamRequestBlockingUnaryResponse'),))
164
165 self.assertIs(grpc.StatusCode.UNKNOWN,
166 exception_context.exception.code())
167
168 def testFailedStreamRequestFutureUnaryResponse(self):
169 requests = tuple(
170 b'\x07\x18' for _ in range(test_constants.STREAM_LENGTH))
171 request_iterator = iter(requests)
172 callback = Callback()
173
174 multi_callable = stream_unary_multi_callable(self._channel)
175 with self._control.fail():
176 response_future = multi_callable.future(
177 request_iterator,
178 metadata=(('test', 'FailedStreamRequestFutureUnaryResponse'),))
179 response_future.add_done_callback(callback)
180 value_passed_to_callback = callback.value()
181
182 with self.assertRaises(grpc.RpcError) as exception_context:
183 response_future.result()
184 self.assertIs(grpc.StatusCode.UNKNOWN, response_future.code())
185 self.assertIs(grpc.StatusCode.UNKNOWN,
186 exception_context.exception.code())
187 self.assertIsInstance(response_future.exception(), grpc.RpcError)
188 self.assertIsNotNone(response_future.traceback())
189 self.assertIs(response_future, value_passed_to_callback)
190
191 def testFailedStreamRequestStreamResponse(self):
192 self._failed_stream_request_stream_response(
193 stream_stream_multi_callable(self._channel))
194
195 def testFailedStreamRequestStreamResponseNonBlocking(self):
196 self._failed_stream_request_stream_response(
197 stream_stream_non_blocking_multi_callable(self._channel))
198
199 def testIgnoredUnaryRequestFutureUnaryResponse(self):
200 request = b'\x37\x17'
201
202 multi_callable = unary_unary_multi_callable(self._channel)
203 multi_callable.future(
204 request,
205 metadata=(('test', 'IgnoredUnaryRequestFutureUnaryResponse'),))
206
207 def testIgnoredUnaryRequestStreamResponse(self):
208 self._ignored_unary_stream_request_future_unary_response(
209 unary_stream_multi_callable(self._channel))
210
211 def testIgnoredUnaryRequestStreamResponseNonBlocking(self):
212 self._ignored_unary_stream_request_future_unary_response(
213 unary_stream_non_blocking_multi_callable(self._channel))
214
215 def testIgnoredStreamRequestFutureUnaryResponse(self):
216 requests = tuple(
217 b'\x07\x18' for _ in range(test_constants.STREAM_LENGTH))
218 request_iterator = iter(requests)
219
220 multi_callable = stream_unary_multi_callable(self._channel)
221 multi_callable.future(
222 request_iterator,
223 metadata=(('test', 'IgnoredStreamRequestFutureUnaryResponse'),))
224
225 def testIgnoredStreamRequestStreamResponse(self):
226 self._ignored_stream_request_stream_response(
227 stream_stream_multi_callable(self._channel))
228
229 def testIgnoredStreamRequestStreamResponseNonBlocking(self):
230 self._ignored_stream_request_stream_response(
231 stream_stream_non_blocking_multi_callable(self._channel))
232
233
234 if __name__ == '__main__':
235 logging.basicConfig()
236 unittest.main(verbosity=3)