-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: avoid checking instance on each stream call #529
Changes from all commits
b96da2d
91a9879
6aecfed
c58789b
8628306
7c85df0
d0138ab
d562351
6323b74
9acd796
0c4e106
11d28d2
33348c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,12 +97,40 @@ async def test_common_methods_in_wrapped_call(): | |
assert mock_call.wait_for_connection.call_count == 1 | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize( | ||
"callable_type,expected_wrapper_type", | ||
[ | ||
(grpc.aio.UnaryStreamMultiCallable, grpc_helpers_async._WrappedUnaryStreamCall), | ||
(grpc.aio.StreamUnaryMultiCallable, grpc_helpers_async._WrappedStreamUnaryCall), | ||
( | ||
grpc.aio.StreamStreamMultiCallable, | ||
grpc_helpers_async._WrappedStreamStreamCall, | ||
), | ||
], | ||
) | ||
async def test_wrap_errors_w_stream_type(callable_type, expected_wrapper_type): | ||
class ConcreteMulticallable(callable_type): | ||
def __call__(self, *args, **kwargs): | ||
raise NotImplementedError("Should not be called") | ||
|
||
with mock.patch.object( | ||
grpc_helpers_async, "_wrap_stream_errors" | ||
) as wrap_stream_errors: | ||
callable_ = ConcreteMulticallable() | ||
grpc_helpers_async.wrap_errors(callable_) | ||
assert wrap_stream_errors.call_count == 1 | ||
wrap_stream_errors.assert_called_once_with(callable_, expected_wrapper_type) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_wrap_stream_errors_unary_stream(): | ||
mock_call = mock.Mock(aio.UnaryStreamCall, autospec=True) | ||
multicallable = mock.Mock(return_value=mock_call) | ||
|
||
wrapped_callable = grpc_helpers_async._wrap_stream_errors(multicallable) | ||
wrapped_callable = grpc_helpers_async._wrap_stream_errors( | ||
multicallable, grpc_helpers_async._WrappedUnaryStreamCall | ||
) | ||
|
||
await wrapped_callable(1, 2, three="four") | ||
multicallable.assert_called_once_with(1, 2, three="four") | ||
|
@@ -114,7 +142,9 @@ async def test_wrap_stream_errors_stream_unary(): | |
mock_call = mock.Mock(aio.StreamUnaryCall, autospec=True) | ||
multicallable = mock.Mock(return_value=mock_call) | ||
|
||
wrapped_callable = grpc_helpers_async._wrap_stream_errors(multicallable) | ||
wrapped_callable = grpc_helpers_async._wrap_stream_errors( | ||
multicallable, grpc_helpers_async._WrappedStreamUnaryCall | ||
) | ||
|
||
await wrapped_callable(1, 2, three="four") | ||
multicallable.assert_called_once_with(1, 2, three="four") | ||
|
@@ -126,22 +156,26 @@ async def test_wrap_stream_errors_stream_stream(): | |
mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) | ||
multicallable = mock.Mock(return_value=mock_call) | ||
|
||
wrapped_callable = grpc_helpers_async._wrap_stream_errors(multicallable) | ||
wrapped_callable = grpc_helpers_async._wrap_stream_errors( | ||
multicallable, grpc_helpers_async._WrappedStreamStreamCall | ||
) | ||
|
||
await wrapped_callable(1, 2, three="four") | ||
multicallable.assert_called_once_with(1, 2, three="four") | ||
assert mock_call.wait_for_connection.call_count == 1 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_wrap_stream_errors_type_error(): | ||
async def test_wrap_errors_type_error(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be helpful to comment that the explicit type of error you're checking for is the "Unexpected type of callable" (which now happens at wrapping time). As per my other comment, if that were a specialized subclass of |
||
""" | ||
If wrap_errors is called with an unexpected type, it should raise a TypeError. | ||
""" | ||
mock_call = mock.Mock() | ||
multicallable = mock.Mock(return_value=mock_call) | ||
|
||
wrapped_callable = grpc_helpers_async._wrap_stream_errors(multicallable) | ||
|
||
with pytest.raises(TypeError): | ||
await wrapped_callable() | ||
with pytest.raises(TypeError) as exc: | ||
grpc_helpers_async.wrap_errors(multicallable) | ||
assert "Unexpected type" in str(exc.value) | ||
|
||
|
||
@pytest.mark.asyncio | ||
|
@@ -151,7 +185,9 @@ async def test_wrap_stream_errors_raised(): | |
mock_call.wait_for_connection = mock.AsyncMock(side_effect=[grpc_error]) | ||
multicallable = mock.Mock(return_value=mock_call) | ||
|
||
wrapped_callable = grpc_helpers_async._wrap_stream_errors(multicallable) | ||
wrapped_callable = grpc_helpers_async._wrap_stream_errors( | ||
multicallable, grpc_helpers_async._WrappedStreamStreamCall | ||
) | ||
|
||
with pytest.raises(exceptions.InvalidArgument): | ||
await wrapped_callable() | ||
|
@@ -166,7 +202,9 @@ async def test_wrap_stream_errors_read(): | |
mock_call.read = mock.AsyncMock(side_effect=grpc_error) | ||
multicallable = mock.Mock(return_value=mock_call) | ||
|
||
wrapped_callable = grpc_helpers_async._wrap_stream_errors(multicallable) | ||
wrapped_callable = grpc_helpers_async._wrap_stream_errors( | ||
multicallable, grpc_helpers_async._WrappedStreamStreamCall | ||
) | ||
|
||
wrapped_call = await wrapped_callable(1, 2, three="four") | ||
multicallable.assert_called_once_with(1, 2, three="four") | ||
|
@@ -189,7 +227,9 @@ async def test_wrap_stream_errors_aiter(): | |
mock_call.__aiter__ = mock.Mock(return_value=mocked_aiter) | ||
multicallable = mock.Mock(return_value=mock_call) | ||
|
||
wrapped_callable = grpc_helpers_async._wrap_stream_errors(multicallable) | ||
wrapped_callable = grpc_helpers_async._wrap_stream_errors( | ||
multicallable, grpc_helpers_async._WrappedStreamStreamCall | ||
) | ||
wrapped_call = await wrapped_callable() | ||
|
||
with pytest.raises(exceptions.InvalidArgument) as exc_info: | ||
|
@@ -210,7 +250,9 @@ async def test_wrap_stream_errors_aiter_non_rpc_error(): | |
mock_call.__aiter__ = mock.Mock(return_value=mocked_aiter) | ||
multicallable = mock.Mock(return_value=mock_call) | ||
|
||
wrapped_callable = grpc_helpers_async._wrap_stream_errors(multicallable) | ||
wrapped_callable = grpc_helpers_async._wrap_stream_errors( | ||
multicallable, grpc_helpers_async._WrappedStreamStreamCall | ||
) | ||
wrapped_call = await wrapped_callable() | ||
|
||
with pytest.raises(TypeError) as exc_info: | ||
|
@@ -224,7 +266,9 @@ async def test_wrap_stream_errors_aiter_called_multiple_times(): | |
mock_call = mock.Mock(aio.StreamStreamCall, autospec=True) | ||
multicallable = mock.Mock(return_value=mock_call) | ||
|
||
wrapped_callable = grpc_helpers_async._wrap_stream_errors(multicallable) | ||
wrapped_callable = grpc_helpers_async._wrap_stream_errors( | ||
multicallable, grpc_helpers_async._WrappedStreamStreamCall | ||
) | ||
wrapped_call = await wrapped_callable() | ||
|
||
assert wrapped_call.__aiter__() == wrapped_call.__aiter__() | ||
|
@@ -239,7 +283,9 @@ async def test_wrap_stream_errors_write(): | |
mock_call.done_writing = mock.AsyncMock(side_effect=[None, grpc_error]) | ||
multicallable = mock.Mock(return_value=mock_call) | ||
|
||
wrapped_callable = grpc_helpers_async._wrap_stream_errors(multicallable) | ||
wrapped_callable = grpc_helpers_async._wrap_stream_errors( | ||
multicallable, grpc_helpers_async._WrappedStreamStreamCall | ||
) | ||
|
||
wrapped_call = await wrapped_callable() | ||
|
||
|
@@ -295,7 +341,9 @@ def test_wrap_errors_streaming(wrap_stream_errors): | |
result = grpc_helpers_async.wrap_errors(callable_) | ||
|
||
assert result == wrap_stream_errors.return_value | ||
wrap_stream_errors.assert_called_once_with(callable_) | ||
wrap_stream_errors.assert_called_once_with( | ||
callable_, grpc_helpers_async._WrappedUnaryStreamCall | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit (not a blocker, consider for the future): consider in cases like this raising a custom subclass of
TypeError
, so that we can check the exception type in tests