|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
|
|
|
|
|
|
|
host "github.com/libp2p/go-libp2p-host"
|
|
|
|
netutil "github.com/libp2p/go-libp2p-netutil"
|
|
|
|
|
|
|
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DiscoveryNotifee struct {
|
|
|
|
h host.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *DiscoveryNotifee) HandlePeerFound(pi pstore.PeerInfo) {
|
|
|
|
n.h.Connect(context.Background(), pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMdnsDiscovery(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
a := bhost.New(netutil.GenSwarmNetwork(t, ctx))
|
|
|
|
b := bhost.New(netutil.GenSwarmNetwork(t, ctx))
|
|
|
|
|
|
|
|
sa, err := NewMdnsService(ctx, a, time.Second, "someTag")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := NewMdnsService(ctx, b, time.Second, "someTag")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = sb
|
|
|
|
|
|
|
|
n := &DiscoveryNotifee{a}
|
|
|
|
|
|
|
|
sa.RegisterNotifee(n)
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
err = a.Connect(ctx, pstore.PeerInfo{ID: b.ID()})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|