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