reconnect_test.go 5.31 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
package reconnect

import (
4
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
6
7
8
9
10
	"io"
	"math/rand"
	"sync"
	"testing"
	"time"

Jeromy's avatar
Jeromy committed
11
12
	bhost "github.com/libp2p/go-libp2p/p2p/host/basic"

Jeromy's avatar
Jeromy committed
13
14
	u "github.com/ipfs/go-ipfs-util"
	logging "github.com/ipfs/go-log"
Jeromy's avatar
Jeromy committed
15
	host "github.com/libp2p/go-libp2p-host"
Jeromy's avatar
Jeromy committed
16
	inet "github.com/libp2p/go-libp2p-net"
Jeromy's avatar
Jeromy committed
17
	testutil "github.com/libp2p/go-libp2p-netutil"
Jeromy's avatar
Jeromy committed
18
	protocol "github.com/libp2p/go-libp2p-protocol"
Jeromy's avatar
Jeromy committed
19
	swarm "github.com/libp2p/go-libp2p-swarm"
20
	ps "github.com/libp2p/go-peerstream"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
22
23
24
25
26
27
)

func init() {
	// change the garbage collect timeout for testing.
	ps.GarbageCollectTimeout = 10 * time.Millisecond
}

Jeromy's avatar
Jeromy committed
28
var log = logging.Logger("reconnect")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29
30
31
32
33

