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

Code before:
	holds[1].Resume()

	// Wait for Channel to become READY.
	testutils.AwaitState(ctx, t, cc, connectivity.Ready)

	// Unblock the First SubConn.
	// Since the LB has already closed this subchannel, the context passed to Dial
	// is canceled. This will lead to an inflight attempt to be cancelled.
	// No success or failure metric should be recorded for this.
	holds[0].Resume()

	// --- Assertions ---

	// Wait for the SUCCESS metric to ensure recording logic has processed.
	waitForMetric(ctx, t, tmr, "grpc.subchannel.connection_attempts_succeeded")

	// Verify Success: Exactly 1 (The Winner).
	if got, _ := tmr.Metric("grpc.subchannel.connection_attempts_succeeded"); got != 1 {
		t.Errorf("Unexpected data for metric %v, got: %v, want: 1", "grpc.subchannel.connection_attempts_succeeded", got)
	}

	// Verify Failure: Exactly 0 (The Loser was ignored).
	// We poll briefly to ensure no delayed failure metric appears.
	shortCtx, shortCancel := context.WithTimeout(ctx, 50*time.Millisecond)
	defer shortCancel()
	for shortCtx.Err() == nil {
		if got, _ := tmr.Metric("grpc.subchannel.connection_attempts_failed"); got != 0 {
			t.Fatalf("Unexpected failure recorded for shutdown subchannel, got: %v, want: 0", got)
		}
		time.Sleep(time.Millisecond)
	}

	// LB Metrics Check
	if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_succeeded"); got != 1 {
		t.Errorf("Unexpected data for metric %v, got: %v, want: 1", "grpc.lb.pick_first.connection_attempts_succeeded", got)
	}
	if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_failed"); got != 0 {
		t.Errorf("Unexpected data for metric %v, got: %v, want: 0", "grpc.lb.pick_first.connection_attempts_failed", got)
	}
}

func (s) TestPickFirstLeaf_InterleavingIPV4Preferred(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	cc := testutils.NewBalancerClientConn(t)
	bal := balancer.Get(pfbalancer.Name).Build(cc, balancer.BuildOptions{})
	defer bal.Close()
	ccState := balancer.ClientConnState{
		ResolverState: resolver.State{
			Endpoints: []resolver.Endpoint{
				{Addresses: []resolver.Address{{Addr: "1.1.1.1:1111"}}},

Review comment (suggestion):
This could be shortened as
```golang
for ; sCtx.Err() == nil; <-time.After(time.Millisecond) { ... (without the sleep) }
```

Code after:
	// Wait for Channel to become READY.
	testutils.AwaitState(ctx, t, cc, connectivity.Ready)

	// Unblock the First SubConn.
	// Since the LB has already closed this subchannel, the context passed to Dial
	// is canceled. This will lead to an inflight attempt to be cancelled.
	// No success or failure metric should be recorded for this.
	holds[0].Resume()

	// --- Assertions ---

	// Wait for the SUCCESS metric to ensure recording logic has processed.
	waitForMetric(ctx, t, tmr, "grpc.subchannel.connection_attempts_succeeded")

	// Verify Success: Exactly 1 (The Winner).
	if got, _ := tmr.Metric("grpc.subchannel.connection_attempts_succeeded"); got != 1 {
		t.Errorf("Unexpected data for metric %v, got: %v, want: 1", "grpc.subchannel.connection_attempts_succeeded", got)
	}

	// Verify Failure: Exactly 0 (The Loser was ignored).
	// We poll briefly to ensure no delayed failure metric appears.
	sCtx, sCancel := context.WithTimeout(ctx, 50*time.Millisecond)
	defer sCancel()
	for ; sCtx.Err() == nil; <-time.After(time.Millisecond) {
		if got, _ := tmr.Metric("grpc.subchannel.connection_attempts_failed"); got != 0 {
			t.Fatalf("Unexpected failure recorded for shutdown subchannel, got: %v, want: 0", got)
		}
	}

	// LB Metrics Check
	if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_succeeded"); got != 1 {
		t.Errorf("Unexpected data for metric %v, got: %v, want: 1", "grpc.lb.pick_first.connection_attempts_succeeded", got)
	}
	if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_failed"); got != 0 {
		t.Errorf("Unexpected data for metric %v, got: %v, want: 0", "grpc.lb.pick_first.connection_attempts_failed", got)
	}
}

func (s) TestPickFirstLeaf_InterleavingIPV4Preferred(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	cc := testutils.NewBalancerClientConn(t)
	bal := balancer.Get(pfbalancer.Name).Build(cc, balancer.BuildOptions{})
	defer bal.Close()
	ccState := balancer.ClientConnState{
		ResolverState: resolver.State{
			Endpoints: []resolver.Endpoint{
				{Addresses: []resolver.Address{{Addr: "1.1.1.1:1111"}}},
				{Addresses: []resolver.Address{{Addr: "2.2.2.2:2"}}},
				{Addresses: []resolver.Address{{Addr: "3.3.3.3:3"}}},
				// IPv4-mapped IPv6 address, considered as an IPv4 for