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

fix (and speed up) flaky TestBackoffConnector test #1316

Merged
merged 1 commit into from Feb 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 19 additions & 32 deletions p2p/discovery/backoff/backoffconnector_test.go
Expand Up @@ -7,10 +7,13 @@ import (
"testing"
"time"

bhost "github.com/libp2p/go-libp2p-blankhost"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"

bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"

"github.com/stretchr/testify/require"
)

type maxDialHost struct {
Expand All @@ -37,7 +40,7 @@ func (h *maxDialHost) Connect(ctx context.Context, ai peer.AddrInfo) error {
return h.Host.Connect(ctx, ai)
}

func getNetHosts(t *testing.T, ctx context.Context, n int) []host.Host {
func getNetHosts(t *testing.T, n int) []host.Host {
var out []host.Host

for i := 0; i < n; i++ {
Expand All @@ -60,50 +63,34 @@ func loadCh(peers []host.Host) <-chan peer.AddrInfo {
}

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

hosts := getNetHosts(t, ctx, 5)
hosts := getNetHosts(t, 5)
primary := &maxDialHost{
Host: hosts[0],
mux: sync.Mutex{},
timesDialed: make(map[peer.ID]int),
maxTimesToDial: map[peer.ID]int{
hosts[1].ID(): 1,
hosts[2].ID(): 2,
},
}

bc, err := NewBackoffConnector(primary, 10, time.Minute, NewFixedBackoff(time.Millisecond*1500))
if err != nil {
t.Fatal(err)
}
bc, err := NewBackoffConnector(primary, 10, time.Minute, NewFixedBackoff(250*time.Millisecond))
require.NoError(t, err)

bc.Connect(ctx, loadCh(hosts))

time.Sleep(time.Millisecond * 100)
if expected, actual := len(hosts)-1, len(primary.Network().Conns()); actual != expected {
t.Fatalf("wrong number of connections. expected %d, actual %d", expected, actual)
}
bc.Connect(context.Background(), loadCh(hosts))
require.Eventually(t, func() bool { return len(primary.Network().Conns()) == len(hosts)-1 }, 3*time.Second, 10*time.Millisecond)

for _, c := range primary.Network().Conns() {
c.Close()
}
require.Eventually(t, func() bool { return len(primary.Network().Conns()) == 0 }, 3*time.Second, 10*time.Millisecond)

for len(primary.Network().Conns()) > 0 {
time.Sleep(time.Millisecond * 100)
}

bc.Connect(ctx, loadCh(hosts))
if numConns := len(primary.Network().Conns()); numConns != 0 {
t.Fatal("shouldn't be connected to any peers")
}

time.Sleep(time.Millisecond * 1600)
bc.Connect(ctx, loadCh(hosts))
bc.Connect(context.Background(), loadCh(hosts))
require.Empty(t, primary.Network().Conns(), "shouldn't be connected to any peers")

time.Sleep(time.Millisecond * 100)
if expected, actual := len(hosts)-2, len(primary.Network().Conns()); actual != expected {
t.Fatalf("wrong number of connections. expected %d, actual %d", expected, actual)
}
time.Sleep(time.Millisecond * 500)
bc.Connect(context.Background(), loadCh(hosts))
require.Eventually(t, func() bool { return len(primary.Network().Conns()) == len(hosts)-2 }, 3*time.Second, 10*time.Millisecond)
// make sure we actually don't connect to host 1 any more
time.Sleep(100 * time.Millisecond)
require.Len(t, primary.Network().Conns(), len(hosts)-2, "wrong number of connections")
}