Repository: grpc/grpc-go
File: balancer/pickfirst/pickfirstleaf/pickfirstleaf_ext_test.go (Go)

Code before:

	// Verify that the second server is contacted and 3rd isn't.
	if err := servers[1].awaitContacted(ctx); err != nil {
		t.Fatalf("Server with address %q not contacted: %v", addrs[1], err)
	}

	if got, want := servers[2].isContacted(), false; got != want {
		t.Fatalf("Server[%q].isContacted() = %t, want %t", addrs[2], got, want)
	}
	timerCh <- struct{}{}
	if err := servers[2].awaitContacted(ctx); err != nil {
		t.Fatalf("Server with address %q not contacted: %v", addrs[2], err)
	}

	// First SubConn Fails.
	servers[0].closeConn()

	// No TF should be reported until the first pass is complete.
	shortCtx, shortCancel := context.WithTimeout(ctx, defaultTestShortTimeout)
	defer shortCancel()

	testutils.AwaitNotState(shortCtx, t, cc, connectivity.TransientFailure)

	// Move off the end of the list, pickfirst should still be waiting for TFs
	// to be reported.
	timerCh <- struct{}{}

	// Third SubConn fails.
	servers[2].closeConn()

	testutils.AwaitNotState(shortCtx, t, cc, connectivity.TransientFailure)

	// Last SubConn fails, this should result in a TF update.
	servers[1].closeConn()
	testutils.AwaitState(ctx, t, cc, connectivity.TransientFailure)
}

// TestPickFirstLeaf_HappyEyeballs_TriggerConnectionDelay verifies that
// pickfirst attempts to connect to the second backend once the happy eyeballs
// timer expires.
func (s) TestPickFirstLeaf_HappyEyeballs_TriggerConnectionDelay(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	timerCh := make(chan struct{})
	originalTimer := pfinternal.TimeAfterFunc
	pfinternal.TimeAfterFunc = func(_ time.Duration, f func()) *time.Timer {
		// Set a really long expiration to prevent it from triggering
		// automatically.
		ret := time.AfterFunc(time.Hour, f)
		go func() {

Review comment (suggestion):
This is slightly off the gRFC. Not a major concern in terms of correctness. The gRFC does say that if we are starting a connection to the last address on the list, we shouldn't be starting the timer. But our code does start the timer.

Code after:
	holds[1].Fail(fmt.Errorf("test error"))
	testutils.AwaitState(ctx, t, cc, connectivity.TransientFailure)
}

// Test verifies that pickfirst attempts to connect to the second backend once
// the happy eyeballs timer expires.
func (s) TestPickFirstLeaf_HappyEyeballs_TriggerConnectionDelay(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	originalTimer := pfinternal.TimeAfterFunc
	defer func() {
		pfinternal.TimeAfterFunc = originalTimer
	}()
	triggerTimer, timeAfter := mockTimer()
	pfinternal.TimeAfterFunc = timeAfter

	dialer := testutils.NewBlockingDialer()
	opts := []grpc.DialOption{
		grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"loadBalancingConfig": [{"%s":{}}]}`, pickfirstleaf.Name)),
		grpc.WithContextDialer(dialer.DialContext),
	}
	cc, rb, bm := setupPickFirstLeaf(t, 2, opts...)
	addrs := bm.resolverAddrs()
	holds := bm.holds(dialer)
	rb.UpdateState(resolver.State{Addresses: addrs})
	cc.Connect()

	testutils.AwaitState(ctx, t, cc, connectivity.Connecting)

	// Verify that only the first server is contacted.
	if holds[0].Wait(ctx) != true {
		t.Fatalf("Timeout waiting for server %d with address %q to be contacted", 0, addrs[0])
	}
	if holds[1].IsStarted() != false {
		t.Fatalf("Server %d with address %q contacted unexpectedly", 1, addrs[1])
	}

	// Make the happy eyeballs timer fire once and verify that the
	// second server is contacted.
	triggerTimer()
	if holds[1].Wait(ctx) != true {
		t.Fatalf("Timeout waiting for server %d with address %q to be contacted", 1, addrs[1])
	}

	// Get the connection attempt to the second server to succeed and verify
	// that the channel becomes READY.
	holds[1].Resume()
	testutils.AwaitState(ctx, t, cc, connectivity.Ready)
}