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

Method TestGRPCLB_PickFirst

balancer/grpclb/grpclb_test.go:1068–1160  ·  view source on GitHub ↗

TestGRPCLB_PickFirst configures grpclb with pick_first as the child policy. The test changes the list of backend addresses returned by the remote balancer and verifies that RPCs are sent to the first address returned.

(t *testing.T)

Source from the content-addressed store, hash-verified

1066// The test changes the list of backend addresses returned by the remote
1067// balancer and verifies that RPCs are sent to the first address returned.
1068func (s) TestGRPCLB_PickFirst(t *testing.T) {
1069 tss, cleanup, err := startBackendsAndRemoteLoadBalancer(t, 3, "", nil)
1070 if err != nil {
1071 t.Fatalf("failed to create new load balancer: %v", err)
1072 }
1073 defer cleanup()
1074
1075 beServers := []*lbpb.Server{{
1076 IpAddress: tss.beIPs[0].AsSlice(),
1077 Port: int32(tss.bePorts[0]),
1078 LoadBalanceToken: lbToken,
1079 }, {
1080 IpAddress: tss.beIPs[1].AsSlice(),
1081 Port: int32(tss.bePorts[1]),
1082 LoadBalanceToken: lbToken,
1083 }, {
1084 IpAddress: tss.beIPs[2].AsSlice(),
1085 Port: int32(tss.bePorts[2]),
1086 LoadBalanceToken: lbToken,
1087 }}
1088 beServerAddrs := []resolver.Address{}
1089 for _, lis := range tss.beListeners {
1090 beServerAddrs = append(beServerAddrs, resolver.Address{Addr: lis.Addr().String()})
1091 }
1092
1093 // Connect to the test backends.
1094 r := manual.NewBuilderWithScheme("whatever")
1095 dopts := []grpc.DialOption{
1096 grpc.WithResolvers(r),
1097 grpc.WithTransportCredentials(&serverNameCheckCreds{}),
1098 grpc.WithContextDialer(fakeNameDialer),
1099 }
1100 cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
1101 if err != nil {
1102 t.Fatalf("Failed to create a client for the backend: %v", err)
1103 }
1104 cc.Connect()
1105 defer cc.Close()
1106
1107 // Push a service config with grpclb as the load balancing policy and
1108 // configure pick_first as its child policy.
1109 rs := resolver.State{ServiceConfig: r.CC().ParseServiceConfig(`{"loadBalancingConfig":[{"grpclb":{"childPolicy":[{"pick_first":{}}]}}]}`)}
1110
1111 // Push a resolver update with the remote balancer address specified via
1112 // attributes.
1113 r.UpdateState(grpclbstate.Set(rs, &grpclbstate.State{BalancerAddresses: []resolver.Address{{Addr: tss.lbAddr, ServerName: lbServerName}}}))
1114
1115 // Push all three backend addresses to the remote balancer, and verify that
1116 // RPCs are routed to the first backend.
1117 tss.ls.sls <- &lbpb.ServerList{Servers: beServers[0:3]}
1118 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
1119 defer cancel()
1120 if err := pickfirst.CheckRPCsToBackend(ctx, cc, beServerAddrs[0]); err != nil {
1121 t.Fatal(err)
1122 }
1123
1124 // Update the address list with the remote balancer and verify pick_first
1125 // behavior based on the new backends.

Callers

nothing calls this directly

Calls 15

NewBuilderWithSchemeFunction · 0.92
WithResolversFunction · 0.92
WithTransportCredentialsFunction · 0.92
WithContextDialerFunction · 0.92
NewClientFunction · 0.92
CheckRPCsToBackendFunction · 0.92
CheckRoundRobinRPCsFunction · 0.92
CCMethod · 0.80
FatalfMethod · 0.65
StringMethod · 0.65
SchemeMethod · 0.65

Tested by

no test coverage detected