Skip to content

Commit

Permalink
npm: tests: Fixed testing gotchas (#32190)
Browse files Browse the repository at this point in the history
  • Loading branch information
guyarb authored Dec 18, 2024
1 parent 26052f9 commit 9f73ec4
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions pkg/network/tracer/tracer_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (s *TracerSuite) TestTCPRemoveEntries() {
defer c2.Close()

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

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

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

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

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

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

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

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

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

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

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

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

assert.Equal(t, clientMessageSize, int(outgoing.Monotonic.SentBytes), "outgoing sent")
assert.Equal(t, serverMessageSize, int(outgoing.Monotonic.RecvBytes), "outgoing recv")
assert.True(t, outgoing.IntraHost, "outgoing intrahost")
assert.Equal(ct, clientMessageSize, int(outgoing.Monotonic.SentBytes), "outgoing sent")
assert.Equal(ct, serverMessageSize, int(outgoing.Monotonic.RecvBytes), "outgoing recv")
assert.True(ct, outgoing.IntraHost, "outgoing intrahost")
}
}, 3*time.Second, 100*time.Millisecond)

Expand Down Expand Up @@ -1660,16 +1660,17 @@ func (s *TracerSuite) TestSendfileRegression() {

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

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

require.NoError(t, c.Close(), "error closing client connection")
require.EventuallyWithT(t, func(collect *assert.CollectT) {
conn, found := findConnection(c.LocalAddr(), srv.Addr(), getConnections(t, tr))
conn, found := findConnection(c.LocalAddr(), srv.Addr(), getConnections(collect, tr))
require.True(collect, found, "could not find connection")
require.True(collect, conn.IsClosed, "connection should be closed")
// after closing the client connection, the duration should be
Expand Down

0 comments on commit 9f73ec4

Please sign in to comment.