Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic_host: Fix flaky tests #2136

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func getHostPair(t *testing.T) (host.Host, host.Host) {
require.NoError(t, err)
h2.Start()

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
h2pi := h2.Peerstore().PeerInfo(h2.ID())
require.NoError(t, h1.Connect(ctx, h2pi))
Expand Down Expand Up @@ -344,11 +344,11 @@ func TestHostProtoMismatch(t *testing.T) {
}

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

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

Expand All @@ -359,15 +359,23 @@ func TestHostProtoPreknowledge(t *testing.T) {
}

h2.SetStreamHandler("/super", handler)
// Prevent pushing identify information so this test actually _uses_ the super protocol.
h1.RemoveStreamHandler(identify.IDPush)

h1.Start()
h2.Start()

// Prevent pushing identify information so this test actually _uses_ the super protocol.
h1.RemoveStreamHandler(identify.IDPush)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why after the Start? This will cause an Identify Push to be sent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start in identify is what sets the stream handler. Removing the stream handler before start is no-op since it's not even registered.

Identify won't do anything until these nodes are connected.


h2pi := h2.Peerstore().PeerInfo(h2.ID())
// Filter to only 1 address so that we don't have to think about parallel
// connections in this test
h2pi.Addrs = h2pi.Addrs[:1]
require.NoError(t, h1.Connect(context.Background(), h2pi))

// This test implicitly relies on 1 connection. If a background identify
// completes after we set the stream handler below things break
require.Equal(t, 1, len(h1.Network().ConnsToPeer(h2.ID())))

// wait for identify handshake to finish completely
select {
case <-h1.ids.IdentifyWait(h1.Network().ConnsToPeer(h2.ID())[0]):
Expand All @@ -383,6 +391,18 @@ func TestHostProtoPreknowledge(t *testing.T) {

h2.SetStreamHandler("/foo", handler)

require.Never(t, func() bool {
protos, err := h1.Peerstore().GetProtocols(h2.ID())
require.NoError(t, err)
for _, p := range protos {
fmt.Println("proto: ", p)
if p == "/foo" {
return true
}
}
return false
}, time.Second, 100*time.Millisecond)

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

Expand Down