Skip to content

Commit

Permalink
tests: add a stream read deadline transport test
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Mar 24, 2023
1 parent 950151e commit 4b784f0
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions p2p/test/transport/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"crypto/rand"
"fmt"
"io"
"net"
"sync"
"testing"
"time"

"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/config"
Expand Down Expand Up @@ -326,3 +328,39 @@ func TestDialerStreamResets(t *testing.T) {
})
}
}

func TestStreamReadDeadline(t *testing.T) {
for _, tc := range transportsToTest {
t.Run(tc.Name, func(t *testing.T) {
h1 := tc.HostGenerator(t, TransportTestCaseOpts{})
h2 := tc.HostGenerator(t, TransportTestCaseOpts{NoListen: true})

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

h1.SetStreamHandler("echo", func(s network.Stream) {
io.Copy(s, s)
})

s, err := h2.NewStream(context.Background(), h1.ID(), "echo")
require.NoError(t, err)
require.NoError(t, s.SetReadDeadline(time.Now().Add(100*time.Millisecond)))
_, err = s.Read([]byte{0})
require.Error(t, err)
require.Contains(t, err.Error(), "deadline")
nerr, ok := err.(net.Error)
require.True(t, ok, "expected a net.Error")
require.True(t, nerr.Timeout(), "expected net.Error.Timeout() == true")
// now test that the stream is still usable
s.SetReadDeadline(time.Time{})
_, err = s.Write([]byte("foobar"))
require.NoError(t, err)
b := make([]byte, 6)
_, err = s.Read(b)
require.Equal(t, "foobar", string(b))
require.NoError(t, err)
})
}
}

0 comments on commit 4b784f0

Please sign in to comment.