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

Method TestBalancerSwitch_grpclbNotRegistered

test/balancer_switching_test.go:331–376  ·  view source on GitHub ↗

TestBalancerSwitch_grpclbNotRegistered tests the scenario where the grpclb balancer is not registered. Verifies that the ClientConn falls back to the default LB policy or the LB policy specified in the service config, and that addresses of type "grpclb" are filtered out.

(t *testing.T)

Source from the content-addressed store, hash-verified

329// default LB policy or the LB policy specified in the service config, and that
330// addresses of type "grpclb" are filtered out.
331func (s) TestBalancerSwitch_grpclbNotRegistered(t *testing.T) {
332 // Unregister the grpclb balancer builder for the duration of this test.
333 grpclbBuilder := balancer.Get("grpclb")
334 internal.BalancerUnregister(grpclbBuilder.Name())
335 defer balancer.Register(grpclbBuilder)
336
337 backends, cleanup := startBackendsForBalancerSwitch(t)
338 defer cleanup()
339 addrs := stubBackendsToResolverAddrs(backends)
340
341 r := manual.NewBuilderWithScheme("whatever")
342 cc, err := grpc.NewClient(r.Scheme()+":///test.server", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(r))
343 if err != nil {
344 t.Fatalf("grpc.NewClient() failed: %v", err)
345 }
346 defer cc.Close()
347 cc.Connect()
348
349 // Push a resolver update which contains a bunch of stub server backends and a
350 // grpclb server address. The latter should get the ClientConn to try and
351 // apply the grpclb policy. But since grpclb is not registered, it should
352 // fallback to the default LB policy which is pick_first. The ClientConn is
353 // also expected to filter out the grpclb address when sending the addresses
354 // list for pick_first.
355 grpclbAddr := []resolver.Address{{Addr: "non-existent-grpclb-server-address"}}
356 grpclbConfig := parseServiceConfig(t, r, `{"loadBalancingPolicy": "grpclb"}`)
357 state := resolver.State{ServiceConfig: grpclbConfig, Addresses: addrs}
358 r.UpdateState(grpclbstate.Set(state, &grpclbstate.State{BalancerAddresses: grpclbAddr}))
359 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
360 defer cancel()
361 if err := pfutil.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil {
362 t.Fatalf("Pick_first backend readiness check failed: %v", err)
363 }
364
365 // Push a resolver update with the same addresses, but with a service config
366 // specifying "round_robin". The ClientConn is expected to filter out the
367 // grpclb address when sending the addresses list to round_robin.
368 r.UpdateState(resolver.State{
369 Addresses: addrs,
370 ServiceConfig: parseServiceConfig(t, r, rrServiceConfig),
371 })
372 client := testgrpc.NewTestServiceClient(cc)
373 if err := rrutil.CheckRoundRobinRPCs(ctx, client, addrs); err != nil {
374 t.Fatalf("Round robin RPCs failed: %v", err)
375 }
376}
377
378// TestBalancerSwitch_OldBalancerCallsShutdownInClose tests the scenario where
379// the balancer being switched out calls Shutdown() in its Close()

Callers

nothing calls this directly

Calls 15

GetFunction · 0.92
RegisterFunction · 0.92
NewBuilderWithSchemeFunction · 0.92
NewClientFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
WithResolversFunction · 0.92
parseServiceConfigFunction · 0.70
NameMethod · 0.65
SchemeMethod · 0.65

Tested by

no test coverage detected