func EchoStreamHandler(stream inet.Stream) {
	c := stream.Conn()
	log.Debugf("%s echoing %s", c.LocalPeer(), c.RemotePeer())
	go func() {
34
35
36
37
38
39
		_, err := io.Copy(stream, stream)
		if err == nil {
			stream.Close()
		} else {
			stream.Reset()
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40
41
42
43
	}()
}

type sendChans struct {
Jeromy's avatar
Jeromy committed
44
45
46
47
48
	send   chan struct{}
	sent   chan struct{}
	read   chan struct{}
	close_ chan struct{}
	closed chan struct{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
50
51
52
}

func newSendChans() sendChans {
	return sendChans{
Jeromy's avatar
Jeromy committed
53
54
55
56
57
		send:   make(chan struct{}),
		sent:   make(chan struct{}),
		read:   make(chan struct{}),
		close_: make(chan struct{}),
		closed: make(chan struct{}),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	}
}

func newSender() (chan sendChans, func(s inet.Stream)) {
	scc := make(chan sendChans)
	return scc, func(s inet.Stream) {
		sc := newSendChans()
		scc <- sc

		defer func() {
			s.Close()
			sc.closed <- struct{}{}
		}()

		buf := make([]byte, 65536)
		buf2 := make([]byte, 65536)
74
		u.NewTimeSeededRand().Read(buf)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

		for {
			select {
			case <-sc.close_:
				return
			case <-sc.send:
			}

			// send a randomly sized subchunk
			from := rand.Intn(len(buf) / 2)
			to := rand.Intn(len(buf) / 2)
			sendbuf := buf[from : from+to]

			log.Debugf("sender sending %d bytes", len(sendbuf))
			n, err := s.Write(sendbuf)
			if err != nil {
				log.Debug("sender error. exiting:", err)
				return
			}

			log.Debugf("sender wrote %d bytes", n)
			sc.sent <- struct{}{}

			if n, err = io.ReadFull(s, buf2[:len(sendbuf)]); err != nil {
				log.Debug("sender error. failed to read:", err)
				return
			}

			log.Debugf("sender read %d bytes", n)
			sc.read <- struct{}{}
		}
	}
}

// TestReconnect tests whether hosts are able to disconnect and reconnect.
func TestReconnect2(t *testing.T) {
	ctx := context.Background()
Jeromy's avatar
Jeromy committed
112
113
	h1 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	h2 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114
115
116
117
118
	hosts := []host.Host{h1, h2}

	h1.SetStreamHandler(protocol.TestingID, EchoStreamHandler)
	h2.SetStreamHandler(protocol.TestingID, EchoStreamHandler)

119
	rounds := 8
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
120
121
122
123
124
125
126
127
128
129
130
131
	if testing.Short() {
		rounds = 4
	}
	for i := 0; i < rounds; i++ {
		log.Debugf("TestReconnect: %d/%d\n", i, rounds)
		SubtestConnSendDisc(t, hosts)
	}
}

// TestReconnect tests whether hosts are able to disconnect and reconnect.
func TestReconnect5(t *testing.T) {
	ctx := context.Background()
Jeromy's avatar
Jeromy committed
132
133
134
135
136
	h1 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	h2 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	h3 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	h4 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	h5 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137
138
139
140
141
142
143
144
	hosts := []host.Host{h1, h2, h3, h4, h5}

	h1.SetStreamHandler(protocol.TestingID, EchoStreamHandler)
	h2.SetStreamHandler(protocol.TestingID, EchoStreamHandler)
	h3.SetStreamHandler(protocol.TestingID, EchoStreamHandler)
	h4.SetStreamHandler(protocol.TestingID, EchoStreamHandler)
	h5.SetStreamHandler(protocol.TestingID, EchoStreamHandler)

145
	rounds := 4
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146
	if testing.Short() {
147
		rounds = 2
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
	}
	for i := 0; i < rounds; i++ {
		log.Debugf("TestReconnect: %d/%d\n", i, rounds)
		SubtestConnSendDisc(t, hosts)
	}
}

func SubtestConnSendDisc(t *testing.T, hosts []host.Host) {

	ctx := context.Background()
	numStreams := 3 * len(hosts)
	numMsgs := 10

	if testing.Short() {
		numStreams = 5 * len(hosts)
		numMsgs = 4
	}

	ss, sF := newSender()

	for _, h1 := range hosts {
		for _, h2 := range hosts {
			if h1.ID() >= h2.ID() {
				continue
			}

			h2pi := h2.Peerstore().PeerInfo(h2.ID())
			log.Debugf("dialing %s", h2pi.Addrs)
			if err := h1.Connect(ctx, h2pi); err != nil {
Kevin Atkinson's avatar
Kevin Atkinson committed
177
				t.Fatal("Failed to connect:", err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
178
179
180
181
182
183
184
185
			}
		}
	}

	var wg sync.WaitGroup
	for i := 0; i < numStreams; i++ {
		h1 := hosts[i%len(hosts)]
		h2 := hosts[(i+1)%len(hosts)]
186
		s, err := h1.NewStream(context.Background(), h2.ID(), protocol.TestingID)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
187
188
189
190
191
192
193
194
195
		if err != nil {
			t.Error(err)
		}

		wg.Add(1)
		go func(j int) {
			defer wg.Done()

			go sF(s)
196
			log.Debugf("getting handle %d", j)
Jeromy's avatar
Jeromy committed
197
			sc := <-ss // wait to get handle.
198
			log.Debugf("spawning worker %d", j)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
199

200
			for k := 0; k < numMsgs; k++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
201
202
				sc.send <- struct{}{}
				<-sc.sent
203
				log.Debugf("%d sent %d", j, k)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
204
				<-sc.read
205
				log.Debugf("%d read %d", j, k)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
			}
			sc.close_ <- struct{}{}
			<-sc.closed
			log.Debugf("closed %d", j)
		}(i)
	}
	wg.Wait()

	for i, h1 := range hosts {
		log.Debugf("host %d has %d conns", i, len(h1.Network().Conns()))
	}

	for _, h1 := range hosts {
		// close connection
		cs := h1.Network().Conns()
		for _, c := range cs {
			sc := c.(*swarm.Conn)
			if sc.LocalPeer() > sc.RemotePeer() {
Jeromy's avatar
Jeromy committed
224
				continue // only close it on one side.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
			}

			log.Debugf("closing: %s", sc.RawConn())
			sc.Close()
		}
	}

	<-time.After(20 * time.Millisecond)

	for i, h := range hosts {
		if len(h.Network().Conns()) > 0 {
			t.Fatalf("host %d %s has %d conns! not zero.", i, h.ID(), len(h.Network().Conns()))
		}
	}
}