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

Method TestRingHash_HeaderHashing

balancer/ringhash/ringhash_e2e_test.go:732–772  ·  view source on GitHub ↗

Tests that ring hash policy that hashes using a header value can send RPCs to specific backends based on their hash.

(t *testing.T)

Source from the content-addressed store, hash-verified

730// Tests that ring hash policy that hashes using a header value can send RPCs
731// to specific backends based on their hash.
732func (s) TestRingHash_HeaderHashing(t *testing.T) {
733 backends := backendAddrs(startTestServiceBackends(t, 4))
734
735 xdsServer, nodeID, xdsResolver := setupManagementServerAndResolver(t)
736
737 const clusterName = "cluster"
738 endpoints := endpointResource(t, clusterName, backends)
739 cluster := e2e.ClusterResourceWithOptions(e2e.ClusterOptions{
740 ClusterName: clusterName,
741 ServiceName: clusterName,
742 Policy: e2e.LoadBalancingPolicyRingHash,
743 })
744 route := headerHashRoute("new_route", virtualHostName, clusterName, "address_hash")
745 listener := e2e.DefaultClientListener(virtualHostName, route.Name)
746
747 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
748 defer cancel()
749
750 if err := xdsServer.Update(ctx, xdsUpdateOpts(nodeID, endpoints, cluster, route, listener)); err != nil {
751 t.Fatalf("Failed to update xDS resources: %v", err)
752 }
753
754 conn, err := grpc.NewClient("xds:///test.server", grpc.WithResolvers(xdsResolver), grpc.WithTransportCredentials(insecure.NewCredentials()))
755 if err != nil {
756 t.Fatalf("Failed to create client: %s", err)
757 }
758 defer conn.Close()
759 client := testgrpc.NewTestServiceClient(conn)
760
761 // Note each type of RPC contains a header value that will always be hashed
762 // to a specific backend as the header value matches the value used to
763 // create the entry in the ring.
764 for _, backend := range backends {
765 ctx := metadata.NewOutgoingContext(ctx, metadata.Pairs("address_hash", backend+"_0"))
766 numRPCs := 10
767 reqPerBackend := checkRPCSendOK(ctx, t, client, numRPCs)
768 if reqPerBackend[backend] != numRPCs {
769 t.Errorf("Got RPC routed to addresses %v, want all RPCs routed to %v", reqPerBackend, backend)
770 }
771 }
772}
773
774// Tests that ring hash policy that hashes using a header value and regex
775// rewrite to aggregate RPCs to 1 backend.

Callers

nothing calls this directly

Calls 15

DefaultClientListenerFunction · 0.92
NewClientFunction · 0.92
WithResolversFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
NewOutgoingContextFunction · 0.92
PairsFunction · 0.92
backendAddrsFunction · 0.85
endpointResourceFunction · 0.85
headerHashRouteFunction · 0.85
xdsUpdateOptsFunction · 0.85

Tested by

no test coverage detected