prysm-pulse/shared/deprecated-p2p/discovery_norace_test.go
Preston Van Loon 82efca9b6f
Move p2p to deprecated-p2p (#3191)
* move to deprecated-p2p

* fix lint

* lint?

* fix lint

* lint

* lint

* lint

* lint
2019-08-13 14:52:04 -04:00

51 lines
1.1 KiB
Go

package p2p
import (
"context"
"testing"
"time"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
mdns "github.com/libp2p/go-libp2p/p2p/discovery"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
)
var _ = mdns.Notifee(&discovery{})
func expectPeers(t *testing.T, h *bhost.BasicHost, n int) {
if len(h.Peerstore().Peers()) != n {
t.Errorf(
"Expected %d peer for host %v, but has %d peers. They are: %v.",
n,
h.ID(),
len(h.Peerstore().Peers()),
h.Peerstore().Peers(),
)
}
}
func TestStartDiscovery_PeerFound(t *testing.T) {
discoveryInterval = 50 * time.Millisecond // Short interval for testing.
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
a := bhost.New(swarmt.GenSwarm(t, ctx))
err := startmDNSDiscovery(ctx, a)
if err != nil {
t.Errorf("Error when starting discovery: %v", err)
}
b := bhost.New(swarmt.GenSwarm(t, ctx))
err = startmDNSDiscovery(ctx, b)
if err != nil {
t.Errorf("Error when starting discovery: %v", err)
}
// The two hosts should have found each other after 1+ intervals.
time.Sleep(2 * discoveryInterval)
expectPeers(t, a, 2)
expectPeers(t, b, 2)
}