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

Method TestCustomLB

interop/xds/custom_lb_test.go:56–135  ·  view source on GitHub ↗

TestCustomLB tests the Custom LB for the interop client. It configures the custom lb as the top level Load Balancing policy of the channel, then asserts it can successfully make an RPC and also that the rpc behavior the Custom LB is configured with makes it's way to the server in metadata.

(t *testing.T)

Source from the content-addressed store, hash-verified

54// it can successfully make an RPC and also that the rpc behavior the Custom LB
55// is configured with makes it's way to the server in metadata.
56func (s) TestCustomLB(t *testing.T) {
57 errCh := testutils.NewChannel()
58 // Setup a backend which verifies the expected rpc-behavior metadata is
59 // present in the request.
60 backend := &stubserver.StubServer{
61 UnaryCallF: func(ctx context.Context, _ *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
62 md, ok := metadata.FromIncomingContext(ctx)
63 if !ok {
64 errCh.Send(errors.New("failed to receive metadata"))
65 return &testpb.SimpleResponse{}, nil
66 }
67 rpcBMD := md.Get("rpc-behavior")
68 if len(rpcBMD) != 1 {
69 errCh.Send(fmt.Errorf("received %d values for metadata key \"rpc-behavior\", want 1", len(rpcBMD)))
70 return &testpb.SimpleResponse{}, nil
71 }
72 wantVal := "error-code-0"
73 if rpcBMD[0] != wantVal {
74 errCh.Send(fmt.Errorf("metadata val for key \"rpc-behavior\": got val %v, want val %v", rpcBMD[0], wantVal))
75 return &testpb.SimpleResponse{}, nil
76 }
77 // Success.
78 errCh.Send(nil)
79 return &testpb.SimpleResponse{}, nil
80 },
81 }
82 if err := backend.StartServer(); err != nil {
83 t.Fatalf("Failed to start backend: %v", err)
84 }
85 t.Logf("Started good TestService backend at: %q", backend.Address)
86 defer backend.Stop()
87
88 lbCfgJSON := `{
89 "loadBalancingConfig": [
90 {
91 "test.RpcBehaviorLoadBalancer": {
92 "rpcBehavior": "error-code-0"
93 }
94 }
95 ]
96 }`
97
98 sc := internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(lbCfgJSON)
99 mr := manual.NewBuilderWithScheme("customlb-e2e")
100 defer mr.Close()
101 mr.InitialState(resolver.State{
102 Addresses: []resolver.Address{
103 {Addr: backend.Address},
104 },
105 ServiceConfig: sc,
106 })
107
108 cc, err := grpc.NewClient(mr.Scheme()+":///", grpc.WithResolvers(mr), grpc.WithTransportCredentials(insecure.NewCredentials()))
109 if err != nil {
110 t.Fatalf("grpc.NewClient() failed: %v", err)
111 }
112 defer cc.Close()
113 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)

Callers

nothing calls this directly

Calls 15

SendMethod · 0.95
StartServerMethod · 0.95
StopMethod · 0.95
UnaryCallMethod · 0.95
ReceiveMethod · 0.95
NewChannelFunction · 0.92
FromIncomingContextFunction · 0.92
NewBuilderWithSchemeFunction · 0.92
NewClientFunction · 0.92
WithResolversFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92

Tested by

no test coverage detected