swarm_net_test.go 4.03 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 (
Jeromy's avatar
Jeromy committed
4
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"fmt"
web3-bot's avatar
web3-bot committed
6
	"io/ioutil"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8 9
	"testing"
	"time"

10
	"github.com/libp2p/go-libp2p-core/network"
Jeromy's avatar
Jeromy committed
11

Steven Allen's avatar
Steven Allen committed
12 13
	. "github.com/libp2p/go-libp2p-swarm/testing"
)
Jeromy's avatar
Jeromy committed
14

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15 16 17 18 19 20
// TestConnectednessCorrect starts a few networks, connects a few
// and tests Connectedness value is correct.
func TestConnectednessCorrect(t *testing.T) {

	ctx := context.Background()

21
	nets := make([]network.Network, 4)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22
	for i := 0; i < 4; i++ {
Steven Allen's avatar
Steven Allen committed
23
		nets[i] = GenSwarm(t, ctx)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25 26 27
	}

	// connect 0-1, 0-2, 0-3, 1-2, 2-3

28
	dial := func(a, b network.Network) {
Jeromy's avatar
Jeromy committed
29
		DivulgeAddresses(b, a)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31 32 33 34 35 36 37 38 39
		if _, err := a.DialPeer(ctx, b.LocalPeer()); err != nil {
			t.Fatalf("Failed to dial: %s", err)
		}
	}

	dial(nets[0], nets[1])
	dial(nets[0], nets[3])
	dial(nets[1], nets[2])
	dial(nets[3], nets[2])

Jeromy's avatar
Jeromy committed
40 41 42 43
	// The notifications for new connections get sent out asynchronously.
	// There is the potential for a race condition here, so we sleep to ensure
	// that they have been received.
	time.Sleep(time.Millisecond * 100)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45 46 47

	// test those connected show up correctly

	// test connected
48 49 50 51
	expectConnectedness(t, nets[0], nets[1], network.Connected)
	expectConnectedness(t, nets[0], nets[3], network.Connected)
	expectConnectedness(t, nets[1], nets[2], network.Connected)
	expectConnectedness(t, nets[3], nets[2], network.Connected)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52 53

	// test not connected
54 55
	expectConnectedness(t, nets[0], nets[2], network.NotConnected)
	expectConnectedness(t, nets[1], nets[3], network.NotConnected)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56

Jeromy's avatar
Jeromy committed
57 58 59 60
	if len(nets[0].Peers()) != 2 {
		t.Fatal("expected net 0 to have two peers")
	}

vyzo's avatar
vyzo committed
61 62
	if len(nets[2].Peers()) != 2 {
		t.Fatal("expected net 2 to have two peers")
Jeromy's avatar
Jeromy committed
63 64 65 66 67 68 69 70 71 72
	}

	if len(nets[1].ConnsToPeer(nets[3].LocalPeer())) != 0 {
		t.Fatal("net 1 should have no connections to net 3")
	}

	if err := nets[2].ClosePeer(nets[1].LocalPeer()); err != nil {
		t.Fatal(err)
	}

73 74
	time.Sleep(time.Millisecond * 50)

75
	expectConnectedness(t, nets[2], nets[1], network.NotConnected)
Jeromy's avatar
Jeromy committed
76

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77 78 79
	for _, n := range nets {
		n.Close()
	}
Jeromy's avatar
Jeromy committed
80 81 82 83

	for _, n := range nets {
		<-n.Process().Closed()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84 85
}

86
func expectConnectedness(t *testing.T, a, b network.Network, expected network.Connectedness) {
Jeromy's avatar
Jeromy committed
87 88 89 90 91
	es := "%s is connected to %s, but Connectedness incorrect. %s %s %s"
	atob := a.Connectedness(b.LocalPeer())
	btoa := b.Connectedness(a.LocalPeer())
	if atob != expected {
		t.Errorf(es, a, b, printConns(a), printConns(b), atob)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92 93 94
	}

	// test symmetric case
Jeromy's avatar
Jeromy committed
95 96
	if btoa != expected {
		t.Errorf(es, b, a, printConns(b), printConns(a), btoa)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
97 98 99
	}
}

100
func printConns(n network.Network) string {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101 102 103 104 105 106
	s := fmt.Sprintf("Connections in %s:\n", n)
	for _, c := range n.Conns() {
		s = s + fmt.Sprintf("- %s\n", c)
	}
	return s
}
Jeromy's avatar
Jeromy committed
107 108 109 110 111

func TestNetworkOpenStream(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

web3-bot's avatar
web3-bot committed
112 113
	testString := "hello ipfs"

114
	nets := make([]network.Network, 4)
Jeromy's avatar
Jeromy committed
115
	for i := 0; i < 4; i++ {
Steven Allen's avatar
Steven Allen committed
116
		nets[i] = GenSwarm(t, ctx)
Jeromy's avatar
Jeromy committed
117 118
	}

119
	dial := func(a, b network.Network) {
Jeromy's avatar
Jeromy committed
120
		DivulgeAddresses(b, a)
Jeromy's avatar
Jeromy committed
121 122 123 124 125 126 127 128 129 130
		if _, err := a.DialPeer(ctx, b.LocalPeer()); err != nil {
			t.Fatalf("Failed to dial: %s", err)
		}
	}

	dial(nets[0], nets[1])
	dial(nets[0], nets[3])
	dial(nets[1], nets[2])

	done := make(chan bool)
131
	nets[1].SetStreamHandler(func(s network.Stream) {
Jeromy's avatar
Jeromy committed
132 133 134
		defer close(done)
		defer s.Close()

web3-bot's avatar
web3-bot committed
135
		buf, err := ioutil.ReadAll(s)
Jeromy's avatar
Jeromy committed
136 137 138 139
		if err != nil {
			t.Error(err)
			return
		}
web3-bot's avatar
web3-bot committed
140
		if string(buf) != testString {
Jeromy's avatar
Jeromy committed
141 142 143 144 145 146 147 148 149
			t.Error("got wrong message")
		}
	})

	s, err := nets[0].NewStream(ctx, nets[1].LocalPeer())
	if err != nil {
		t.Fatal(err)
	}

web3-bot's avatar
web3-bot committed
150 151 152
	numStreams := 0
	for _, conn := range nets[0].ConnsToPeer(nets[1].LocalPeer()) {
		numStreams += len(conn.GetStreams())
Jeromy's avatar
Jeromy committed
153 154
	}

web3-bot's avatar
web3-bot committed
155
	if numStreams != 1 {
Jeromy's avatar
Jeromy committed
156 157 158
		t.Fatal("should only have one stream there")
	}

web3-bot's avatar
web3-bot committed
159
	n, err := s.Write([]byte(testString))
Jeromy's avatar
Jeromy committed
160 161
	if err != nil {
		t.Fatal(err)
web3-bot's avatar
web3-bot committed
162 163
	} else if n != len(testString) {
		t.Errorf("expected to write %d bytes, wrote %d", len(testString), n)
Jeromy's avatar
Jeromy committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	}

	err = s.Close()
	if err != nil {
		t.Fatal(err)
	}

	select {
	case <-done:
	case <-time.After(time.Millisecond * 100):
		t.Fatal("timed out waiting on stream")
	}

	_, err = nets[1].NewStream(ctx, nets[3].LocalPeer())
	if err == nil {
		t.Fatal("expected stream open 1->3 to fail")
	}
}