mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-27 05:38:55 +00:00
82efca9b6f
* move to deprecated-p2p * fix lint * lint? * fix lint * lint * lint * lint * lint
51 lines
1.1 KiB
Go
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)
|
|
}
|