Skip to content
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

How to test grpc status codes with unittests #130

Closed
ozangungor12 opened this issue Apr 6, 2021 · 2 comments
Closed

How to test grpc status codes with unittests #130

ozangungor12 opened this issue Apr 6, 2021 · 2 comments

Comments

@ozangungor12
Copy link

Hello,

I am implementing custom exception handling with gRPC status codes in which I send specific status codes for specific exceptions such as INVALID_ARGUMENT or FAILED_PRECONDITION instead of general UNKNOWN status.

I managed to send the status codes I want by using await stream.send_trailing_metadata().

However, when I try to create a similar scenario in unit tests, I don't see that specific status code but STATUS_UNKNOWN error appears.

Is there a correct way to extract the status code from the metadata itself? This is what I am doing in my test:

servicer = MyServicer()
    async with ChannelFor([servicer]) as channel:
        stub = MyStub(channel)
        request = MyRequest()
        response = await stub.MyService([request])

Thanks for the help!

@vmagamedov
Copy link
Owner

vmagamedov commented Apr 14, 2021

Just wrote a test to check your issue:

@pytest.mark.asyncio
async def test_failure():
class FailingService(DummyService):
async def UnaryUnary(self, stream):
raise GRPCError(Status.FAILED_PRECONDITION)
async with ChannelFor([FailingService()]) as channel:
stub = DummyServiceStub(channel)
with pytest.raises(GRPCError) as err:
await stub.UnaryUnary(DummyRequest(value='ping'))
assert err.value.status is Status.FAILED_PRECONDITION

And seems that everything works as expected.

And the same is true for:

await stream.send_trailing_metadata(status=Status.FAILED_PRECONDITION)

@ozangungor12
Copy link
Author

Thanks for the insights!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants