swarm_net_test.go 3.72 KB
Newer Older
1
2
3
4
5
6
7
package swarm_test

import (
	"fmt"
	"testing"
	"time"

8
9
	inet "github.com/libp2p/go-libp2p/p2p/net"
	testutil "github.com/libp2p/go-libp2p/p2p/test/util"
10
	"context"
11
12
13
14
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()

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

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

	dial := func(a, b inet.Network) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27
28
		testutil.DivulgeAddresses(b, a)
		if _, err := a.DialPeer(ctx, b.LocalPeer()); err != nil {
29
30
31
32
33
34
35
36
37
			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
38
39
40
41
	// 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)
42
43
44
45
46
47
48
49
50
51
52
53
54

	// test those connected show up correctly

	// test connected
	expectConnectedness(t, nets[0], nets[1], inet.Connected)
	expectConnectedness(t, nets[0], nets[3], inet.Connected)
	expectConnectedness(t, nets[1], nets[2], inet.Connected)
	expectConnectedness(t, nets[3], nets[2], inet.Connected)

	// test not connected
	expectConnectedness(t, nets[0], nets[2], inet.NotConnected)
	expectConnectedness(t, nets[1], nets[3], inet.NotConnected)

Jeromy's avatar
Jeromy committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
	if len(nets[0].Peers()) != 2 {
		t.Fatal("expected net 0 to have two peers")
	}

	if len(nets[2].Conns()) != 2 {
		t.Fatal("expected net 2 to have two conns")
	}

	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)
	}

71
72
	time.Sleep(time.Millisecond * 50)

Jeromy's avatar
Jeromy committed
73
74
	expectConnectedness(t, nets[2], nets[1], inet.NotConnected)

75
76
77
	for _, n := range nets {
		n.Close()
	}
Jeromy's avatar
Jeromy committed
78
79
80
81

	for _, n := range nets {
		<-n.Process().Closed()
	}
82
83
84
}

func expectConnectedness(t *testing.T, a, b inet.Network, expected inet.Connectedness) {
Jeromy's avatar
Jeromy committed
85
86
87
88
89
	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)
90
91
92
	}

	// test symmetric case
Jeromy's avatar
Jeromy committed
93
94
	if btoa != expected {
		t.Errorf(es, b, a, printConns(b), printConns(a), btoa)
95
96
97
98
99
100
101
102
103
104
	}
}

func printConns(n inet.Network) string {
	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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

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

	nets := make([]inet.Network, 4)
	for i := 0; i < 4; i++ {
		nets[i] = testutil.GenSwarmNetwork(t, ctx)
	}

	dial := func(a, b inet.Network) {
		testutil.DivulgeAddresses(b, a)
		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)
	nets[1].SetStreamHandler(func(s inet.Stream) {
		defer close(done)
		defer s.Close()

		buf := make([]byte, 10)
		_, err := s.Read(buf)
		if err != nil {
			t.Error(err)
			return
		}
		if string(buf) != "hello ipfs" {
			t.Error("got wrong message")
		}
	})

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

	_, err = s.Write([]byte("hello ipfs"))
	if err != nil {
		t.Fatal(err)
	}

	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")
	}
}