google.golang.org/grpc@v1.72.2/internal/idle/idle_e2e_test.go (about)

     1  /*
     2   *
     3   * Copyright 2023 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package idle_test
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"io"
    25  	"strings"
    26  	"sync"
    27  	"testing"
    28  	"time"
    29  
    30  	"google.golang.org/grpc"
    31  	"google.golang.org/grpc/balancer"
    32  	"google.golang.org/grpc/balancer/roundrobin"
    33  	"google.golang.org/grpc/codes"
    34  	"google.golang.org/grpc/connectivity"
    35  	"google.golang.org/grpc/credentials/insecure"
    36  	"google.golang.org/grpc/internal"
    37  	"google.golang.org/grpc/internal/balancer/stub"
    38  	"google.golang.org/grpc/internal/channelz"
    39  	"google.golang.org/grpc/internal/grpctest"
    40  	"google.golang.org/grpc/internal/stubserver"
    41  	"google.golang.org/grpc/internal/testutils"
    42  	"google.golang.org/grpc/resolver"
    43  	"google.golang.org/grpc/resolver/manual"
    44  	"google.golang.org/grpc/status"
    45  
    46  	testgrpc "google.golang.org/grpc/interop/grpc_testing"
    47  	testpb "google.golang.org/grpc/interop/grpc_testing"
    48  )
    49  
    50  func init() {
    51  	channelz.TurnOn()
    52  }
    53  
    54  type s struct {
    55  	grpctest.Tester
    56  }
    57  
    58  func Test(t *testing.T) {
    59  	grpctest.RunSubTests(t, s{})
    60  }
    61  
    62  const (
    63  	defaultTestTimeout          = 10 * time.Second
    64  	defaultTestShortTimeout     = 100 * time.Millisecond
    65  	defaultTestShortIdleTimeout = 500 * time.Millisecond
    66  )
    67  
    68  // channelzTraceEventFound looks up the top-channels in channelz (expects a
    69  // single one), and checks if there is a trace event on the channel matching the
    70  // provided description string.
    71  func channelzTraceEventFound(ctx context.Context, wantDesc string) error {
    72  	for ctx.Err() == nil {
    73  		tcs, _ := channelz.GetTopChannels(0, 0)
    74  		if l := len(tcs); l != 1 {
    75  			return fmt.Errorf("when looking for channelz trace event with description %q, found %d top-level channels, want 1", wantDesc, l)
    76  		}
    77  		trace := tcs[0].Trace()
    78  		if trace == nil {
    79  			return fmt.Errorf("when looking for channelz trace event with description %q, no trace events found for top-level channel", wantDesc)
    80  		}
    81  
    82  		for _, e := range trace.Events {
    83  			if strings.Contains(e.Desc, wantDesc) {
    84  				return nil
    85  			}
    86  		}
    87  	}
    88  	return fmt.Errorf("when looking for channelz trace event with description %q, %w", wantDesc, ctx.Err())
    89  }
    90  
    91  // Registers a wrapped round_robin LB policy for the duration of this test that
    92  // retains all the functionality of the round_robin LB policy and makes the
    93  // balancer close event available for inspection by the test.
    94  //
    95  // Returns a channel that gets pinged when the balancer is closed.
    96  func registerWrappedRoundRobinPolicy(t *testing.T) chan struct{} {
    97  	rrBuilder := balancer.Get(roundrobin.Name)
    98  	closeCh := make(chan struct{}, 1)
    99  	stub.Register(roundrobin.Name, stub.BalancerFuncs{
   100  		Init: func(bd *stub.BalancerData) {
   101  			bd.Data = rrBuilder.Build(bd.ClientConn, bd.BuildOptions)
   102  		},
   103  		UpdateClientConnState: func(bd *stub.BalancerData, ccs balancer.ClientConnState) error {
   104  			bal := bd.Data.(balancer.Balancer)
   105  			return bal.UpdateClientConnState(ccs)
   106  		},
   107  		Close: func(bd *stub.BalancerData) {
   108  			select {
   109  			case closeCh <- struct{}{}:
   110  			default:
   111  			}
   112  			bal := bd.Data.(balancer.Balancer)
   113  			bal.Close()
   114  		},
   115  	})
   116  	t.Cleanup(func() { balancer.Register(rrBuilder) })
   117  
   118  	return closeCh
   119  }
   120  
   121  // Tests the case where channel idleness is disabled by passing an idle_timeout
   122  // of 0. Verifies that a READY channel with no RPCs does not move to IDLE.
   123  func (s) TestChannelIdleness_Disabled_NoActivity(t *testing.T) {
   124  	closeCh := registerWrappedRoundRobinPolicy(t)
   125  
   126  	// Create a ClientConn with idle_timeout set to 0.
   127  	r := manual.NewBuilderWithScheme("whatever")
   128  	dopts := []grpc.DialOption{
   129  		grpc.WithTransportCredentials(insecure.NewCredentials()),
   130  		grpc.WithResolvers(r),
   131  		grpc.WithIdleTimeout(0), // Disable idleness.
   132  		grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
   133  	}
   134  	cc, err := grpc.NewClient(r.Scheme()+":///test.server", dopts...)
   135  	if err != nil {
   136  		t.Fatalf("grpc.NewClient() failed: %v", err)
   137  	}
   138  	defer cc.Close()
   139  	cc.Connect()
   140  
   141  	// Start a test backend and push an address update via the resolver.
   142  	backend := stubserver.StartTestService(t, nil)
   143  	defer backend.Stop()
   144  	r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: backend.Address}}})
   145  
   146  	// Verify that the ClientConn moves to READY.
   147  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   148  	defer cancel()
   149  	testutils.AwaitState(ctx, t, cc, connectivity.Ready)
   150  
   151  	// Verify that the ClientConn stays in READY.
   152  	sCtx, sCancel := context.WithTimeout(ctx, 3*defaultTestShortIdleTimeout)
   153  	defer sCancel()
   154  	testutils.AwaitNoStateChange(sCtx, t, cc, connectivity.Ready)
   155  
   156  	// Verify that the LB policy is not closed which is expected to happen when
   157  	// the channel enters IDLE.
   158  	sCtx, sCancel = context.WithTimeout(ctx, defaultTestShortIdleTimeout)
   159  	defer sCancel()
   160  	select {
   161  	case <-sCtx.Done():
   162  	case <-closeCh:
   163  		t.Fatal("LB policy closed when expected not to")
   164  	}
   165  }
   166  
   167  // Tests the case where channel idleness is enabled by passing a small value for
   168  // idle_timeout. Verifies that a READY channel with no RPCs moves to IDLE, and
   169  // the connection to the backend is closed.
   170  func (s) TestChannelIdleness_Enabled_NoActivity(t *testing.T) {
   171  	closeCh := registerWrappedRoundRobinPolicy(t)
   172  
   173  	// Create a ClientConn with a short idle_timeout.
   174  	r := manual.NewBuilderWithScheme("whatever")
   175  	dopts := []grpc.DialOption{
   176  		grpc.WithTransportCredentials(insecure.NewCredentials()),
   177  		grpc.WithResolvers(r),
   178  		grpc.WithIdleTimeout(defaultTestShortIdleTimeout),
   179  		grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
   180  	}
   181  	cc, err := grpc.NewClient(r.Scheme()+":///test.server", dopts...)
   182  	if err != nil {
   183  		t.Fatalf("grpc.NewClient() failed: %v", err)
   184  	}
   185  	defer cc.Close()
   186  
   187  	cc.Connect()
   188  	// Start a test backend and push an address update via the resolver.
   189  	lis := testutils.NewListenerWrapper(t, nil)
   190  	backend := stubserver.StartTestService(t, &stubserver.StubServer{Listener: lis})
   191  	defer backend.Stop()
   192  	r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: backend.Address}}})
   193  
   194  	// Verify that the ClientConn moves to READY.
   195  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   196  	defer cancel()
   197  	testutils.AwaitState(ctx, t, cc, connectivity.Ready)
   198  
   199  	// Retrieve the wrapped conn from the listener.
   200  	v, err := lis.NewConnCh.Receive(ctx)
   201  	if err != nil {
   202  		t.Fatalf("Failed to retrieve conn from test listener: %v", err)
   203  	}
   204  	conn := v.(*testutils.ConnWrapper)
   205  
   206  	// Verify that the ClientConn moves to IDLE as there is no activity.
   207  	testutils.AwaitState(ctx, t, cc, connectivity.Idle)
   208  
   209  	// Verify idleness related channelz events.
   210  	if err := channelzTraceEventFound(ctx, "entering idle mode"); err != nil {
   211  		t.Fatal(err)
   212  	}
   213  
   214  	// Verify that the previously open connection is closed.
   215  	if _, err := conn.CloseCh.Receive(ctx); err != nil {
   216  		t.Fatalf("Failed when waiting for connection to be closed after channel entered IDLE: %v", err)
   217  	}
   218  
   219  	// Verify that the LB policy is closed.
   220  	select {
   221  	case <-ctx.Done():
   222  		t.Fatal("Timeout waiting for LB policy to be closed after the channel enters IDLE")
   223  	case <-closeCh:
   224  	}
   225  }
   226  
   227  // Tests the case where channel idleness is enabled by passing a small value for
   228  // idle_timeout. Verifies that a READY channel with an ongoing RPC stays READY.
   229  func (s) TestChannelIdleness_Enabled_OngoingCall(t *testing.T) {
   230  	tests := []struct {
   231  		name    string
   232  		makeRPC func(ctx context.Context, client testgrpc.TestServiceClient) error
   233  	}{
   234  		{
   235  			name: "unary",
   236  			makeRPC: func(ctx context.Context, client testgrpc.TestServiceClient) error {
   237  				if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
   238  					return fmt.Errorf("EmptyCall RPC failed: %v", err)
   239  				}
   240  				return nil
   241  			},
   242  		},
   243  		{
   244  			name: "streaming",
   245  			makeRPC: func(ctx context.Context, client testgrpc.TestServiceClient) error {
   246  				stream, err := client.FullDuplexCall(ctx)
   247  				if err != nil {
   248  					t.Fatalf("FullDuplexCall RPC failed: %v", err)
   249  				}
   250  				if _, err := stream.Recv(); err != nil && err != io.EOF {
   251  					t.Fatalf("stream.Recv() failed: %v", err)
   252  				}
   253  				return nil
   254  			},
   255  		},
   256  	}
   257  
   258  	for _, test := range tests {
   259  		t.Run(test.name, func(t *testing.T) {
   260  			closeCh := registerWrappedRoundRobinPolicy(t)
   261  
   262  			// Create a ClientConn with a short idle_timeout.
   263  			r := manual.NewBuilderWithScheme("whatever")
   264  			dopts := []grpc.DialOption{
   265  				grpc.WithTransportCredentials(insecure.NewCredentials()),
   266  				grpc.WithResolvers(r),
   267  				grpc.WithIdleTimeout(defaultTestShortIdleTimeout),
   268  				grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
   269  			}
   270  			cc, err := grpc.NewClient(r.Scheme()+":///test.server", dopts...)
   271  			if err != nil {
   272  				t.Fatalf("grpc.NewClient() failed: %v", err)
   273  			}
   274  			defer cc.Close()
   275  			cc.Connect()
   276  			// Start a test backend that keeps the RPC call active by blocking
   277  			// on a channel that is closed by the test later on.
   278  			blockCh := make(chan struct{})
   279  			backend := &stubserver.StubServer{
   280  				EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
   281  					<-blockCh
   282  					return &testpb.Empty{}, nil
   283  				},
   284  				FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
   285  					<-blockCh
   286  					return nil
   287  				},
   288  			}
   289  			if err := backend.StartServer(); err != nil {
   290  				t.Fatalf("Failed to start backend: %v", err)
   291  			}
   292  			defer backend.Stop()
   293  
   294  			// Push an address update containing the address of the above
   295  			// backend via the manual resolver.
   296  			r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: backend.Address}}})
   297  
   298  			// Verify that the ClientConn moves to READY.
   299  			ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   300  			defer cancel()
   301  			testutils.AwaitState(ctx, t, cc, connectivity.Ready)
   302  
   303  			// Spawn a goroutine to check for expected behavior while a blocking
   304  			// RPC all is made from the main test goroutine.
   305  			errCh := make(chan error, 1)
   306  			go func() {
   307  				defer close(blockCh)
   308  
   309  				// Verify that the ClientConn stays in READY.
   310  				sCtx, sCancel := context.WithTimeout(ctx, 3*defaultTestShortIdleTimeout)
   311  				defer sCancel()
   312  				if cc.WaitForStateChange(sCtx, connectivity.Ready) {
   313  					errCh <- fmt.Errorf("state changed from %q to %q when no state change was expected", connectivity.Ready, cc.GetState())
   314  					return
   315  				}
   316  
   317  				// Verify that the LB policy is not closed which is expected to happen when
   318  				// the channel enters IDLE.
   319  				sCtx, sCancel = context.WithTimeout(ctx, defaultTestShortIdleTimeout)
   320  				defer sCancel()
   321  				select {
   322  				case <-sCtx.Done():
   323  				case <-closeCh:
   324  					errCh <- fmt.Errorf("LB policy closed when expected not to")
   325  				}
   326  				errCh <- nil
   327  			}()
   328  
   329  			if err := test.makeRPC(ctx, testgrpc.NewTestServiceClient(cc)); err != nil {
   330  				t.Fatalf("%s rpc failed: %v", test.name, err)
   331  			}
   332  
   333  			select {
   334  			case err := <-errCh:
   335  				if err != nil {
   336  					t.Fatal(err)
   337  				}
   338  			case <-ctx.Done():
   339  				t.Fatalf("Timeout when trying to verify that an active RPC keeps channel from moving to IDLE")
   340  			}
   341  		})
   342  	}
   343  }
   344  
   345  // Tests the case where channel idleness is enabled by passing a small value for
   346  // idle_timeout. Verifies that activity on a READY channel (frequent and short
   347  // RPCs) keeps it from moving to IDLE.
   348  func (s) TestChannelIdleness_Enabled_ActiveSinceLastCheck(t *testing.T) {
   349  	closeCh := registerWrappedRoundRobinPolicy(t)
   350  
   351  	// Create a ClientConn with a short idle_timeout.
   352  	r := manual.NewBuilderWithScheme("whatever")
   353  	dopts := []grpc.DialOption{
   354  		grpc.WithTransportCredentials(insecure.NewCredentials()),
   355  		grpc.WithResolvers(r),
   356  		grpc.WithIdleTimeout(defaultTestShortIdleTimeout),
   357  		grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
   358  	}
   359  	cc, err := grpc.NewClient(r.Scheme()+":///test.server", dopts...)
   360  	if err != nil {
   361  		t.Fatalf("grpc.NewClient() failed: %v", err)
   362  	}
   363  	defer cc.Close()
   364  	cc.Connect()
   365  	// Start a test backend and push an address update via the resolver.
   366  	backend := stubserver.StartTestService(t, nil)
   367  	defer backend.Stop()
   368  	r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: backend.Address}}})
   369  
   370  	// Verify that the ClientConn moves to READY.
   371  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   372  	defer cancel()
   373  	testutils.AwaitState(ctx, t, cc, connectivity.Ready)
   374  
   375  	// For a duration of three times the configured idle timeout, making RPCs
   376  	// every now and then and ensure that the channel does not move out of
   377  	// READY.
   378  	sCtx, sCancel := context.WithTimeout(ctx, 3*defaultTestShortIdleTimeout)
   379  	defer sCancel()
   380  	go func() {
   381  		for ; sCtx.Err() == nil; <-time.After(defaultTestShortIdleTimeout / 4) {
   382  			client := testgrpc.NewTestServiceClient(cc)
   383  			if _, err := client.EmptyCall(sCtx, &testpb.Empty{}); err != nil {
   384  				// While iterating through this for loop, at some point in time,
   385  				// the context deadline will expire. It is safe to ignore that
   386  				// error code.
   387  				if status.Code(err) != codes.DeadlineExceeded {
   388  					t.Errorf("EmptyCall RPC failed: %v", err)
   389  					return
   390  				}
   391  			}
   392  		}
   393  	}()
   394  
   395  	// Verify that the ClientConn stays in READY.
   396  	testutils.AwaitNoStateChange(sCtx, t, cc, connectivity.Ready)
   397  
   398  	// Verify that the LB policy is not closed which is expected to happen when
   399  	// the channel enters IDLE.
   400  	select {
   401  	case <-sCtx.Done():
   402  	case <-closeCh:
   403  		t.Fatal("LB policy closed when expected not to")
   404  	}
   405  }
   406  
   407  // Tests the case where channel idleness is enabled by passing a small value for
   408  // idle_timeout. Verifies that a READY channel with no RPCs moves to IDLE. Also
   409  // verifies that a subsequent RPC on the IDLE channel kicks it out of IDLE.
   410  func (s) TestChannelIdleness_Enabled_ExitIdleOnRPC(t *testing.T) {
   411  	closeCh := registerWrappedRoundRobinPolicy(t)
   412  
   413  	// Start a test backend and set the bootstrap state of the resolver to
   414  	// include this address. This will ensure that when the resolver is
   415  	// restarted when exiting idle, it will push the same address to grpc again.
   416  	r := manual.NewBuilderWithScheme("whatever")
   417  	backend := stubserver.StartTestService(t, nil)
   418  	defer backend.Stop()
   419  	r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: backend.Address}}})
   420  
   421  	// Create a ClientConn with a short idle_timeout.
   422  	dopts := []grpc.DialOption{
   423  		grpc.WithTransportCredentials(insecure.NewCredentials()),
   424  		grpc.WithResolvers(r),
   425  		grpc.WithIdleTimeout(defaultTestShortIdleTimeout),
   426  		grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
   427  	}
   428  	cc, err := grpc.NewClient(r.Scheme()+":///test.server", dopts...)
   429  	if err != nil {
   430  		t.Fatalf("grpc.NewClient() failed: %v", err)
   431  	}
   432  	defer cc.Close()
   433  	cc.Connect()
   434  	// Verify that the ClientConn moves to READY.
   435  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   436  	defer cancel()
   437  	testutils.AwaitState(ctx, t, cc, connectivity.Ready)
   438  
   439  	// Verify that the ClientConn moves to IDLE as there is no activity.
   440  	testutils.AwaitState(ctx, t, cc, connectivity.Idle)
   441  
   442  	// Verify idleness related channelz events.
   443  	if err := channelzTraceEventFound(ctx, "entering idle mode"); err != nil {
   444  		t.Fatal(err)
   445  	}
   446  
   447  	// Verify that the LB policy is closed.
   448  	select {
   449  	case <-ctx.Done():
   450  		t.Fatal("Timeout waiting for LB policy to be closed after the channel enters IDLE")
   451  	case <-closeCh:
   452  	}
   453  
   454  	// Make an RPC and ensure that it succeeds and moves the channel back to
   455  	// READY.
   456  	client := testgrpc.NewTestServiceClient(cc)
   457  	if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
   458  		t.Fatalf("EmptyCall RPC failed: %v", err)
   459  	}
   460  	testutils.AwaitState(ctx, t, cc, connectivity.Ready)
   461  	if err := channelzTraceEventFound(ctx, "exiting idle mode"); err != nil {
   462  		t.Fatal(err)
   463  	}
   464  }
   465  
   466  // Tests the case where channel idleness is enabled by passing a small value for
   467  // idle_timeout. Simulates a race between the idle timer firing and RPCs being
   468  // initiated, after a period of inactivity on the channel.
   469  //
   470  // After a period of inactivity (for the configured idle timeout duration), when
   471  // RPCs are started, there are two possibilities:
   472  //   - the idle timer wins the race and puts the channel in idle. The RPCs then
   473  //     kick it out of idle.
   474  //   - the RPCs win the race, and therefore the channel never moves to idle.
   475  //
   476  // In either of these cases, all RPCs must succeed.
   477  func (s) TestChannelIdleness_Enabled_IdleTimeoutRacesWithRPCs(t *testing.T) {
   478  	// Start a test backend and set the bootstrap state of the resolver to
   479  	// include this address. This will ensure that when the resolver is
   480  	// restarted when exiting idle, it will push the same address to grpc again.
   481  	r := manual.NewBuilderWithScheme("whatever")
   482  	backend := stubserver.StartTestService(t, nil)
   483  	defer backend.Stop()
   484  	r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: backend.Address}}})
   485  
   486  	// Create a ClientConn with a short idle_timeout.
   487  	dopts := []grpc.DialOption{
   488  		grpc.WithTransportCredentials(insecure.NewCredentials()),
   489  		grpc.WithResolvers(r),
   490  		grpc.WithIdleTimeout(defaultTestShortTimeout),
   491  		grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
   492  	}
   493  	cc, err := grpc.NewClient(r.Scheme()+":///test.server", dopts...)
   494  	if err != nil {
   495  		t.Fatalf("grpc.NewClient() failed: %v", err)
   496  	}
   497  	defer cc.Close()
   498  
   499  	// Verify that the ClientConn moves to READY.
   500  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   501  	defer cancel()
   502  	client := testgrpc.NewTestServiceClient(cc)
   503  	if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
   504  		t.Errorf("EmptyCall RPC failed: %v", err)
   505  	}
   506  
   507  	// Make an RPC every defaultTestShortTimeout duration so as to race with the
   508  	// idle timeout. Whether the idle timeout wins the race or the RPC wins the
   509  	// race, RPCs must succeed.
   510  	for i := 0; i < 20; i++ {
   511  		<-time.After(defaultTestShortTimeout)
   512  		if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
   513  			t.Fatalf("EmptyCall RPC failed: %v", err)
   514  		}
   515  		t.Logf("Iteration %d succeeded", i)
   516  	}
   517  }
   518  
   519  // Tests the case where the channel is IDLE and we call cc.Connect.
   520  func (s) TestChannelIdleness_Connect(t *testing.T) {
   521  	// Start a test backend and set the bootstrap state of the resolver to
   522  	// include this address. This will ensure that when the resolver is
   523  	// restarted when exiting idle, it will push the same address to grpc again.
   524  	r := manual.NewBuilderWithScheme("whatever")
   525  	backend := stubserver.StartTestService(t, nil)
   526  	defer backend.Stop()
   527  	r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: backend.Address}}})
   528  
   529  	// Create a ClientConn with a short idle_timeout.
   530  	dopts := []grpc.DialOption{
   531  		grpc.WithTransportCredentials(insecure.NewCredentials()),
   532  		grpc.WithResolvers(r),
   533  		grpc.WithIdleTimeout(defaultTestShortIdleTimeout),
   534  		grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
   535  	}
   536  	cc, err := grpc.NewClient(r.Scheme()+":///test.server", dopts...)
   537  	if err != nil {
   538  		t.Fatalf("grpc.NewClient() failed: %v", err)
   539  	}
   540  	defer cc.Close()
   541  
   542  	// Verify that the ClientConn moves to IDLE.
   543  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   544  	defer cancel()
   545  
   546  	testutils.AwaitState(ctx, t, cc, connectivity.Idle)
   547  
   548  	// Connect should exit channel idleness.
   549  	cc.Connect()
   550  
   551  	// Verify that the ClientConn moves back to READY.
   552  	testutils.AwaitState(ctx, t, cc, connectivity.Ready)
   553  }
   554  
   555  // runFunc runs f repeatedly until the context expires.
   556  func runFunc(ctx context.Context, f func()) {
   557  	for {
   558  		select {
   559  		case <-ctx.Done():
   560  			return
   561  		case <-time.After(10 * time.Millisecond):
   562  			f()
   563  		}
   564  	}
   565  }
   566  
   567  // Tests the scenario where there are concurrent calls to exit and enter idle
   568  // mode on the ClientConn. Verifies that there is no race under this scenario.
   569  func (s) TestChannelIdleness_RaceBetweenEnterAndExitIdleMode(t *testing.T) {
   570  	// Start a test backend and set the bootstrap state of the resolver to
   571  	// include this address. This will ensure that when the resolver is
   572  	// restarted when exiting idle, it will push the same address to grpc again.
   573  	r := manual.NewBuilderWithScheme("whatever")
   574  	backend := stubserver.StartTestService(t, nil)
   575  	defer backend.Stop()
   576  	r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: backend.Address}}})
   577  
   578  	// Create a ClientConn with a long idle_timeout. We will explicitly trigger
   579  	// entering and exiting IDLE mode from the test.
   580  	dopts := []grpc.DialOption{
   581  		grpc.WithTransportCredentials(insecure.NewCredentials()),
   582  		grpc.WithResolvers(r),
   583  		grpc.WithIdleTimeout(30 * time.Minute),
   584  		grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"pick_first":{}}]}`),
   585  	}
   586  	cc, err := grpc.NewClient(r.Scheme()+":///test.server", dopts...)
   587  	if err != nil {
   588  		t.Fatalf("grpc.NewClient() failed: %v", err)
   589  	}
   590  	defer cc.Close()
   591  
   592  	enterIdle := internal.EnterIdleModeForTesting.(func(*grpc.ClientConn))
   593  	enterIdleFunc := func() { enterIdle(cc) }
   594  	exitIdle := internal.ExitIdleModeForTesting.(func(*grpc.ClientConn) error)
   595  	exitIdleFunc := func() {
   596  		if err := exitIdle(cc); err != nil {
   597  			t.Errorf("Failed to exit idle mode: %v", err)
   598  		}
   599  	}
   600  	// Spawn goroutines that call methods on the ClientConn to enter and exit
   601  	// idle mode concurrently for one second.
   602  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
   603  	defer cancel()
   604  	var wg sync.WaitGroup
   605  	wg.Add(4)
   606  	go func() {
   607  		runFunc(ctx, enterIdleFunc)
   608  		wg.Done()
   609  	}()
   610  	go func() {
   611  		runFunc(ctx, enterIdleFunc)
   612  		wg.Done()
   613  	}()
   614  	go func() {
   615  		runFunc(ctx, exitIdleFunc)
   616  		wg.Done()
   617  	}()
   618  	go func() {
   619  		runFunc(ctx, exitIdleFunc)
   620  		wg.Done()
   621  	}()
   622  	wg.Wait()
   623  }