mirror of https://github.com/libp2p/go-libp2p.git
Jeromy
8 years ago
2 changed files with 84 additions and 20 deletions
@ -0,0 +1,51 @@ |
|||
package discovery |
|||
|
|||
import ( |
|||
"context" |
|||
"testing" |
|||
"time" |
|||
|
|||
host "github.com/libp2p/go-libp2p/p2p/host" |
|||
netutil "github.com/libp2p/go-libp2p/p2p/test/util" |
|||
|
|||
pstore "github.com/ipfs/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 := netutil.GenHostSwarm(t, ctx) |
|||
b := netutil.GenHostSwarm(t, ctx) |
|||
|
|||
sa, err := NewMdnsService(ctx, a, time.Second) |
|||
if err != nil { |
|||
t.Fatal(err) |
|||
} |
|||
|
|||
sb, err := NewMdnsService(ctx, b, time.Second) |
|||
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) |
|||
} |
|||
} |
Loading…
Reference in new issue