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

Method TestBalancerClose

internal/balancer/gracefulswitch/gracefulswitch_test.go:443–547  ·  view source on GitHub ↗

TestBalancerClose tests the graceful switch balancer's Close() functionality. From the Close() call, the graceful switch balancer should shut down any created Subconns and Close() the current and pending load balancers. This Close() call should also cause any other events (calls to entrance functio

(t *testing.T)

Source from the content-addressed store, hash-verified

441// balancers. This Close() call should also cause any other events (calls to
442// entrance functions) to be no-ops.
443func (s) TestBalancerClose(t *testing.T) {
444 // Setup gsb balancer with current, pending, and one created SubConn on both
445 // current and pending.
446 tcc, gsb := setup(t)
447 gsb.SwitchTo(mockBalancerBuilder1{})
448 gsb.SwitchTo(mockBalancerBuilder2{})
449
450 sc1, err := gsb.balancerCurrent.Balancer.(*mockBalancer).newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{})
451 // Will eventually get back a SubConn with an identifying property id 1
452 if err != nil {
453 t.Fatalf("error constructing newSubConn in gsb: %v", err)
454 }
455 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
456 defer cancel()
457 select {
458 case <-ctx.Done():
459 t.Fatalf("timeout while waiting for an NewSubConn call on the ClientConn")
460 case <-tcc.NewSubConnCh:
461 }
462
463 sc2, err := gsb.balancerPending.Balancer.(*mockBalancer).newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{})
464 // Will eventually get back a SubConn with an identifying property id 2
465 if err != nil {
466 t.Fatalf("error constructing newSubConn in gsb: %v", err)
467 }
468 select {
469 case <-ctx.Done():
470 t.Fatalf("timeout while waiting for an NewSubConn call on the ClientConn")
471 case <-tcc.NewSubConnCh:
472 }
473
474 currBal := gsb.balancerCurrent.Balancer.(*mockBalancer)
475 pendBal := gsb.balancerPending.Balancer.(*mockBalancer)
476
477 // Closing the graceful switch load balancer should lead to removing any
478 // created SubConns, and closing both the current and pending load balancer.
479 gsb.Close()
480
481 // The order of SubConns the graceful switch load balancer tells the Client
482 // Conn to shut down is non deterministic, as it is stored in a
483 // map. However, the first SubConn shut down should be either sc1 or sc2.
484 select {
485 case <-ctx.Done():
486 t.Fatalf("timeout while waiting for an UpdateAddresses call on the ClientConn")
487 case sc := <-tcc.ShutdownSubConnCh:
488 if sc != sc1 && sc != sc2 {
489 t.Fatalf("ShutdownSubConn, want either %v or %v, got %v", sc1, sc2, sc)
490 }
491 }
492
493 // The graceful switch load balancer should then tell the ClientConn to
494 // shut down the other SubConn.
495 select {
496 case <-ctx.Done():
497 t.Fatalf("timeout while waiting for an UpdateAddresses call on the ClientConn")
498 case sc := <-tcc.ShutdownSubConnCh:
499 if sc != sc1 && sc != sc2 {
500 t.Fatalf("ShutdownSubConn, want either %v or %v, got %v", sc1, sc2, sc)

Callers

nothing calls this directly

Calls 13

SwitchToMethod · 0.80
waitForResolverErrorMethod · 0.80
ErrMethod · 0.80
setupFunction · 0.70
FatalfMethod · 0.65
CloseMethod · 0.65
FatalMethod · 0.65
UpdateClientConnStateMethod · 0.65
ResolverErrorMethod · 0.65
ErrorMethod · 0.65
newSubConnMethod · 0.45
DoneMethod · 0.45

Tested by

no test coverage detected