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)
| 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. |
| 56 | func (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) |
nothing calls this directly
no test coverage detected