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

Method TestGRPCLBWithTargetNameFieldInConfig

balancer/grpclb/grpclb_test.go:1291–1374  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1289}
1290
1291func (s) TestGRPCLBWithTargetNameFieldInConfig(t *testing.T) {
1292 r := manual.NewBuilderWithScheme("whatever")
1293
1294 tss, cleanup, err := startBackendsAndRemoteLoadBalancer(t, 1, "", nil)
1295 if err != nil {
1296 t.Fatalf("failed to create new load balancer: %v", err)
1297 }
1298 defer cleanup()
1299 sl := &lbpb.ServerList{
1300 Servers: []*lbpb.Server{
1301 {
1302 IpAddress: tss.beIPs[0].AsSlice(),
1303 Port: int32(tss.bePorts[0]),
1304 LoadBalanceToken: lbToken,
1305 },
1306 },
1307 }
1308 // Push the backend address to the remote balancer.
1309 tss.ls.sls <- sl
1310
1311 cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName,
1312 grpc.WithResolvers(r),
1313 grpc.WithTransportCredentials(&serverNameCheckCreds{}),
1314 grpc.WithContextDialer(fakeNameDialer),
1315 grpc.WithUserAgent(testUserAgent))
1316 if err != nil {
1317 t.Fatalf("Failed to create a client for the backend %v", err)
1318 }
1319 defer cc.Close()
1320 cc.Connect()
1321 testC := testgrpc.NewTestServiceClient(cc)
1322
1323 // Push a resolver update with grpclb configuration which does not contain the
1324 // target_name field. Our fake remote balancer is configured to always
1325 // expect `beServerName` as the server name in the initial request.
1326 rs := grpclbstate.Set(resolver.State{ServiceConfig: r.CC().ParseServiceConfig(grpclbConfig)},
1327 &grpclbstate.State{BalancerAddresses: []resolver.Address{{
1328 Addr: tss.lbAddr,
1329 ServerName: lbServerName,
1330 }}})
1331 r.UpdateState(rs)
1332
1333 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
1334 defer cancel()
1335 select {
1336 case <-ctx.Done():
1337 t.Fatalf("timeout when waiting for BalanceLoad RPC to be called on the remote balancer")
1338 case <-tss.ls.balanceLoadCh:
1339 }
1340 if _, err := testC.EmptyCall(ctx, &testpb.Empty{}); err != nil {
1341 t.Fatalf("%v.EmptyCall(_, _) = _, %v, want _, <nil>", testC, err)
1342 }
1343
1344 // When the value of target_field changes, grpclb will recreate the stream
1345 // to the remote balancer. So, we need to update the fake remote balancer to
1346 // expect a new server name in the initial request.
1347 const newServerName = "new-server-name"
1348 tss.ls.updateServerName(newServerName)

Callers

nothing calls this directly

Calls 15

EmptyCallMethod · 0.95
NewBuilderWithSchemeFunction · 0.92
NewClientFunction · 0.92
WithResolversFunction · 0.92
WithTransportCredentialsFunction · 0.92
WithContextDialerFunction · 0.92
WithUserAgentFunction · 0.92
CCMethod · 0.80
updateServerNameMethod · 0.80
FatalfMethod · 0.65
SchemeMethod · 0.65

Tested by

no test coverage detected