Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Oct 23, 2024
1 parent d069252 commit da2e83d
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package basichost

import (
"context"
"encoding/binary"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -941,3 +942,54 @@ func TestTrimHostAddrList(t *testing.T) {
})
}
}

func TestHostTimeoutNewStream(t *testing.T) {
h1, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
h1.Start()
defer h1.Close()

const proto = "/testing"
h2 := swarmt.GenSwarm(t)

h2.SetStreamHandler(func(s network.Stream) {
// First message is multistream header. Just echo it
msHeader := []byte("\x19/multistream/1.0.0\n")
_, err := s.Read(msHeader)
assert.NoError(t, err)
_, err = s.Write(msHeader)
assert.NoError(t, err)

buf := make([]byte, 1024)
n, err := s.Read(buf)
assert.NoError(t, err)

msgLen, varintN := binary.Uvarint(buf[:n])
proto := buf[varintN : varintN+int(msgLen)]
if string(proto) == "/ipfs/id/1.0.0\n" {
// Signal we don't support identify
na := []byte("na\n")
n := binary.PutUvarint(buf, uint64(len(na)))
copy(buf[n:], na)

_, err = s.Write(buf[:int(n)+len(na)])
assert.NoError(t, err)
} else {
// Stall
time.Sleep(5 * time.Second)
t.Log("Resetting")
}
s.Reset()
})

h1.Connect(context.Background(), peer.AddrInfo{
ID: h2.LocalPeer(),
Addrs: h2.ListenAddresses(),
})

// No context passed in, fallback to negtimeout
h1.negtimeout = time.Second
_, err = h1.NewStream(context.Background(), h2.LocalPeer(), proto)
require.Error(t, err)
require.ErrorContains(t, err, "context deadline exceeded")
}

0 comments on commit da2e83d

Please sign in to comment.