Skip to content

Commit

Permalink
transport tests: add deadline tests (#2286)
Browse files Browse the repository at this point in the history
* Add deadline test

* Test read/write deadlines

* Add SetDeadline test

* Skip mplex test for now

* Rename fake proto

* Sleep for an hour

* Add comment

* Update Yamux dep

* Skip WebSocket

* Skip WebSocket and Yamux

* Cleanup

* Bump timings up

* Close nodes. Remove mplex

* Use net.Error cast
  • Loading branch information
MarcoPolo authored Aug 19, 2023
1 parent a2591e7 commit ddc7238
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions p2p/test/transport/deadline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package transport_integration

import (
"context"
"net"
"testing"
"time"

"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
)

func TestReadWriteDeadlines(t *testing.T) {
// Send a lot of data so that writes have to flush (can't just buffer it all)
sendBuf := make([]byte, 10<<20)
for _, tc := range transportsToTest {
t.Run(tc.Name, func(t *testing.T) {
listener := tc.HostGenerator(t, TransportTestCaseOpts{})
defer listener.Close()
dialer := tc.HostGenerator(t, TransportTestCaseOpts{NoListen: true})
defer dialer.Close()

require.NoError(t, dialer.Connect(context.Background(), peer.AddrInfo{
ID: listener.ID(),
Addrs: listener.Addrs(),
}))

// This simply stalls
listener.SetStreamHandler("/stall", func(s network.Stream) {
time.Sleep(time.Hour)
s.Close()
})

t.Run("ReadDeadline", func(t *testing.T) {
s, err := dialer.NewStream(context.Background(), listener.ID(), "/stall")
require.NoError(t, err)
defer s.Close()

start := time.Now()
// Set a deadline
s.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
buf := make([]byte, 1)
_, err = s.Read(buf)
require.Error(t, err)
require.True(t, err.(net.Error).Timeout())
require.Less(t, time.Since(start), 1*time.Second)
})

t.Run("WriteDeadline", func(t *testing.T) {
s, err := dialer.NewStream(context.Background(), listener.ID(), "/stall")
require.NoError(t, err)
defer s.Close()

// Set a deadline
s.SetWriteDeadline(time.Now().Add(10 * time.Millisecond))
start := time.Now()
_, err = s.Write(sendBuf)
require.Error(t, err)
require.True(t, err.(net.Error).Timeout())
require.Less(t, time.Since(start), 1*time.Second)
})

// Like the above, but with SetDeadline
t.Run("SetDeadline", func(t *testing.T) {
for _, op := range []string{"Read", "Write"} {
t.Run(op, func(t *testing.T) {
s, err := dialer.NewStream(context.Background(), listener.ID(), "/stall")
require.NoError(t, err)
defer s.Close()

// Set a deadline
s.SetDeadline(time.Now().Add(10 * time.Millisecond))
start := time.Now()

if op == "Read" {
buf := make([]byte, 1)
_, err = s.Read(buf)
} else {
_, err = s.Write(sendBuf)
}
require.Error(t, err)
require.True(t, err.(net.Error).Timeout())
require.Less(t, time.Since(start), 1*time.Second)
})
}
})
})
}
}

0 comments on commit ddc7238

Please sign in to comment.