def test_wrap_stream_iterable_iterface(): response_iter = mock.create_autospec(grpc.Call, instance=True) callable_ = mock.Mock(spec=['__call__'], return_value=response_iter) wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) got_iterator = wrapped_callable() callable_.assert_called_once_with() # Check each aliased method in the grpc.Call interface got_iterator.add_callback(mock.sentinel.callback) response_iter.add_callback.assert_called_once_with(mock.sentinel.callback) got_iterator.cancel() response_iter.cancel.assert_called_once_with() got_iterator.code() response_iter.code.assert_called_once_with() got_iterator.details() response_iter.details.assert_called_once_with() got_iterator.initial_metadata() response_iter.initial_metadata.assert_called_once_with() got_iterator.is_active() response_iter.is_active.assert_called_once_with() got_iterator.time_remaining() response_iter.time_remaining.assert_called_once_with() got_iterator.trailing_metadata() response_iter.trailing_metadata.assert_called_once_with()
def test_wrap_stream_iterable_iterface(): response_iter = mock.create_autospec(grpc.Call, instance=True) callable_ = mock.Mock(spec=["__call__"], return_value=response_iter) wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) got_iterator = wrapped_callable() callable_.assert_called_once_with() # Check each aliased method in the grpc.Call interface got_iterator.add_callback(mock.sentinel.callback) response_iter.add_callback.assert_called_once_with(mock.sentinel.callback) got_iterator.cancel() response_iter.cancel.assert_called_once_with() got_iterator.code() response_iter.code.assert_called_once_with() got_iterator.details() response_iter.details.assert_called_once_with() got_iterator.initial_metadata() response_iter.initial_metadata.assert_called_once_with() got_iterator.is_active() response_iter.is_active.assert_called_once_with() got_iterator.time_remaining() response_iter.time_remaining.assert_called_once_with() got_iterator.trailing_metadata() response_iter.trailing_metadata.assert_called_once_with()
def test_wrap_stream_errors_invocation(): grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) callable_ = mock.Mock(spec=["__call__"], side_effect=grpc_error) wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) with pytest.raises(exceptions.InvalidArgument) as exc_info: wrapped_callable(1, 2, three="four") callable_.assert_called_once_with(1, 2, three="four") assert exc_info.value.response == grpc_error
def test_wrap_stream_errors_invocation(): grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) callable_ = mock.Mock(spec=['__call__'], side_effect=grpc_error) wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) with pytest.raises(exceptions.InvalidArgument) as exc_info: wrapped_callable(1, 2, three='four') callable_.assert_called_once_with(1, 2, three='four') assert exc_info.value.response == grpc_error
def test_wrap_stream_okay(): expected_responses = [1, 2, 3] callable_ = mock.Mock(spec=["__call__"], return_value=iter(expected_responses)) wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) got_iterator = wrapped_callable(1, 2, three="four") responses = list(got_iterator) callable_.assert_called_once_with(1, 2, three="four") assert responses == expected_responses
def test_wrap_stream_errors_iterator_initialization(): grpc_error = RpcErrorImpl(grpc.StatusCode.UNAVAILABLE) response_iter = RpcResponseIteratorImpl([grpc_error]) callable_ = mock.Mock(spec=["__call__"], return_value=response_iter) wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) with pytest.raises(exceptions.ServiceUnavailable) as exc_info: wrapped_callable(1, 2, three="four") callable_.assert_called_once_with(1, 2, three="four") assert exc_info.value.response == grpc_error
def test_wrap_stream_empty_iterator(): expected_responses = [] callable_ = mock.Mock(spec=["__call__"], return_value=iter(expected_responses)) wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) got_iterator = wrapped_callable() responses = list(got_iterator) callable_.assert_called_once_with() assert responses == expected_responses
def test_wrap_stream_prefetch_disabled(): responses = [1, 2, 3] iter_responses = iter(responses) callable_ = mock.Mock(spec=["__call__"], return_value=iter_responses) callable_._prefetch_first_result_ = False wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) wrapped_callable(1, 2, three="four") assert list( iter_responses) == responses # no items should have been pre-fetched callable_.assert_called_once_with(1, 2, three="four")
def test_wrap_stream_okay(): expected_responses = [1, 2, 3] callable_ = mock.Mock(spec=["__call__"], return_value=iter(expected_responses)) wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) got_iterator = wrapped_callable(1, 2, three="four") responses = list(got_iterator) callable_.assert_called_once_with(1, 2, three="four") assert responses == expected_responses
def test_wrap_stream_errors_iterator(): grpc_error = RpcErrorImpl(grpc.StatusCode.UNAVAILABLE) response_iter = RpcResponseIteratorImpl(grpc_error) callable_ = mock.Mock(spec=['__call__'], return_value=response_iter) wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) got_iterator = wrapped_callable(1, 2, three='four') with pytest.raises(exceptions.ServiceUnavailable) as exc_info: next(got_iterator) callable_.assert_called_once_with(1, 2, three='four') assert exc_info.value.response == grpc_error
def test_wrap_stream_errors_iterator(): grpc_error = RpcErrorImpl(grpc.StatusCode.UNAVAILABLE) response_iter = RpcResponseIteratorImpl(grpc_error) callable_ = mock.Mock(spec=['__call__'], return_value=response_iter) wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) got_iterator = wrapped_callable(1, 2, three='four') with pytest.raises(exceptions.ServiceUnavailable) as exc_info: next(got_iterator) callable_.assert_called_once_with(1, 2, three='four') assert exc_info.value.response == grpc_error