peers_test.go 1.8 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1
package swarm_test
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
2 3

import (
4
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5 6
	"testing"

7 8 9 10
	"github.com/libp2p/go-libp2p-core/network"
	"github.com/libp2p/go-libp2p-core/peer"
	"github.com/libp2p/go-libp2p-core/peerstore"

Jeromy's avatar
Jeromy committed
11
	ma "github.com/multiformats/go-multiaddr"
Steven Allen's avatar
Steven Allen committed
12 13

	. "github.com/libp2p/go-libp2p-swarm"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15 16 17 18 19 20 21 22 23
)

func TestPeers(t *testing.T) {
	ctx := context.Background()
	swarms := makeSwarms(ctx, t, 2)
	s1 := swarms[0]
	s2 := swarms[1]

	connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) {
		// TODO: make a DialAddr func.
24
		s.Peerstore().AddAddr(dst, addr, peerstore.PermanentAddrTTL)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25
		// t.Logf("connections from %s", s.LocalPeer())
Steven Allen's avatar
Steven Allen committed
26
		// for _, c := range s.ConnsToPeer(dst) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27 28 29
		// 	t.Logf("connection from %s to %s: %v", s.LocalPeer(), dst, c)
		// }
		// t.Logf("")
Steven Allen's avatar
Steven Allen committed
30
		if _, err := s.DialPeer(ctx, dst); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32 33 34 35
			t.Fatal("error swarm dialing to peer", err)
		}
		// t.Log(s.swarm.Dump())
	}

Cory Schwartz's avatar
Cory Schwartz committed
36 37
	s1GotConn := make(chan struct{})
	s2GotConn := make(chan struct{})
38
	s1.SetConnHandler(func(c network.Conn) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40
		s1GotConn <- struct{}{}
	})
41
	s2.SetConnHandler(func(c network.Conn) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42 43 44 45 46 47 48 49 50 51 52 53 54
		s2GotConn <- struct{}{}
	})

	connect(s1, s2.LocalPeer(), s2.ListenAddresses()[0])
	<-s2GotConn // have to wait here so the other side catches up.
	connect(s2, s1.LocalPeer(), s1.ListenAddresses()[0])

	for i := 0; i < 100; i++ {
		connect(s1, s2.LocalPeer(), s2.ListenAddresses()[0])
		connect(s2, s1.LocalPeer(), s1.ListenAddresses()[0])
	}

	for _, s := range swarms {
Steven Allen's avatar
Steven Allen committed
55
		log.Infof("%s swarm routing table: %s", s.LocalPeer(), s.Peers())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56 57 58 59 60 61 62 63
	}

	test := func(s *Swarm) {
		expect := 1
		actual := len(s.Peers())
		if actual != expect {
			t.Errorf("%s has %d peers, not %d: %v", s.LocalPeer(), actual, expect, s.Peers())
		}
Steven Allen's avatar
Steven Allen committed
64
		actual = len(s.Conns())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
		if actual != expect {
Steven Allen's avatar
Steven Allen committed
66
			t.Errorf("%s has %d conns, not %d: %v", s.LocalPeer(), actual, expect, s.Conns())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67 68 69 70 71 72
		}
	}

	test(s1)
	test(s2)
}