Skip to content

Commit

Permalink
Fix flaky natpmp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jun 30, 2023
1 parent e7d4246 commit 93c515d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
5 changes: 4 additions & 1 deletion internal/natpmp/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ func launchUDPServer(t *testing.T, exchanges []udpExchange) (

if exchange.close {
err = conn.Close()
assert.NoError(t, err)
if !errors.Is(err, net.ErrClosed) {
// connection might be already closed by client production code
assert.NoError(t, err)
}
return
}

Expand Down
10 changes: 7 additions & 3 deletions internal/natpmp/portmapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ func Test_Client_AddPortMapping(t *testing.T) {
assert.Equal(t, testCase.assignedInternalPort, assignedInternalPort)
assert.Equal(t, testCase.assignedExternalPort, assignedExternalPort)
assert.Equal(t, testCase.assignedLifetime, assignedLifetime)
assert.ErrorIs(t, err, testCase.err)
if testCase.err != nil {
assert.EqualError(t, err, testCase.errMessage)
if testCase.errMessage != "" {
if testCase.err != nil {
assert.ErrorIs(t, err, testCase.err)
}
assert.Regexp(t, "^"+testCase.errMessage+"$", err.Error())
} else {
assert.NoError(t, err)
}
})
}
Expand Down
17 changes: 10 additions & 7 deletions internal/natpmp/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_Client_rpc(t *testing.T) {
Expand Down Expand Up @@ -51,9 +50,11 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0, 1},
initialRetry: time.Millisecond,
exchanges: []udpExchange{{close: true}},
err: ErrConnectionTimeout,
errMessage: "connection timeout: after 1ms",
exchanges: []udpExchange{
{request: []byte{0, 1}, close: true},
},
err: ErrConnectionTimeout,
errMessage: "connection timeout: after 1ms",
},
"response_too_small": {
ctx: context.Background(),
Expand Down Expand Up @@ -152,10 +153,12 @@ func Test_Client_rpc(t *testing.T) {
testCase.request, testCase.responseSize)

if testCase.errMessage != "" {
require.Error(t, err)
assert.Regexp(t, testCase.errMessage, err.Error())
if testCase.err != nil {
assert.ErrorIs(t, err, testCase.err)
}
assert.Regexp(t, "^"+testCase.errMessage+"$", err.Error())
} else {
assert.ErrorIs(t, err, testCase.err)
assert.NoError(t, err)
}
assert.Equal(t, testCase.expectedResponse, response)
})
Expand Down

0 comments on commit 93c515d

Please sign in to comment.