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

Method TestRingHash_NoHashPolicy

balancer/ringhash/ringhash_e2e_test.go:872–910  ·  view source on GitHub ↗

Tests that ring hash policy that hashes using a random value.

(t *testing.T)

Source from the content-addressed store, hash-verified

870
871// Tests that ring hash policy that hashes using a random value.
872func (s) TestRingHash_NoHashPolicy(t *testing.T) {
873 backends := backendAddrs(startTestServiceBackends(t, 2))
874 numRPCs := computeIdealNumberOfRPCs(t, .5, errorTolerance)
875
876 const clusterName = "cluster"
877 endpoints := endpointResource(t, clusterName, backends)
878 cluster := e2e.ClusterResourceWithOptions(e2e.ClusterOptions{
879 ClusterName: clusterName,
880 ServiceName: clusterName,
881 })
882 setRingHashLBPolicyWithHighMinRingSize(t, cluster)
883 route := e2e.DefaultRouteConfig("new_route", virtualHostName, clusterName)
884 listener := e2e.DefaultClientListener(virtualHostName, route.Name)
885
886 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
887 defer cancel()
888
889 xdsServer, nodeID, xdsResolver := setupManagementServerAndResolver(t)
890 if err := xdsServer.Update(ctx, xdsUpdateOpts(nodeID, endpoints, cluster, route, listener)); err != nil {
891 t.Fatalf("Failed to update xDS resources: %v", err)
892 }
893
894 conn, err := grpc.NewClient("xds:///test.server", grpc.WithResolvers(xdsResolver), grpc.WithTransportCredentials(insecure.NewCredentials()))
895 if err != nil {
896 t.Fatalf("Failed to create client: %s", err)
897 }
898 defer conn.Close()
899 client := testgrpc.NewTestServiceClient(conn)
900
901 // Send a large number of RPCs and check that they are distributed randomly.
902 gotPerBackend := checkRPCSendOK(ctx, t, client, numRPCs)
903 for _, backend := range backends {
904 got := float64(gotPerBackend[backend]) / float64(numRPCs)
905 want := .5
906 if !cmp.Equal(got, want, cmpopts.EquateApprox(0, errorTolerance)) {
907 t.Errorf("Fraction of RPCs to backend %s: got %v, want %v (margin: +-%v)", backend, got, want, errorTolerance)
908 }
909 }
910}
911
912// Tests that we observe endpoint weights.
913func (s) TestRingHash_EndpointWeights(t *testing.T) {

Callers

nothing calls this directly

Calls 15

DefaultRouteConfigFunction · 0.92
DefaultClientListenerFunction · 0.92
NewClientFunction · 0.92
WithResolversFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
backendAddrsFunction · 0.85
computeIdealNumberOfRPCsFunction · 0.85
endpointResourceFunction · 0.85
xdsUpdateOptsFunction · 0.85

Tested by

no test coverage detected