dht_test.go 9.22 KB
Newer Older
1 2
package dht

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
3
import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
	"bytes"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5 6 7 8
	"testing"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"

9
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11 12 13 14 15 16 17
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"

	ci "github.com/jbenet/go-ipfs/crypto"
	inet "github.com/jbenet/go-ipfs/net"
	mux "github.com/jbenet/go-ipfs/net/mux"
	netservice "github.com/jbenet/go-ipfs/net/service"
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"
18
	testutil "github.com/jbenet/go-ipfs/util/testutil"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19 20 21 22 23

	"fmt"
	"time"
)

24
func setupDHT(ctx context.Context, t *testing.T, p peer.Peer) *IpfsDHT {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26
	peerstore := peer.NewPeerstore()

27
	dhts := netservice.NewService(ctx, nil) // nil handler for now, need to patch it
28
	net, err := inet.NewIpfsNetwork(ctx, p.Addresses(), p, peerstore, &mux.ProtocolMap{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29 30 31 32 33 34
		mux.ProtocolID_Routing: dhts,
	})
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35
	d := NewDHT(ctx, p, peerstore, net, dhts, ds.NewMapDatastore())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
	dhts.SetHandler(d)
Jeromy's avatar
Jeromy committed
37 38 39
	d.Validators["v"] = func(u.Key, []byte) error {
		return nil
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41 42
	return d
}

43
func setupDHTS(ctx context.Context, n int, t *testing.T) ([]ma.Multiaddr, []peer.Peer, []*IpfsDHT) {
44
	var addrs []ma.Multiaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
	for i := 0; i < n; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 47 48 49 50 51 52
		a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5000+i))
		if err != nil {
			t.Fatal(err)
		}
		addrs = append(addrs, a)
	}

53
	var peers []peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54
	for i := 0; i < n; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56 57 58
		p := makePeer(addrs[i])
		peers = append(peers, p)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59 60
	dhts := make([]*IpfsDHT, n)
	for i := 0; i < n; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
		dhts[i] = setupDHT(ctx, t, peers[i])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63 64 65 66
	}

	return addrs, peers, dhts
}

