@@ -1935,6 +1935,7 @@ func (s) TestPickFirstLeaf_HappyEyeballs_TF_AfterEndOfList(t *testing.T) {
19351935 holds [1 ].Fail (fmt .Errorf ("test error" ))
19361936 tmr .WaitForInt64CountIncr (ctx , 1 )
19371937 testutils .AwaitState (ctx , t , cc , connectivity .TransientFailure )
1938+ waitForMetric (ctx , t , tmr , "grpc.subchannel.connection_attempts_failed" )
19381939
19391940 // Only connection attempt fails in this test.
19401941 if got , _ := tmr .Metric ("grpc.lb.pick_first.connection_attempts_succeeded" ); got != 0 {
@@ -1946,6 +1947,12 @@ func (s) TestPickFirstLeaf_HappyEyeballs_TF_AfterEndOfList(t *testing.T) {
19461947 if got , _ := tmr .Metric ("grpc.lb.pick_first.disconnections" ); got != 0 {
19471948 t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.lb.pick_first.disconnections" , got , 0 )
19481949 }
1950+ if got , _ := tmr .Metric ("grpc.subchannel.connection_attempts_failed" ); got != 1 {
1951+ t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.subchannel.connection_attempts_failed" , got , 1 )
1952+ }
1953+ if got , _ := tmr .Metric ("grpc.lb.subchannel.connection_attempts_succeeded" ); got != 0 {
1954+ t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.lb.subchannel.connection_attempts_succeeded" , got , 0 )
1955+ }
19491956}
19501957
19511958// Test verifies that pickfirst attempts to connect to the second backend once
@@ -1995,7 +2002,7 @@ func (s) TestPickFirstLeaf_HappyEyeballs_TriggerConnectionDelay(t *testing.T) {
19952002 // that the channel becomes READY.
19962003 holds [1 ].Resume ()
19972004 testutils .AwaitState (ctx , t , cc , connectivity .Ready )
1998-
2005+ waitForMetric ( ctx , t , tmr , "grpc.subchannel.connection_attempts_succeeded" )
19992006 // Only connection attempt successes in this test.
20002007 if got , _ := tmr .Metric ("grpc.lb.pick_first.connection_attempts_succeeded" ); got != 1 {
20012008 t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.lb.pick_first.connection_attempts_succeeded" , got , 1 )
@@ -2006,6 +2013,27 @@ func (s) TestPickFirstLeaf_HappyEyeballs_TriggerConnectionDelay(t *testing.T) {
20062013 if got , _ := tmr .Metric ("grpc.lb.pick_first.disconnections" ); got != 0 {
20072014 t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.lb.pick_first.disconnections" , got , 0 )
20082015 }
2016+
2017+ if got , _ := tmr .Metric ("grpc.subchannel.connection_attempts_succeeded" ); got != 1 {
2018+ t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.subchannel.connection_attempts_succeeded" , got , 1 )
2019+ }
2020+ if got , _ := tmr .Metric ("grpc.subchannel.connection_attempts_failed" ); got != 0 {
2021+ t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.subchannel.connection_attempts_failed" , got , 0 )
2022+ }
2023+ }
2024+
2025+ func waitForMetric (ctx context.Context , t * testing.T , tmr * stats.TestMetricsRecorder , metricName string ) {
2026+ for {
2027+ if _ , ok := tmr .Metric (metricName ); ok {
2028+ break
2029+ }
2030+ select {
2031+ case <- ctx .Done ():
2032+ t .Fatalf ("Timeout waiting for metric emission: %s" , metricName )
2033+ case <- time .After (10 * time .Millisecond ):
2034+ continue
2035+ }
2036+ }
20092037}
20102038
20112039// Test tests the pickfirst balancer by causing a SubConn to fail and then
@@ -2057,6 +2085,9 @@ func (s) TestPickFirstLeaf_HappyEyeballs_TF_ThenTimerFires(t *testing.T) {
20572085 if got , _ := tmr .Metric ("grpc.lb.pick_first.connection_attempts_failed" ); got != 1 {
20582086 t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.lb.pick_first.connection_attempts_failed" , got , 1 )
20592087 }
2088+ if got , _ := tmr .Metric ("grpc.subchannel.connection_attempts_failed" ); got != 1 {
2089+ t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.subchannel.connection_attempts_failed" , got , 1 )
2090+ }
20602091 if holds [2 ].IsStarted () != false {
20612092 t .Fatalf ("Server %d with address %q contacted unexpectedly" , 2 , addrs [2 ])
20622093 }
@@ -2073,7 +2104,10 @@ func (s) TestPickFirstLeaf_HappyEyeballs_TF_ThenTimerFires(t *testing.T) {
20732104 // that the channel becomes READY.
20742105 holds [1 ].Resume ()
20752106 testutils .AwaitState (ctx , t , cc , connectivity .Ready )
2076-
2107+ waitForMetric (ctx , t , tmr , "grpc.subchannel.connection_attempts_succeeded" )
2108+ if got , _ := tmr .Metric ("grpc.subchannel.connection_attempts_succeeded" ); got != 1 {
2109+ t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.subchannel.connection_attempts_succeeded" , got , 1 )
2110+ }
20772111 if got , _ := tmr .Metric ("grpc.lb.pick_first.connection_attempts_succeeded" ); got != 1 {
20782112 t .Errorf ("Unexpected data for metric %v, got: %v, want: %v" , "grpc.lb.pick_first.connection_attempts_succeeded" , got , 1 )
20792113 }
@@ -2082,6 +2116,84 @@ func (s) TestPickFirstLeaf_HappyEyeballs_TF_ThenTimerFires(t *testing.T) {
20822116 }
20832117}
20842118
2119+ // Test verifies that when a subchannel is shut down by the LB (because another
2120+ // subchannel won) while its dial is still in-flight, it records exactly one
2121+ // successful attempt.
2122+ func (s ) TestPickFirstLeaf_HappyEyeballs_Ignore_Inflight_Cancellations (t * testing.T ) {
2123+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
2124+ defer cancel ()
2125+
2126+ originalTimer := pfinternal .TimeAfterFunc
2127+ defer func () {
2128+ pfinternal .TimeAfterFunc = originalTimer
2129+ }()
2130+ triggerTimer , timeAfter := mockTimer ()
2131+ pfinternal .TimeAfterFunc = timeAfter
2132+
2133+ tmr := stats .NewTestMetricsRecorder ()
2134+ dialer := testutils .NewBlockingDialer ()
2135+ opts := []grpc.DialOption {
2136+ grpc .WithDefaultServiceConfig (fmt .Sprintf (`{"loadBalancingConfig": [{"%s":{}}]}` , pfbalancer .Name )),
2137+ grpc .WithContextDialer (dialer .DialContext ),
2138+ grpc .WithStatsHandler (tmr ),
2139+ }
2140+
2141+ // Setup 2 backend addresses
2142+ cc , rb , bm := setupPickFirstLeaf (t , 2 , opts ... )
2143+ addrs := bm .resolverAddrs ()
2144+ holds := bm .holds (dialer )
2145+ rb .UpdateState (resolver.State {Addresses : addrs })
2146+ cc .Connect ()
2147+
2148+ // Make sure we connect to second subconn
2149+ testutils .AwaitState (ctx , t , cc , connectivity .Connecting )
2150+ if holds [0 ].Wait (ctx ) != true {
2151+ t .Fatalf ("Timeout waiting for server %d to be contacted" , 0 )
2152+ }
2153+ triggerTimer ()
2154+ if holds [1 ].Wait (ctx ) != true {
2155+ t .Fatalf ("Timeout waiting for server %d to be contacted" , 1 )
2156+ }
2157+ holds [1 ].Resume ()
2158+
2159+ // Wait for Channel to become READY.
2160+ testutils .AwaitState (ctx , t , cc , connectivity .Ready )
2161+
2162+ // Unblock the First SubConn.
2163+ // Since the LB has already closed this subchannel, the context passed to Dial
2164+ // is canceled. This will lead to an inflight attempt to be cancelled.
2165+ // No success or failure metric should be recorded for this.
2166+ holds [0 ].Resume ()
2167+
2168+ // --- Assertions ---
2169+
2170+ // Wait for the SUCCESS metric to ensure recording logic has processed.
2171+ waitForMetric (ctx , t , tmr , "grpc.subchannel.connection_attempts_succeeded" )
2172+
2173+ // Verify Success: Exactly 1 (The Winner).
2174+ if got , _ := tmr .Metric ("grpc.subchannel.connection_attempts_succeeded" ); got != 1 {
2175+ t .Errorf ("Unexpected data for metric %v, got: %v, want: 1" , "grpc.subchannel.connection_attempts_succeeded" , got )
2176+ }
2177+
2178+ // Verify Failure: Exactly 0 (The Loser was ignored).
2179+ // We poll briefly to ensure no delayed failure metric appears.
2180+ sCtx , sCancel := context .WithTimeout (ctx , 50 * time .Millisecond )
2181+ defer sCancel ()
2182+ for ; sCtx .Err () == nil ; <- time .After (time .Millisecond ) {
2183+ if got , _ := tmr .Metric ("grpc.subchannel.connection_attempts_failed" ); got != 0 {
2184+ t .Fatalf ("Unexpected failure recorded for shutdown subchannel, got: %v, want: 0" , got )
2185+ }
2186+ }
2187+
2188+ // LB Metrics Check
2189+ if got , _ := tmr .Metric ("grpc.lb.pick_first.connection_attempts_succeeded" ); got != 1 {
2190+ t .Errorf ("Unexpected data for metric %v, got: %v, want: 1" , "grpc.lb.pick_first.connection_attempts_succeeded" , got )
2191+ }
2192+ if got , _ := tmr .Metric ("grpc.lb.pick_first.connection_attempts_failed" ); got != 0 {
2193+ t .Errorf ("Unexpected data for metric %v, got: %v, want: 0" , "grpc.lb.pick_first.connection_attempts_failed" , got )
2194+ }
2195+ }
2196+
20852197func (s ) TestPickFirstLeaf_InterleavingIPV4Preferred (t * testing.T ) {
20862198 ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
20872199 defer cancel ()
0 commit comments