Skip to content

Commit

Permalink
net_util_test: fix and test reconnect retry logic (#107)
Browse files Browse the repository at this point in the history
This commit fixes the reconnect retry logic that was added with:
#106. The issue was that the
arguments to errors.Is() were switched.
  • Loading branch information
charlievieth authored Sep 21, 2020
1 parent b9fe4c6 commit d74c79b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions net_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,12 @@ func testNetSinkReconnect(t *testing.T, protocol string) {
// This test is flaky with UDP and the race detector, but good
// to have so we log instead of fail the test.
if protocol == "udp" {
stat := ts.WaitForStat(replaceFatalWithLog{t}, defaultRetryInterval*2)
stat := ts.WaitForStat(replaceFatalWithLog{t}, defaultRetryInterval*3)
if stat != "" && stat != expected {
t.Fatalf("stats got: %q want: %q", stat, expected)
}
} else {
stat := ts.WaitForStat(t, defaultRetryInterval*2)
stat := ts.WaitForStat(t, defaultRetryInterval*3)
if stat != expected {
t.Fatalf("stats got: %q want: %q", stat, expected)
}
Expand Down
78 changes: 77 additions & 1 deletion net_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func reconnectRetry(t testing.TB, fn func() error) {
// Retry if the error is due to the address being in use.
// On slow systems (CI) it can take awhile for the OS to
// realize the address is not in use.
if errors.Is(syscall.EADDRINUSE, err) {
if errors.Is(err, syscall.EADDRINUSE) {
time.Sleep(Retry)
} else {
t.Fatalf("unexpected error reconnecting: %s", err)
Expand All @@ -357,3 +357,79 @@ func reconnectRetry(t testing.TB, fn func() error) {
t.Fatalf("failed to reconnect after %d attempts and %s: %v",
N, Timeout, err)
}

func TestReconnectRetryTCP(t *testing.T) {
t.Parallel()

l1, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 0,
})
if err != nil {
t.Fatal(err)
}
defer l1.Close()

l2, err := net.ListenTCP(l1.Addr().Network(), l1.Addr().(*net.TCPAddr))
if err == nil {
l2.Close()
t.Fatal("expected an error got nil")
}

if !errors.Is(err, syscall.EADDRINUSE) {
t.Fatalf("expected error to wrap %T got: %#v", syscall.EADDRINUSE, err)
}

first := true
callCount := 0
reconnectRetry(t, func() error {
callCount++
if first {
first = false
return err
}
return nil
})

if callCount != 2 {
t.Errorf("Expected call cound to be %d got: %d", 2, callCount)
}
}

func TestReconnectRetryUDP(t *testing.T) {
t.Parallel()

l1, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 0,
})
if err != nil {
t.Fatal(err)
}
defer l1.Close()

l2, err := net.ListenUDP(l1.LocalAddr().Network(), l1.LocalAddr().(*net.UDPAddr))
if err == nil {
l2.Close()
t.Fatal("expected an error got nil")
}

if !errors.Is(err, syscall.EADDRINUSE) {
t.Fatalf("expected error to wrap %T got: %#v", syscall.EADDRINUSE, err)
}

first := true
callCount := 0
reconnectRetry(t, func() error {
callCount++
if first {
first = false
return err
}
return nil
})

if callCount != 2 {
t.Errorf("Expected call cound to be %d got: %d", 2, callCount)
}
}

0 comments on commit d74c79b

Please sign in to comment.