Skip to content

Commit

Permalink
Fix flaky TestPeerScoreOnNewConnection
Browse files Browse the repository at this point in the history
Test was failing with "Failed to add connection to peer" due to the
connection being in an invalid state.

Similar to #433, the test ended before the connection was added
on both sides, causing the error log. Fix is similar, to use ping
to ensure the connection is added on both sides rather than ending
the test while the connection is still being added.
  • Loading branch information
prashantv committed Jul 15, 2016
1 parent 13c0e2d commit 99cdc76
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions peer_test.go
Expand Up @@ -968,9 +968,6 @@ func TestPeerSelectionAfterClosed(t *testing.T) {
}

func TestPeerScoreOnNewConnection(t *testing.T) {
ctx, cancel := NewContext(time.Second)
defer cancel()

tests := []struct {
message string
connect func(s1, s2 *Channel) *Peer
Expand All @@ -997,6 +994,9 @@ func TestPeerScoreOnNewConnection(t *testing.T) {

for _, tt := range tests {
testutils.WithTestServer(t, nil, func(ts *testutils.TestServer) {
ctx, cancel := NewContext(time.Second)
defer cancel()

s1 := ts.Server()
s2 := ts.NewServer(nil)

Expand All @@ -1005,7 +1005,7 @@ func TestPeerScoreOnNewConnection(t *testing.T) {

initialScore := getScore(s1.Peers())
peer := tt.connect(s1, s2)
_, err := peer.GetConnection(ctx)
conn, err := peer.GetConnection(ctx)
require.NoError(t, err, "%v: GetConnection failed", tt.message)

// When receiving an inbound connection, the outbound connect may return
Expand All @@ -1015,6 +1015,9 @@ func TestPeerScoreOnNewConnection(t *testing.T) {
return connectedScore < initialScore
}), "%v: Expected connected peer score %v to be less than initial score %v",
tt.message, getScore(s1.Peers()), initialScore)

// Ping to ensure the connection has been added to peers on both sides.
require.NoError(t, conn.Ping(ctx), "%v: Ping failed", tt.message)
})
}
}
Expand Down

0 comments on commit 99cdc76

Please sign in to comment.