TestUnaryClientInterceptor_ContextValuePropagation verifies that a unary interceptor receives context values specified in the context passed to the RPC call.
(t *testing.T)
| 48 | // interceptor receives context values specified in the context passed to the |
| 49 | // RPC call. |
| 50 | func (s) TestUnaryClientInterceptor_ContextValuePropagation(t *testing.T) { |
| 51 | errCh := testutils.NewChannel() |
| 52 | unaryInt := func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { |
| 53 | if got, ok := ctx.Value(parentCtxkey{}).(string); !ok || got != parentCtxVal { |
| 54 | errCh.Send(fmt.Errorf("unaryInt got %q in context.Val, want %q", got, parentCtxVal)) |
| 55 | } |
| 56 | errCh.Send(nil) |
| 57 | return invoker(ctx, method, req, reply, cc, opts...) |
| 58 | } |
| 59 | |
| 60 | // Start a stub server and use the above unary interceptor while creating a |
| 61 | // ClientConn to it. |
| 62 | ss := &stubserver.StubServer{ |
| 63 | EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil }, |
| 64 | } |
| 65 | if err := ss.Start(nil, grpc.WithUnaryInterceptor(unaryInt)); err != nil { |
| 66 | t.Fatalf("Failed to start stub server: %v", err) |
| 67 | } |
| 68 | defer ss.Stop() |
| 69 | |
| 70 | ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 71 | defer cancel() |
| 72 | if _, err := ss.Client.EmptyCall(context.WithValue(ctx, parentCtxkey{}, parentCtxVal), &testpb.Empty{}); err != nil { |
| 73 | t.Fatalf("ss.Client.EmptyCall() failed: %v", err) |
| 74 | } |
| 75 | val, err := errCh.Receive(ctx) |
| 76 | if err != nil { |
| 77 | t.Fatalf("timeout when waiting for unary interceptor to be invoked: %v", err) |
| 78 | } |
| 79 | if val != nil { |
| 80 | t.Fatalf("unary interceptor failed: %v", val) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // TestChainUnaryClientInterceptor_ContextValuePropagation verifies that a chain |
| 85 | // of unary interceptors receive context values specified in the original call |
nothing calls this directly
no test coverage detected