From 3ff901834683461e850e737ae941dafff68131eb Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Wed, 6 Nov 2024 19:02:35 -0500 Subject: [PATCH] fix: fix HappyEyeballs test race --- transport/happyeyeballs_test.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/transport/happyeyeballs_test.go b/transport/happyeyeballs_test.go index d4fd24ea..a55baae3 100644 --- a/transport/happyeyeballs_test.go +++ b/transport/happyeyeballs_test.go @@ -226,8 +226,26 @@ func TestHappyEyeballsStreamDialer_DialStream(t *testing.T) { }) t.Run("IP order", func(t *testing.T) { + waitForIPv4Ch := make(chan struct{}) dialFailErr := errors.New("failed to dial") - baseDialer := collectStreamDialer{Dialer: newErrorStreamDialer(dialFailErr)} + baseDialer := collectStreamDialer{Dialer: FuncStreamDialer(func(ctx context.Context, addr string) (StreamConn, error) { + ap, err := netip.ParseAddrPort(addr) + require.NoError(t, err) + if ap.Addr().Is4() { + select { + case <-waitForIPv4Ch: + // Avoids closing a closed channel. + default: + close(waitForIPv4Ch) + } + } else { + // We block IPv6 dials until we have IPv4 results. Otherwise all IPv6 dials may proceed before the IPv4 results + // are processed, resulting on a race condition. + <-waitForIPv4Ch + } + time.Sleep(time.Microsecond) + return nil, dialFailErr + })} dialer := HappyEyeballsStreamDialer{ Dialer: &baseDialer, Resolve: NewParallelHappyEyeballsResolveFunc(