67
func makePeer(addr ma.Multiaddr) peer.Peer {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68 69 70 71
	sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
	if err != nil {
		panic(err)
	}
72
	p, err := testutil.NewPeerWithKeyPair(sk, pk)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74 75
	if err != nil {
		panic(err)
	}
76
	p.AddAddress(addr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77 78 79 80
	return p
}

func TestPing(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81
	// t.Skip("skipping test to debug another")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
	u.Debug = false
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84 85 86 87 88 89 90 91 92 93 94 95
	addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/2222")
	if err != nil {
		t.Fatal(err)
	}
	addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5678")
	if err != nil {
		t.Fatal(err)
	}

	peerA := makePeer(addrA)
	peerB := makePeer(addrB)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96 97
	dhtA := setupDHT(ctx, t, peerA)
	dhtB := setupDHT(ctx, t, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99 100
	defer dhtA.Close()
	defer dhtB.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101 102
	defer dhtA.dialer.(inet.Network).Close()
	defer dhtB.dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104
	_, err = dhtA.Connect(ctx, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105 106 107 108 109
	if err != nil {
		t.Fatal(err)
	}

	//Test that we can ping the node
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
	ctxT, _ := context.WithTimeout(ctx, 100*time.Millisecond)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111
	err = dhtA.Ping(ctxT, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
112 113 114
	if err != nil {
		t.Fatal(err)
	}
115

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
116
	ctxT, _ = context.WithTimeout(ctx, 100*time.Millisecond)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117
	err = dhtB.Ping(ctxT, peerA)
118 119 120
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121 122 123
}

func TestValueGetSet(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
124 125
	// t.Skip("skipping test to debug another")

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
127
	u.Debug = false
128
	addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/11235")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
129 130 131
	if err != nil {
		t.Fatal(err)
	}
132
	addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/15679")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
133 134 135 136 137 138 139
	if err != nil {
		t.Fatal(err)
	}

	peerA := makePeer(addrA)
	peerB := makePeer(addrB)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140 141
	dhtA := setupDHT(ctx, t, peerA)
	dhtB := setupDHT(ctx, t, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
142

Jeromy's avatar
Jeromy committed
143 144 145 146 147 148
	vf := func(u.Key, []byte) error {
		return nil
	}
	dhtA.Validators["v"] = vf
	dhtB.Validators["v"] = vf

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149 150
	defer dhtA.Close()
	defer dhtB.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151 152
	defer dhtA.dialer.(inet.Network).Close()
	defer dhtB.dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
153

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
154
	_, err = dhtA.Connect(ctx, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
155 156 157 158
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
159
	ctxT, _ := context.WithTimeout(ctx, time.Second)
Jeromy's avatar
Jeromy committed
160
	dhtA.PutValue(ctxT, "/v/hello", []byte("world"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
162
	ctxT, _ = context.WithTimeout(ctx, time.Second*2)
Jeromy's avatar
Jeromy committed
163
	val, err := dhtA.GetValue(ctxT, "/v/hello")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
164 165 166 167 168 169 170 171
	if err != nil {
		t.Fatal(err)
	}

	if string(val) != "world" {
		t.Fatalf("Expected 'world' got '%s'", string(val))
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172
	ctxT, _ = context.WithTimeout(ctx, time.Second*2)
Jeromy's avatar
Jeromy committed
173
	val, err = dhtB.GetValue(ctxT, "/v/hello")
174 175 176 177 178 179 180
	if err != nil {
		t.Fatal(err)
	}

	if string(val) != "world" {
		t.Fatalf("Expected 'world' got '%s'", string(val))
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
181 182
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
183 184
func TestProvides(t *testing.T) {
	// t.Skip("skipping test to debug another")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
185
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
186 187 188

	u.Debug = false

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
189
	_, peers, dhts := setupDHTS(ctx, 4, t)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
190 191
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
192
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
193
			defer dhts[i].dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
194 195 196
		}
	}()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
197
	_, err := dhts[0].Connect(ctx, peers[1])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
198 199 200 201
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202
	_, err = dhts[1].Connect(ctx, peers[2])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
203 204 205 206
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
207
	_, err = dhts[1].Connect(ctx, peers[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221
	if err != nil {
		t.Fatal(err)
	}

	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
	if err != nil {
		t.Fatal(err)
	}

	bits, err := dhts[3].getLocal(u.Key("hello"))
	if err != nil && bytes.Equal(bits, []byte("world")) {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
222
	err = dhts[3].Provide(ctx, u.Key("hello"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
223 224 225 226 227 228
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 60)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
229
	ctxT, _ := context.WithTimeout(ctx, time.Second)
Jeromy's avatar
Jeromy committed
230
	provchan := dhts[0].FindProvidersAsync(ctxT, u.Key("hello"), 1)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
231

Jeromy's avatar
Jeromy committed
232 233 234 235 236 237 238 239
	after := time.After(time.Second)
	select {
	case prov := <-provchan:
		if prov == nil {
			t.Fatal("Got back nil provider")
		}
	case <-after:
		t.Fatal("Did not get a provider back.")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
240 241 242
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
243
func TestProvidesAsync(t *testing.T) {
244 245 246
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
247

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
248
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
249 250
	u.Debug = false

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
251
	_, peers, dhts := setupDHTS(ctx, 4, t)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
252 253
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
254
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
255
			defer dhts[i].dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
256 257 258
		}
	}()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
259
	_, err := dhts[0].Connect(ctx, peers[1])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
260 261 262 263
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
264
	_, err = dhts[1].Connect(ctx, peers[2])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
265 266 267 268
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
269
	_, err = dhts[1].Connect(ctx, peers[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283
	if err != nil {
		t.Fatal(err)
	}

	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
	if err != nil {
		t.Fatal(err)
	}

	bits, err := dhts[3].getLocal(u.Key("hello"))
	if err != nil && bytes.Equal(bits, []byte("world")) {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
284
	err = dhts[3].Provide(ctx, u.Key("hello"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
285 286 287 288 289 290
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 60)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
291 292
	ctxT, _ := context.WithTimeout(ctx, time.Millisecond*300)
	provs := dhts[0].FindProvidersAsync(ctxT, u.Key("hello"), 5)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
293
	select {
Jeromy's avatar
Jeromy committed
294 295 296 297 298 299 300
	case p, ok := <-provs:
		if !ok {
			t.Fatal("Provider channel was closed...")
		}
		if p == nil {
			t.Fatal("Got back nil provider!")
		}
301
		if !p.ID().Equal(dhts[3].self.ID()) {
302
			t.Fatalf("got a provider, but not the right one. %s", p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
303
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
304
	case <-ctxT.Done():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
305 306 307 308
		t.Fatal("Didnt get back providers")
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
309
func TestLayeredGet(t *testing.T) {
310 311 312
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
313

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
314
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
315
	u.Debug = false
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
316
	_, peers, dhts := setupDHTS(ctx, 4, t)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
317 318
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
319
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
320
			defer dhts[i].dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
321 322 323
		}
	}()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
324
	_, err := dhts[0].Connect(ctx, peers[1])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
325 326 327 328
	if err != nil {
		t.Fatalf("Failed to connect: %s", err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
329
	_, err = dhts[1].Connect(ctx, peers[2])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
330 331 332 333
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
334
	_, err = dhts[1].Connect(ctx, peers[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
335 336 337 338
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
339
	err = dhts[3].putLocal(u.Key("/v/hello"), []byte("world"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
340 341 342 343
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
344
	err = dhts[3].Provide(ctx, u.Key("/v/hello"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
345 346 347 348 349 350
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 60)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
351
	ctxT, _ := context.WithTimeout(ctx, time.Second)
Jeromy's avatar
Jeromy committed
352
	val, err := dhts[0].GetValue(ctxT, u.Key("/v/hello"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
353 354 355 356 357 358 359 360 361 362 363
	if err != nil {
		t.Fatal(err)
	}

	if string(val) != "world" {
		t.Fatal("Got incorrect value.")
	}

}

func TestFindPeer(t *testing.T) {
364 365 366
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
367

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
368
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
369 370
	u.Debug = false

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
371
	_, peers, dhts := setupDHTS(ctx, 4, t)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
372 373
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
374
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
375
			dhts[i].dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
376 377 378
		}
	}()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
379
	_, err := dhts[0].Connect(ctx, peers[1])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
380 381 382 383
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
384
	_, err = dhts[1].Connect(ctx, peers[2])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
385 386 387 388
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
389
	_, err = dhts[1].Connect(ctx, peers[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
390 391 392 393
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
394
	ctxT, _ := context.WithTimeout(ctx, time.Second)
395
	p, err := dhts[0].FindPeer(ctxT, peers[2].ID())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
396 397 398 399 400 401 402 403
	if err != nil {
		t.Fatal(err)
	}

	if p == nil {
		t.Fatal("Failed to find peer.")
	}

404
	if !p.ID().Equal(peers[2].ID()) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
405 406 407
		t.Fatal("Didnt find expected peer.")
	}
}
408 409

func TestConnectCollision(t *testing.T) {
410 411 412
	if testing.Short() {
		t.SkipNow()
	}
413

414
	runTimes := 10
415

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
416 417
	for rtime := 0; rtime < runTimes; rtime++ {
		log.Notice("Running Time: ", rtime)
418

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
419 420
		ctx := context.Background()
		u.Debug = false
421
		addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/11235")
422 423 424
		if err != nil {
			t.Fatal(err)
		}
425
		addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/15679")
426 427 428 429
		if err != nil {
			t.Fatal(err)
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
430 431 432 433 434 435 436 437
		peerA := makePeer(addrA)
		peerB := makePeer(addrB)

		dhtA := setupDHT(ctx, t, peerA)
		dhtB := setupDHT(ctx, t, peerB)

		done := make(chan struct{})
		go func() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
438
			_, err := dhtA.Connect(ctx, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
439 440 441 442 443 444
			if err != nil {
				t.Fatal(err)
			}
			done <- struct{}{}
		}()
		go func() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
445
			_, err := dhtB.Connect(ctx, peerA)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
			if err != nil {
				t.Fatal(err)
			}
			done <- struct{}{}
		}()

		timeout := time.After(time.Second)
		select {
		case <-done:
		case <-timeout:
			t.Fatal("Timeout received!")
		}
		select {
		case <-done:
		case <-timeout:
			t.Fatal("Timeout received!")
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
464 465
		dhtA.Close()
		dhtB.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
466 467
		dhtA.dialer.(inet.Network).Close()
		dhtB.dialer.(inet.Network).Close()
468 469

		<-time.After(200 * time.Millisecond)
Jeromy's avatar
Jeromy committed
470
	}
471
}