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

Method TestGRPCLB_Fallback

balancer/grpclb/grpclb_test.go:793–886  ·  view source on GitHub ↗

TestGRPCLB_Fallback tests the following fallback scenarios: - when the remote balancer address specified in attributes is invalid, the test verifies that RPCs are routed to the fallback backend. - when the remote balancer address specified in attributes is changed to a valid one, the test verifies t

(t *testing.T)

Source from the content-addressed store, hash-verified

791// - when the configured remote balancer goes down, the test verifies that
792// RPCs are routed to the fallback backend.
793func (s) TestGRPCLB_Fallback(t *testing.T) {
794 balancer.Register(newLBBuilderWithFallbackTimeout(100 * time.Millisecond))
795 defer balancer.Register(newLBBuilder())
796
797 tss, cleanup, err := startBackendsAndRemoteLoadBalancer(t, 1, "", nil)
798 if err != nil {
799 t.Fatalf("failed to create new load balancer: %v", err)
800 }
801 defer cleanup()
802 sl := &lbpb.ServerList{
803 Servers: []*lbpb.Server{
804 {
805 IpAddress: tss.beIPs[0].AsSlice(),
806 Port: int32(tss.bePorts[0]),
807 LoadBalanceToken: lbToken,
808 },
809 },
810 }
811 // Push the backend address to the remote balancer.
812 tss.ls.sls <- sl
813
814 // Start a standalone backend for fallback.
815 beLis, err := net.Listen("tcp", "localhost:0")
816 if err != nil {
817 t.Fatalf("Failed to listen %v", err)
818 }
819 defer beLis.Close()
820 standaloneBEs := startBackends(t, beServerName, true, beLis)
821 defer stopBackends(standaloneBEs)
822
823 r := manual.NewBuilderWithScheme("whatever")
824 // Set the initial resolver state with fallback backend address stored in
825 // the `Addresses` field and an invalid remote balancer address stored in
826 // attributes, which will cause fallback behavior to be invoked.
827 rs := resolver.State{
828 Addresses: []resolver.Address{{Addr: beLis.Addr().String()}},
829 ServiceConfig: internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(grpclbConfig),
830 }
831 rs = grpclbstate.Set(rs, &grpclbstate.State{BalancerAddresses: []resolver.Address{{Addr: "invalid.address", ServerName: lbServerName}}})
832 r.InitialState(rs)
833
834 dopts := []grpc.DialOption{
835 grpc.WithResolvers(r),
836 grpc.WithTransportCredentials(&serverNameCheckCreds{}),
837 grpc.WithContextDialer(fakeNameDialer),
838 }
839 cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
840 if err != nil {
841 t.Fatalf("Failed to create new client to the backend %v", err)
842 }
843 defer cc.Close()
844 testC := testgrpc.NewTestServiceClient(cc)
845
846 // Make an RPC and verify that it got routed to the fallback backend.
847 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
848 defer cancel()
849 if err := roundrobin.CheckRoundRobinRPCs(ctx, testC, []resolver.Address{{Addr: beLis.Addr().String()}}); err != nil {
850 t.Fatal(err)

Callers

nothing calls this directly

Calls 15

RegisterFunction · 0.92
NewBuilderWithSchemeFunction · 0.92
WithResolversFunction · 0.92
WithTransportCredentialsFunction · 0.92
WithContextDialerFunction · 0.92
NewClientFunction · 0.92
CheckRoundRobinRPCsFunction · 0.92
newLBBuilderFunction · 0.85
stopBackendsFunction · 0.85
InitialStateMethod · 0.80

Tested by

no test coverage detected