MCPcopy
hub / github.com/grpc/grpc-go / TestUnaryClientInterceptor_ContextValuePropagation

Method TestUnaryClientInterceptor_ContextValuePropagation

test/interceptor_test.go:50–82  ·  view source on GitHub ↗

TestUnaryClientInterceptor_ContextValuePropagation verifies that a unary interceptor receives context values specified in the context passed to the RPC call.

(t *testing.T)

Source from the content-addressed store, hash-verified

48// interceptor receives context values specified in the context passed to the
49// RPC call.
50func (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

Callers

nothing calls this directly

Calls 11

SendMethod · 0.95
StartMethod · 0.95
StopMethod · 0.95
ReceiveMethod · 0.95
NewChannelFunction · 0.92
WithUnaryInterceptorFunction · 0.92
WithValueMethod · 0.80
ErrorfMethod · 0.65
FatalfMethod · 0.65
EmptyCallMethod · 0.65
ValueMethod · 0.45

Tested by

no test coverage detected