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

Function checkRoundRobinRPCs

balancer/leastrequest/leastrequest_test.go:165–198  ·  view source on GitHub ↗

checkRoundRobinRPCs verifies that EmptyCall RPCs on the given ClientConn, connected to a server exposing the test.grpc_testing.TestService, are roundrobined across the given backend addresses. Returns a non-nil error if context deadline expires before RPCs start to get roundrobined across the given

(ctx context.Context, client testgrpc.TestServiceClient, addrs []resolver.Address)

Source from the content-addressed store, hash-verified

163// Returns a non-nil error if context deadline expires before RPCs start to get
164// roundrobined across the given backends.
165func checkRoundRobinRPCs(ctx context.Context, client testgrpc.TestServiceClient, addrs []resolver.Address) error {
166 wantAddrCount := make(map[string]int)
167 for _, addr := range addrs {
168 wantAddrCount[addr.Addr]++
169 }
170 gotAddrCount := make(map[string]int)
171 for ; ctx.Err() == nil; <-time.After(time.Millisecond) {
172 gotAddrCount = make(map[string]int)
173 // Perform 3 iterations.
174 var iterations [][]string
175 for i := 0; i < 3; i++ {
176 iteration := make([]string, len(addrs))
177 for c := 0; c < len(addrs); c++ {
178 var peer peer.Peer
179 client.EmptyCall(ctx, &testpb.Empty{}, grpc.Peer(&peer))
180 iteration[c] = peer.Addr.String()
181 }
182 iterations = append(iterations, iteration)
183 }
184 // Ensure the first iteration contains all addresses in addrs.
185 for _, addr := range iterations[0] {
186 gotAddrCount[addr]++
187 }
188 if !cmp.Equal(gotAddrCount, wantAddrCount) {
189 continue
190 }
191 // Ensure all three iterations contain the same addresses.
192 if !cmp.Equal(iterations[0], iterations[1]) || !cmp.Equal(iterations[0], iterations[2]) {
193 continue
194 }
195 return nil
196 }
197 return fmt.Errorf("timeout when waiting for roundrobin distribution of RPCs across addresses: %v; got: %v", addrs, gotAddrCount)
198}
199
200// TestLeastRequestE2E tests the Least Request LB policy in an e2e style. The
201// Least Request balancer is configured as the top level balancer of the

Calls 6

PeerFunction · 0.92
ErrMethod · 0.80
EmptyCallMethod · 0.65
StringMethod · 0.65
EqualMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected