Setting the context for unit testing

1,118 views
Skip to first unread message

marquis.t...@gmail.com

unread,
May 19, 2018, 2:39:51 AM5/19/18
to grpc.io
I have a gRPC service that reads values from the context.  While unit testing how do I set the value of the context being used in java?

My current setup that does not work is using GrpcServerRule().directExecutor().  In the @Before method I update the context via 
Context.current().withValue("key", "value")).apply();

The server returned doesnt seems to be using this context because I guess this inst the correct thread scope to attach it to.  Any tips?

Thanks in advance.

Spencer Fang

unread,
May 21, 2018, 11:45:20 AM5/21/18
to marquis.t...@gmail.com, grpc.io
If you are doing an end to end test that involves setting up an in process server as you are doing with GrpcServerRule, I would recommend just creating the context normally (most likely based on the initial metadata). If you want to unit test the server handler in isolation, then you probably don't want to use GrpcServerRule and instead want to call your handler directly with the context attached.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/b82e88f4-b36d-4b02-aa2c-ab9c85dffe06%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Spencer Fang

Penn (Dapeng) Zhang

unread,
May 30, 2018, 1:20:42 PM5/30/18
to grpc.io
Do you have a junit test Timeout? If you have timeout, then you can not attach a context in set up, because with timeout, the test method runs in a different thread the @Before method.
Reply all
Reply to author
Forward
0 new messages