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

Method TestPickerRandomHash

balancer/ringhash/picker_test.go:250–311  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

248}
249
250func (s) TestPickerRandomHash(t *testing.T) {
251 tests := []struct {
252 name string
253 hash uint64
254 connectivityStates []connectivity.State
255 wantSC balancer.SubConn
256 wantErr error
257 wantSCToConnect balancer.SubConn
258 hasEndpointInConnectingState bool
259 }{
260 {
261 name: "header not set, picked is Ready",
262 connectivityStates: []connectivity.State{connectivity.Ready, connectivity.Idle},
263 wantSC: testSubConns[0],
264 },
265 {
266 name: "header not set, picked is Idle, another is Ready. Connect and pick Ready",
267 connectivityStates: []connectivity.State{connectivity.Idle, connectivity.Ready},
268 wantSC: testSubConns[1],
269 wantSCToConnect: testSubConns[0],
270 },
271 {
272 name: "header not set, picked is Idle, there is at least one Connecting",
273 connectivityStates: []connectivity.State{connectivity.Connecting, connectivity.Idle},
274 wantErr: balancer.ErrNoSubConnAvailable,
275 hasEndpointInConnectingState: true,
276 },
277 {
278 name: "header not set, all Idle or TransientFailure, connect",
279 connectivityStates: []connectivity.State{connectivity.TransientFailure, connectivity.Idle},
280 wantErr: balancer.ErrNoSubConnAvailable,
281 wantSCToConnect: testSubConns[1],
282 },
283 }
284 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
285 defer cancel()
286 for _, tt := range tests {
287 t.Run(tt.name, func(t *testing.T) {
288 ring, epStates := testRingAndEndpointStates(tt.connectivityStates)
289 p := &picker{
290 ring: ring,
291 endpointStates: epStates,
292 requestHashHeader: "some-header",
293 hasEndpointInConnectingState: tt.hasEndpointInConnectingState,
294 randUint64: func() uint64 { return 0 }, // always return the first endpoint on the ring.
295 }
296 if got, err := p.Pick(balancer.PickInfo{Ctx: ctx}); err != tt.wantErr {
297 t.Errorf("Pick() error = %v, wantErr %v", err, tt.wantErr)
298 return
299 } else if got.SubConn != tt.wantSC {
300 t.Errorf("Pick() got = %v, want picked SubConn: %v", got, tt.wantSC)
301 }
302 if sc := tt.wantSCToConnect; sc != nil {
303 select {
304 case <-sc.(*testutils.TestSubConn).ConnectCh:
305 case <-time.After(defaultTestShortTimeout):
306 t.Errorf("timeout waiting for Connect() from SubConn %v", sc)
307 }

Callers

nothing calls this directly

Calls 3

PickMethod · 0.95
ErrorfMethod · 0.65

Tested by

no test coverage detected