Skip to content

Commit

Permalink
identify: refactor sending of Identify pushes
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jan 13, 2023
1 parent 6bcab9e commit 63a38de
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 415 deletions.
1 change: 1 addition & 0 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ func (h *BasicHost) updateLocalIpAddr() {
func (h *BasicHost) Start() {
h.psManager.Start()
h.refCount.Add(1)
h.ids.Start()
go h.background()
}

Expand Down
17 changes: 9 additions & 8 deletions p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestHostAddrsFactory(t *testing.T) {

addrs := h.Addrs()
if len(addrs) != 1 {
t.Fatalf("expected 1 addr, got %d", len(addrs))
t.Fatalf("expected 1 addr, got %+v", addrs)
}
if !addrs[0].Equal(maddr) {
t.Fatalf("expected %s, got %s", maddr.String(), addrs[0].String())
Expand Down Expand Up @@ -342,14 +342,12 @@ func TestHostProtoMismatch(t *testing.T) {
}

func TestHostProtoPreknowledge(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

h1, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
defer h1.Close()

h2, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
defer h1.Close()
defer h2.Close()

conn := make(chan protocol.ID)
Expand All @@ -362,8 +360,11 @@ func TestHostProtoPreknowledge(t *testing.T) {
// Prevent pushing identify information so this test actually _uses_ the super protocol.
h1.RemoveStreamHandler(identify.IDPush)

h1.Start()
h2.Start()

h2pi := h2.Peerstore().PeerInfo(h2.ID())
require.NoError(t, h1.Connect(ctx, h2pi))
require.NoError(t, h1.Connect(context.Background(), h2pi))

// wait for identify handshake to finish completely
select {
Expand All @@ -380,12 +381,12 @@ func TestHostProtoPreknowledge(t *testing.T) {

h2.SetStreamHandler("/foo", handler)

s, err := h1.NewStream(ctx, h2.ID(), "/foo", "/bar", "/super")
s, err := h1.NewStream(context.Background(), h2.ID(), "/foo", "/bar", "/super")
require.NoError(t, err)

select {
case p := <-conn:
t.Fatal("shouldnt have gotten connection yet, we should have a lazy stream: ", p)
t.Fatal("shouldn't have gotten connection yet, we should have a lazy stream: ", p)
case <-time.After(time.Millisecond * 50):
}

Expand Down
2 changes: 1 addition & 1 deletion p2p/net/mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ func TestLimitedStreams(t *testing.T) {
}

wg.Wait()
if !within(time.Since(before), time.Second*2, time.Second) {
if !within(time.Since(before), time.Second*5/2, time.Second) {
t.Fatal("Expected 2ish seconds but got ", time.Since(before))
}
}
Expand Down
2 changes: 2 additions & 0 deletions p2p/protocol/holepunch/holepunch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ var _ identify.IDService = &mockIDService{}
func newMockIDService(t *testing.T, h host.Host) identify.IDService {
ids, err := identify.NewIDService(h)
require.NoError(t, err)
ids.Start()
t.Cleanup(func() { ids.Close() })
return &mockIDService{IDService: ids}
}

Expand Down
Loading

0 comments on commit 63a38de

Please sign in to comment.