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

Method TestGRPCLB_ExplicitFallback

balancer/grpclb/grpclb_test.go:891–959  ·  view source on GitHub ↗

TestGRPCLB_ExplicitFallback tests the case where the remote balancer sends an explicit fallback signal to the grpclb client, and the test verifies that RPCs are routed to the fallback backend.

(t *testing.T)

Source from the content-addressed store, hash-verified

889// explicit fallback signal to the grpclb client, and the test verifies that
890// RPCs are routed to the fallback backend.
891func (s) TestGRPCLB_ExplicitFallback(t *testing.T) {
892 tss, cleanup, err := startBackendsAndRemoteLoadBalancer(t, 1, "", nil)
893 if err != nil {
894 t.Fatalf("failed to create new load balancer: %v", err)
895 }
896 defer cleanup()
897 sl := &lbpb.ServerList{
898 Servers: []*lbpb.Server{
899 {
900 IpAddress: tss.beIPs[0].AsSlice(),
901 Port: int32(tss.bePorts[0]),
902 LoadBalanceToken: lbToken,
903 },
904 },
905 }
906 // Push the backend address to the remote balancer.
907 tss.ls.sls <- sl
908
909 // Start a standalone backend for fallback.
910 beLis, err := net.Listen("tcp", "localhost:0")
911 if err != nil {
912 t.Fatalf("Failed to listen %v", err)
913 }
914 defer beLis.Close()
915 standaloneBEs := startBackends(t, beServerName, true, beLis)
916 defer stopBackends(standaloneBEs)
917
918 // Configure the manual resolver with an initial state containing a service
919 // config with grpclb as the load balancing policy and the address of the
920 // fallback backend. The remote balancer address is specified via
921 // attributes.
922 r := manual.NewBuilderWithScheme("whatever")
923 rs := resolver.State{
924 Addresses: []resolver.Address{{Addr: beLis.Addr().String()}},
925 ServiceConfig: internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(grpclbConfig),
926 }
927 rs = grpclbstate.Set(rs, &grpclbstate.State{BalancerAddresses: []resolver.Address{{Addr: tss.lbAddr, ServerName: lbServerName}}})
928 r.InitialState(rs)
929
930 dopts := []grpc.DialOption{
931 grpc.WithResolvers(r),
932 grpc.WithTransportCredentials(&serverNameCheckCreds{}),
933 grpc.WithContextDialer(fakeNameDialer),
934 }
935 cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
936 if err != nil {
937 t.Fatalf("Failed to create a client for the backend %v", err)
938 }
939 defer cc.Close()
940 testC := testgrpc.NewTestServiceClient(cc)
941
942 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
943 defer cancel()
944 if err := roundrobin.CheckRoundRobinRPCs(ctx, testC, []resolver.Address{{Addr: tss.beListeners[0].Addr().String()}}); err != nil {
945 t.Fatal(err)
946 }
947
948 // Send fallback signal from remote balancer; should use fallback.

Callers

nothing calls this directly

Calls 15

NewBuilderWithSchemeFunction · 0.92
WithResolversFunction · 0.92
WithTransportCredentialsFunction · 0.92
WithContextDialerFunction · 0.92
NewClientFunction · 0.92
CheckRoundRobinRPCsFunction · 0.92
stopBackendsFunction · 0.85
InitialStateMethod · 0.80
fallbackNowMethod · 0.80
startBackendsFunction · 0.70
FatalfMethod · 0.65

Tested by

no test coverage detected