mdns_test.go 888 Bytes
Newer Older
Jeromy's avatar
Jeromy committed
1
2
3
4
5
6
7
package discovery

import (
	"context"
	"testing"
	"time"

Jeromy's avatar
Jeromy committed
8
	host "github.com/libp2p/go-libp2p-host"
Jeromy's avatar
Jeromy committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
	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)
	}
}