Skip to content

Commit

Permalink
fix: fix HappyEyeballs test race
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna committed Nov 7, 2024
1 parent ed096db commit 3ff9018
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion transport/happyeyeballs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3ff9018

Please sign in to comment.