|
|
|
@ -11,7 +11,7 @@ |
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
|
|
# See the License for the specific language governing permissions and |
|
|
|
|
# limitations under the License. |
|
|
|
|
"""Tests behavior of the try connect API on client side.""" |
|
|
|
|
"""Tests behavior of the wait for connection API on client side.""" |
|
|
|
|
|
|
|
|
|
import asyncio |
|
|
|
|
import logging |
|
|
|
@ -26,9 +26,9 @@ from tests_aio.unit._test_base import AioTestBase |
|
|
|
|
from tests_aio.unit._test_server import start_test_server |
|
|
|
|
from tests_aio.unit import _common |
|
|
|
|
from src.proto.grpc.testing import messages_pb2, test_pb2_grpc |
|
|
|
|
from tests_aio.unit._constants import UNREACHABLE_TARGET |
|
|
|
|
|
|
|
|
|
_REQUEST = b'\x01\x02\x03' |
|
|
|
|
_UNREACHABLE_TARGET = '0.1:1111' |
|
|
|
|
_TEST_METHOD = '/test/Test' |
|
|
|
|
|
|
|
|
|
_NUM_STREAM_RESPONSES = 5 |
|
|
|
@ -36,13 +36,13 @@ _REQUEST_PAYLOAD_SIZE = 7 |
|
|
|
|
_RESPONSE_PAYLOAD_SIZE = 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestTryConnect(AioTestBase): |
|
|
|
|
"""Tests if try connect raises connectivity issue.""" |
|
|
|
|
class TestWaitForConnection(AioTestBase): |
|
|
|
|
"""Tests if wait_for_connection raises connectivity issue.""" |
|
|
|
|
|
|
|
|
|
async def setUp(self): |
|
|
|
|
address, self._server = await start_test_server() |
|
|
|
|
self._channel = aio.insecure_channel(address) |
|
|
|
|
self._dummy_channel = aio.insecure_channel(_UNREACHABLE_TARGET) |
|
|
|
|
self._dummy_channel = aio.insecure_channel(UNREACHABLE_TARGET) |
|
|
|
|
self._stub = test_pb2_grpc.TestServiceStub(self._channel) |
|
|
|
|
|
|
|
|
|
async def tearDown(self): |
|
|
|
@ -50,6 +50,15 @@ class TestTryConnect(AioTestBase): |
|
|
|
|
await self._channel.close() |
|
|
|
|
await self._server.stop(None) |
|
|
|
|
|
|
|
|
|
async def test_unary_unary_ok(self): |
|
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
|
|
|
|
|
|
# No exception raised and no message swallowed. |
|
|
|
|
await call.wait_for_connection() |
|
|
|
|
|
|
|
|
|
response = await call |
|
|
|
|
self.assertIsInstance(response, messages_pb2.SimpleResponse) |
|
|
|
|
|
|
|
|
|
async def test_unary_stream_ok(self): |
|
|
|
|
request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
|
for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
@ -59,7 +68,7 @@ class TestTryConnect(AioTestBase): |
|
|
|
|
call = self._stub.StreamingOutputCall(request) |
|
|
|
|
|
|
|
|
|
# No exception raised and no message swallowed. |
|
|
|
|
await call.try_connect() |
|
|
|
|
await call.wait_for_connection() |
|
|
|
|
|
|
|
|
|
response_cnt = 0 |
|
|
|
|
async for response in call: |
|
|
|
@ -75,7 +84,7 @@ class TestTryConnect(AioTestBase): |
|
|
|
|
call = self._stub.StreamingInputCall() |
|
|
|
|
|
|
|
|
|
# No exception raised and no message swallowed. |
|
|
|
|
await call.try_connect() |
|
|
|
|
await call.wait_for_connection() |
|
|
|
|
|
|
|
|
|
payload = messages_pb2.Payload(body=b'\0' * _REQUEST_PAYLOAD_SIZE) |
|
|
|
|
request = messages_pb2.StreamingInputCallRequest(payload=payload) |
|
|
|
@ -95,7 +104,7 @@ class TestTryConnect(AioTestBase): |
|
|
|
|
call = self._stub.FullDuplexCall() |
|
|
|
|
|
|
|
|
|
# No exception raised and no message swallowed. |
|
|
|
|
await call.try_connect() |
|
|
|
|
await call.wait_for_connection() |
|
|
|
|
|
|
|
|
|
request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
|
request.response_parameters.append( |
|
|
|
@ -112,11 +121,19 @@ class TestTryConnect(AioTestBase): |
|
|
|
|
|
|
|
|
|
self.assertEqual(grpc.StatusCode.OK, await call.code()) |
|
|
|
|
|
|
|
|
|
async def test_unary_unary_error(self): |
|
|
|
|
call = self._dummy_channel.unary_unary(_TEST_METHOD)(_REQUEST) |
|
|
|
|
|
|
|
|
|
with self.assertRaises(aio.AioRpcError) as exception_context: |
|
|
|
|
await call.wait_for_connection() |
|
|
|
|
rpc_error = exception_context.exception |
|
|
|
|
self.assertEqual(grpc.StatusCode.UNAVAILABLE, rpc_error.code()) |
|
|
|
|
|
|
|
|
|
async def test_unary_stream_error(self): |
|
|
|
|
call = self._dummy_channel.unary_stream(_TEST_METHOD)(_REQUEST) |
|
|
|
|
|
|
|
|
|
with self.assertRaises(aio.AioRpcError) as exception_context: |
|
|
|
|
await call.try_connect() |
|
|
|
|
await call.wait_for_connection() |
|
|
|
|
rpc_error = exception_context.exception |
|
|
|
|
self.assertEqual(grpc.StatusCode.UNAVAILABLE, rpc_error.code()) |
|
|
|
|
|
|
|
|
@ -124,7 +141,7 @@ class TestTryConnect(AioTestBase): |
|
|
|
|
call = self._dummy_channel.stream_unary(_TEST_METHOD)() |
|
|
|
|
|
|
|
|
|
with self.assertRaises(aio.AioRpcError) as exception_context: |
|
|
|
|
await call.try_connect() |
|
|
|
|
await call.wait_for_connection() |
|
|
|
|
rpc_error = exception_context.exception |
|
|
|
|
self.assertEqual(grpc.StatusCode.UNAVAILABLE, rpc_error.code()) |
|
|
|
|
|
|
|
|
@ -132,7 +149,7 @@ class TestTryConnect(AioTestBase): |
|
|
|
|
call = self._dummy_channel.stream_stream(_TEST_METHOD)() |
|
|
|
|
|
|
|
|
|
with self.assertRaises(aio.AioRpcError) as exception_context: |
|
|
|
|
await call.try_connect() |
|
|
|
|
await call.wait_for_connection() |
|
|
|
|
rpc_error = exception_context.exception |
|
|
|
|
self.assertEqual(grpc.StatusCode.UNAVAILABLE, rpc_error.code()) |
|
|
|
|
|