[Grpc Python] Tests - intercept request

407 views
Skip to first unread message

Fabien

unread,
Jun 3, 2022, 8:47:26 AM6/3/22
to grpc.io
Hi all,

I am currently working on writing tests to my service implemented with grpc python - using grpc_testing package.

Everything has been quite smooth until now, using the inovke methods and so on which works very well.

However i'm now having a hard time : one of my service - let's call it service 1 -  has a method which call another service - service 2 -, the current implementation create the stub channel directly in the method from service 1 with a given ip and port.
As such, I would like in my tests for this method (in service 1) to intercept every request adressed to service 2 (could it be done independently of any ip / port given in the real implementation ?) and return a mock value when necessary.

So far here is how i see things :

      ----- Set up step -----
  1. Register service 1 with grpc_testing.server_from_dictionary
  2. Create a mock for service 2
     ----- When testing the method calling service 2 ----
  1. Register service 2 mock to intercept requests
  2. Invoke_unary_unay
  3. Service 2 return the mock value
  4. Collect result of method in service 1
Points 1 and 3 from part 2 are what I am struggling with.

I have seen the examples in https://github.com/grpc/grpc/tree/master/src/python/grpcio_tests/tests/testing and the client test is what I found to be the more interesting for the given points, but is not consistent with my current implementation.
I nonetheless would like to know if any of you have any tip on this please ?

Thank you in advance !

Sincerly,

Lidi Zheng

unread,
Jun 8, 2022, 2:42:28 PM6/8/22
to grpc.io
For implementing the testing interceptor, you might find this example useful: https://github.com/grpc/grpc/tree/master/examples/python/interceptors/default_value, which injects default response value.

Alternatively, you could create a fake server instead. This will ensure you are only testing the intended parts of your service.

Fabien

unread,
Jun 10, 2022, 3:48:04 AM6/10/22
to grpc.io
Thank you for your reply !

Fake server is indeed the solution i used until now.
Thanks for the example, it somehow corresponds to what i was looking for.

Have a nice day.
Reply all
Reply to author
Forward
0 new messages