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

Method TestEnterIdleDuringBalancerNewSubConn

resolver_balancer_ext_test.go:261–306  ·  view source on GitHub ↗

TestEnterIdleDuringBalancerNewSubConn tests calling NewSubConn at the same time as the balancer being closed while the channel enters idle mode.

(t *testing.T)

Source from the content-addressed store, hash-verified

259// TestEnterIdleDuringBalancerNewSubConn tests calling NewSubConn at the same
260// time as the balancer being closed while the channel enters idle mode.
261func (s) TestEnterIdleDuringBalancerNewSubConn(t *testing.T) {
262 channelz.TurnOn()
263 defer internal.ChannelzTurnOffForTesting()
264 enterIdle := internal.EnterIdleModeForTesting.(func(*grpc.ClientConn))
265 name := strings.ReplaceAll(strings.ToLower(t.Name()), "/", "")
266
267 // Create a balancer that calls NewSubConn once asynchronously, attempting
268 // to create a subchannel after going idle.
269 bf := stub.BalancerFuncs{
270 UpdateClientConnState: func(bd *stub.BalancerData, _ balancer.ClientConnState) error {
271 go func() {
272 bd.ClientConn.NewSubConn([]resolver.Address{{Addr: "test"}}, balancer.NewSubConnOptions{})
273 }()
274 return nil
275 },
276 }
277 stub.Register(name, bf)
278
279 rb := manual.NewBuilderWithScheme(name)
280 rb.BuildCallback = func(_ resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) {
281 cc.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: "test"}}})
282 }
283 resolver.Register(rb)
284
285 cc, err := grpc.NewClient(
286 name+":///",
287 grpc.WithTransportCredentials(insecure.NewCredentials()),
288 grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"`+name+`":{}}]}`))
289 if err != nil {
290 t.Fatalf("grpc.NewClient error: %v", err)
291 }
292 defer cc.Close()
293
294 // Enter/exit idle mode repeatedly.
295 for i := 0; i < 2000; i++ {
296 enterIdle(cc)
297 tcs, _ := channelz.GetTopChannels(0, 0)
298 if len(tcs) != 1 {
299 t.Fatalf("Found channels: %v; expected 1 entry", tcs)
300 }
301 if got := tcs[0].SubChans(); len(got) != 0 {
302 t.Fatalf("Found subchannels: %v; expected 0 entries", got)
303 }
304 cc.Connect()
305 }
306}

Callers

nothing calls this directly

Calls 15

TurnOnFunction · 0.92
RegisterFunction · 0.92
NewBuilderWithSchemeFunction · 0.92
RegisterFunction · 0.92
NewClientFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
WithDefaultServiceConfigFunction · 0.92
GetTopChannelsFunction · 0.92
SubChansMethod · 0.80
NameMethod · 0.65
NewSubConnMethod · 0.65

Tested by

no test coverage detected