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)
| 889 | // explicit fallback signal to the grpclb client, and the test verifies that |
| 890 | // RPCs are routed to the fallback backend. |
| 891 | func (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. |
nothing calls this directly
no test coverage detected