Skip to content

Commit 9f73ec4

Browse files
authored
npm: tests: Fixed testing gotchas (#32190)
1 parent 26052f9 commit 9f73ec4

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

pkg/network/tracer/tracer_linux_test.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (s *TracerSuite) TestTCPRemoveEntries() {
109109
defer c2.Close()
110110

111111
assert.EventuallyWithT(t, func(ct *assert.CollectT) {
112-
conn, ok := findConnection(c2.LocalAddr(), c2.RemoteAddr(), getConnections(t, tr))
112+
conn, ok := findConnection(c2.LocalAddr(), c2.RemoteAddr(), getConnections(ct, tr))
113113
if !assert.True(ct, ok) {
114114
return
115115
}
@@ -123,9 +123,9 @@ func (s *TracerSuite) TestTCPRemoveEntries() {
123123
}, 3*time.Second, 100*time.Millisecond)
124124

125125
// Make sure the first connection got cleaned up
126-
assert.Eventually(t, func() bool {
127-
_, ok := findConnection(c.LocalAddr(), c.RemoteAddr(), getConnections(t, tr))
128-
return !ok
126+
assert.EventuallyWithT(t, func(ct *assert.CollectT) {
127+
_, ok := findConnection(c.LocalAddr(), c.RemoteAddr(), getConnections(ct, tr))
128+
require.False(ct, ok)
129129
}, 5*time.Second, 100*time.Millisecond)
130130

131131
}
@@ -735,10 +735,10 @@ func (s *TracerSuite) TestGatewayLookupEnabled() {
735735
dnsServerAddr := &net.UDPAddr{IP: dnsAddr, Port: 53}
736736

737737
var conn *network.ConnectionStats
738-
require.Eventually(t, func() bool {
738+
require.EventuallyWithT(t, func(ct *assert.CollectT) {
739739
var ok bool
740-
conn, ok = findConnection(dnsClientAddr, dnsServerAddr, getConnections(t, tr))
741-
return ok
740+
conn, ok = findConnection(dnsClientAddr, dnsServerAddr, getConnections(ct, tr))
741+
require.True(ct, ok, "connection not found")
742742
}, 3*time.Second, 100*time.Millisecond)
743743

744744
require.NotNil(t, conn.Via, "connection is missing via: %s", conn)
@@ -791,10 +791,10 @@ func (s *TracerSuite) TestGatewayLookupSubnetLookupError() {
791791
dnsClientAddr := &net.UDPAddr{IP: net.ParseIP(clientIP), Port: clientPort}
792792
dnsServerAddr := &net.UDPAddr{IP: destAddr, Port: 53}
793793
var c *network.ConnectionStats
794-
require.Eventually(t, func() bool {
794+
require.EventuallyWithT(t, func(ct *assert.CollectT) {
795795
var ok bool
796-
c, ok = findConnection(dnsClientAddr, dnsServerAddr, getConnections(t, tr))
797-
return ok
796+
c, ok = findConnection(dnsClientAddr, dnsServerAddr, getConnections(ct, tr))
797+
require.True(ct, ok, "connection not found")
798798
}, 3*time.Second, 100*time.Millisecond, "connection not found")
799799
require.Nil(t, c.Via)
800800

@@ -804,10 +804,10 @@ func (s *TracerSuite) TestGatewayLookupSubnetLookupError() {
804804
}, 6*time.Second, 100*time.Millisecond, "failed to send dns query")
805805

806806
dnsClientAddr = &net.UDPAddr{IP: net.ParseIP(clientIP), Port: clientPort}
807-
require.Eventually(t, func() bool {
807+
require.EventuallyWithT(t, func(ct *assert.CollectT) {
808808
var ok bool
809-
c, ok = findConnection(dnsClientAddr, dnsServerAddr, getConnections(t, tr))
810-
return ok
809+
c, ok = findConnection(dnsClientAddr, dnsServerAddr, getConnections(ct, tr))
810+
require.True(ct, ok, "connection not found")
811811
}, 3*time.Second, 100*time.Millisecond, "connection not found")
812812
require.Nil(t, c.Via)
813813

@@ -1529,25 +1529,25 @@ func testUDPReusePort(t *testing.T, udpnet string, ip string) {
15291529

15301530
assert.EventuallyWithT(t, func(ct *assert.CollectT) { //nolint:revive // TODO
15311531
// use t instead of ct because getConnections uses require (not assert), and we get a better error message that way
1532-
connections := getConnections(t, tr)
1532+
connections := getConnections(ct, tr)
15331533

15341534
incoming, ok := findConnection(c.RemoteAddr(), c.LocalAddr(), connections)
1535-
if assert.True(t, ok, "unable to find incoming connection") {
1536-
assert.Equal(t, network.INCOMING, incoming.Direction)
1535+
if assert.True(ct, ok, "unable to find incoming connection") {
1536+
assert.Equal(ct, network.INCOMING, incoming.Direction)
15371537

15381538
// make sure the inverse values are seen for the other message
1539-
assert.Equal(t, serverMessageSize, int(incoming.Monotonic.SentBytes), "incoming sent")
1540-
assert.Equal(t, clientMessageSize, int(incoming.Monotonic.RecvBytes), "incoming recv")
1541-
assert.True(t, incoming.IntraHost, "incoming intrahost")
1539+
assert.Equal(ct, serverMessageSize, int(incoming.Monotonic.SentBytes), "incoming sent")
1540+
assert.Equal(ct, clientMessageSize, int(incoming.Monotonic.RecvBytes), "incoming recv")
1541+
assert.True(ct, incoming.IntraHost, "incoming intrahost")
15421542
}
15431543

15441544
outgoing, ok := findConnection(c.LocalAddr(), c.RemoteAddr(), connections)
1545-
if assert.True(t, ok, "unable to find outgoing connection") {
1546-
assert.Equal(t, network.OUTGOING, outgoing.Direction)
1545+
if assert.True(ct, ok, "unable to find outgoing connection") {
1546+
assert.Equal(ct, network.OUTGOING, outgoing.Direction)
15471547

1548-
assert.Equal(t, clientMessageSize, int(outgoing.Monotonic.SentBytes), "outgoing sent")
1549-
assert.Equal(t, serverMessageSize, int(outgoing.Monotonic.RecvBytes), "outgoing recv")
1550-
assert.True(t, outgoing.IntraHost, "outgoing intrahost")
1548+
assert.Equal(ct, clientMessageSize, int(outgoing.Monotonic.SentBytes), "outgoing sent")
1549+
assert.Equal(ct, serverMessageSize, int(outgoing.Monotonic.RecvBytes), "outgoing recv")
1550+
assert.True(ct, outgoing.IntraHost, "outgoing intrahost")
15511551
}
15521552
}, 3*time.Second, 100*time.Millisecond)
15531553

@@ -1660,16 +1660,17 @@ func (s *TracerSuite) TestSendfileRegression() {
16601660

16611661
t.Logf("looking for connections %+v <-> %+v", c.LocalAddr(), c.RemoteAddr())
16621662
var outConn, inConn *network.ConnectionStats
1663-
assert.Eventually(t, func() bool {
1664-
conns := getConnections(t, tr)
1663+
assert.EventuallyWithT(t, func(ct *assert.CollectT) {
1664+
conns := getConnections(ct, tr)
16651665
t.Log(conns)
16661666
if outConn == nil {
16671667
outConn = network.FirstConnection(conns, network.ByType(connType), network.ByFamily(family), network.ByTuple(c.LocalAddr(), c.RemoteAddr()))
16681668
}
16691669
if inConn == nil {
16701670
inConn = network.FirstConnection(conns, network.ByType(connType), network.ByFamily(family), network.ByTuple(c.RemoteAddr(), c.LocalAddr()))
16711671
}
1672-
return outConn != nil && inConn != nil
1672+
require.NotNil(ct, outConn)
1673+
require.NotNil(ct, inConn)
16731674
}, 3*time.Second, 100*time.Millisecond, "couldn't find connections used by sendfile(2)")
16741675

16751676
if assert.NotNil(t, outConn, "couldn't find outgoing connection used by sendfile(2)") {
@@ -2433,7 +2434,7 @@ LOOP:
24332434

24342435
require.NoError(t, c.Close(), "error closing client connection")
24352436
require.EventuallyWithT(t, func(collect *assert.CollectT) {
2436-
conn, found := findConnection(c.LocalAddr(), srv.Addr(), getConnections(t, tr))
2437+
conn, found := findConnection(c.LocalAddr(), srv.Addr(), getConnections(collect, tr))
24372438
require.True(collect, found, "could not find connection")
24382439
require.True(collect, conn.IsClosed, "connection should be closed")
24392440
// after closing the client connection, the duration should be

0 commit comments

Comments
 (0)