Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Nov 12, 2024
1 parent ec86c8e commit 4514a2c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions p2p/transport/tcpreuse/demultiplex.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
manet "github.com/multiformats/go-multiaddr/net"
)

// This is readiung the first 3 bytes of the packet. It should be instant.
const identifyConnTimeout = 1 * time.Second
// This is reading the first 3 bytes of the packet. It should be instant.
// A var so we can change it in tests.
var identifyConnTimeout = 1 * time.Second

type DemultiplexedConnType int

Expand Down
27 changes: 27 additions & 0 deletions p2p/transport/tcpreuse/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,30 @@ func TestListenerClose(t *testing.T) {
testClose(listenAddr)
}
}

func setDeferReset[T any](t testing.TB, ptr *T, val T) {
t.Helper()
orig := *ptr
*ptr = val
t.Cleanup(func() { *ptr = orig })
}

// TestHitTimeout asserts that we don't panic in case we fail to peek at the connection.
func TestHitTimeout(t *testing.T) {
setDeferReset(t, &identifyConnTimeout, 100*time.Millisecond)
// listen on port 0
cm := NewConnMgr(false, nil, nil)

listenAddr := ma.StringCast("/ip4/127.0.0.1/tcp/0")
ml, err := cm.DemultiplexedListen(listenAddr, DemultiplexedConnType_MultistreamSelect)
require.NoError(t, err)
defer ml.Close()

tcpConn, err := net.Dial(ml.Addr().Network(), ml.Addr().String())
require.NoError(t, err)

// Stall tcp conn for over the timeout.
time.Sleep(identifyConnTimeout + 100*time.Millisecond)

tcpConn.Close()
}

0 comments on commit 4514a2c

Please sign in to comment.