dht_test.go 11.2 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"
5
	"sort"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7 8 9
	"testing"

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

10
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13 14 15 16 17 18
	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"
19
	testutil "github.com/jbenet/go-ipfs/util/testutil"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21 22 23 24

	"fmt"
	"time"
)

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

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

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

44
func setupDHTS(ctx context.Context, n int, t *testing.T) ([]ma.Multiaddr, []peer.Peer, []*IpfsDHT) {
45
	var addrs []ma.Multiaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
	for i := 0; i < n; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48 49 50 51 52 53
		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)
	}

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

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

	return addrs, peers, dhts
}

68 69 70 71 72 73 74 75
func makePeerString(t *testing.T, addr string) peer.Peer {
	maddr, err := ma.NewMultiaddr(addr)
	if err != nil {
		t.Fatal(err)
	}
	return makePeer(maddr)
}

76
func makePeer(addr ma.Multiaddr) peer.Peer {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77 78 79 80
	sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
	if err != nil {
		panic(err)
	}
81
	p, err := testutil.NewPeerWithKeyPair(sk, pk)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82 83 84
	if err != nil {
		panic(err)
	}
85
	p.AddAddress(addr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86 87 88 89
	return p
}

func TestPing(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
	// t.Skip("skipping test to debug another")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92
	u.Debug = false
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93 94 95 96 97 98 99 100 101 102 103 104
	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
105 106
	dhtA := setupDHT(ctx, t, peerA)
	dhtB := setupDHT(ctx, t, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
107

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108 109
	defer dhtA.Close()
	defer dhtB.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110 111
	defer dhtA.dialer.(inet.Network).Close()
	defer dhtB.dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
112

113
	err = dhtA.Connect(ctx, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114 115 116 117 118
	if err != nil {
		t.Fatal(err)
	}

	//Test that we can ping the node
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
119
	ctxT, _ := context.WithTimeout(ctx, 100*time.Millisecond)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
120
	err = dhtA.Ping(ctxT, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121 122 123
	if err != nil {
		t.Fatal(err)
	}
124

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
125
	ctxT, _ = context.WithTimeout(ctx, 100*time.Millisecond)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126
	err = dhtB.Ping(ctxT, peerA)
127 128 129
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130 131 132
}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
135
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
136
	u.Debug = false
137
	addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/11235")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
138 139 140
	if err != nil {
		t.Fatal(err)
	}
141
	addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/15679")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
142 143 144 145 146 147 148
	if err != nil {
		t.Fatal(err)
	}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149 150
	dhtA := setupDHT(ctx, t, peerA)
	dhtB := setupDHT(ctx, t, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151

Jeromy's avatar
Jeromy committed
152 153 154 155 156 157
	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
158 159
	defer dhtA.Close()
	defer dhtB.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
160 161
	defer dhtA.dialer.(inet.Network).Close()
	defer dhtB.dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
162

163
	err = dhtA.Connect(ctx, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
164 165 166 167
	if err != nil {
		t.Fatal(err)
	}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
171
	ctxT, _ = context.WithTimeout(ctx, time.Second*2)
Jeromy's avatar
Jeromy committed
172
	val, err := dhtA.GetValue(ctxT, "/v/hello")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
173 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
	ctxT, _ = context.WithTimeout(ctx, time.Second*2)
Jeromy's avatar
Jeromy committed
182
	val, err = dhtB.GetValue(ctxT, "/v/hello")
183 184 185 186 187 188 189
	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
190 191
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
192 193
func TestProvides(t *testing.T) {
	// t.Skip("skipping test to debug another")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
194
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
195 196 197

	u.Debug = false

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
198
	_, peers, dhts := setupDHTS(ctx, 4, t)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
199 200
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
201
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202
			defer dhts[i].dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
203 204 205
		}
	}()

206
	err := dhts[0].Connect(ctx, peers[1])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
207 208 209 210
	if err != nil {
		t.Fatal(err)
	}

211
	err = dhts[1].Connect(ctx, peers[2])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
212 213 214 215
	if err != nil {
		t.Fatal(err)
	}

216
	err = dhts[1].Connect(ctx, peers[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230
	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
231
	err = dhts[3].Provide(ctx, u.Key("hello"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
232 233 234 235 236 237
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 60)

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

Jeromy's avatar
Jeromy committed
241 242 243 244 245 246 247 248
	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
249 250 251
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
252
func TestProvidesAsync(t *testing.T) {
253 254 255
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
256

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
257
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
258 259
	u.Debug = false

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
260
	_, peers, dhts := setupDHTS(ctx, 4, t)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
261 262
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
263
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
264
			defer dhts[i].dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
265 266 267
		}
	}()

268
	err := dhts[0].Connect(ctx, peers[1])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
269 270 271 272
	if err != nil {
		t.Fatal(err)
	}

273
	err = dhts[1].Connect(ctx, peers[2])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
274 275 276 277
	if err != nil {
		t.Fatal(err)
	}

278
	err = dhts[1].Connect(ctx, peers[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292
	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
293
	err = dhts[3].Provide(ctx, u.Key("hello"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
294 295 296 297 298 299
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 60)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
300 301
	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
302
	select {
Jeromy's avatar
Jeromy committed
303 304 305 306 307 308 309
	case p, ok := <-provs:
		if !ok {
			t.Fatal("Provider channel was closed...")
		}
		if p == nil {
			t.Fatal("Got back nil provider!")
		}
310
		if !p.ID().Equal(dhts[3].self.ID()) {
311
			t.Fatalf("got a provider, but not the right one. %s", p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
312
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
313
	case <-ctxT.Done():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
314 315 316 317
		t.Fatal("Didnt get back providers")
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
318
func TestLayeredGet(t *testing.T) {
319 320 321
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
322

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
323
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
324
	u.Debug = false
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
325
	_, peers, dhts := setupDHTS(ctx, 4, t)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
326 327
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
328
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
329
			defer dhts[i].dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
330 331 332
		}
	}()

333
	err := dhts[0].Connect(ctx, peers[1])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
334 335 336 337
	if err != nil {
		t.Fatalf("Failed to connect: %s", err)
	}

338
	err = dhts[1].Connect(ctx, peers[2])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
339 340 341 342
	if err != nil {
		t.Fatal(err)
	}

343
	err = dhts[1].Connect(ctx, peers[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
344 345 346 347
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
348
	err = dhts[3].putLocal(u.Key("/v/hello"), []byte("world"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
349 350 351 352
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
353
	err = dhts[3].Provide(ctx, u.Key("/v/hello"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
354 355 356 357 358 359
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 60)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
360
	ctxT, _ := context.WithTimeout(ctx, time.Second)
Jeromy's avatar
Jeromy committed
361
	val, err := dhts[0].GetValue(ctxT, u.Key("/v/hello"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
362 363 364 365 366 367 368 369 370 371 372
	if err != nil {
		t.Fatal(err)
	}

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

}

func TestFindPeer(t *testing.T) {
373 374 375
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
376

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
377
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
378 379
	u.Debug = false

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
380
	_, peers, dhts := setupDHTS(ctx, 4, t)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
381 382
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
383
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
384
			dhts[i].dialer.(inet.Network).Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
385 386 387
		}
	}()

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

393
	err = dhts[1].Connect(ctx, peers[2])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
394 395 396 397
	if err != nil {
		t.Fatal(err)
	}

398
	err = dhts[1].Connect(ctx, peers[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
399 400 401 402
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
403
	ctxT, _ := context.WithTimeout(ctx, time.Second)
404
	p, err := dhts[0].FindPeer(ctxT, peers[2].ID())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
405 406 407 408 409 410 411 412
	if err != nil {
		t.Fatal(err)
	}

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

413
	if !p.ID().Equal(peers[2].ID()) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
414 415 416
		t.Fatal("Didnt find expected peer.")
	}
}
417

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
func TestFindPeersConnectedToPeer(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}

	ctx := context.Background()
	u.Debug = false

	_, peers, dhts := setupDHTS(ctx, 4, t)
	defer func() {
		for i := 0; i < 4; i++ {
			dhts[i].Close()
			dhts[i].dialer.(inet.Network).Close()
		}
	}()

	// topology:
	// 0-1, 1-2, 1-3, 2-3
	err := dhts[0].Connect(ctx, peers[1])
	if err != nil {
		t.Fatal(err)
	}

	err = dhts[1].Connect(ctx, peers[2])
	if err != nil {
		t.Fatal(err)
	}

	err = dhts[1].Connect(ctx, peers[3])
	if err != nil {
		t.Fatal(err)
	}

	err = dhts[2].Connect(ctx, peers[3])
	if err != nil {
		t.Fatal(err)
	}

	// fmt.Println("0 is", peers[0])
	// fmt.Println("1 is", peers[1])
	// fmt.Println("2 is", peers[2])
	// fmt.Println("3 is", peers[3])

	ctxT, _ := context.WithTimeout(ctx, time.Second)
	pchan, err := dhts[0].FindPeersConnectedToPeer(ctxT, peers[2].ID())
	if err != nil {
		t.Fatal(err)
	}

	// shouldFind := []peer.Peer{peers[1], peers[3]}
	found := []peer.Peer{}
	for nextp := range pchan {
		found = append(found, nextp)
	}

	// fmt.Printf("querying 0 (%s) FindPeersConnectedToPeer 2 (%s)\n", peers[0], peers[2])
	// fmt.Println("should find 1, 3", shouldFind)
	// fmt.Println("found", found)

	// testPeerListsMatch(t, shouldFind, found)

	log.Warning("TestFindPeersConnectedToPeer is not quite correct")
	if len(found) == 0 {
		t.Fatal("didn't find any peers.")
	}
}

func testPeerListsMatch(t *testing.T, p1, p2 []peer.Peer) {

	if len(p1) != len(p2) {
		t.Fatal("did not find as many peers as should have", p1, p2)
	}

	ids1 := make([]string, len(p1))
	ids2 := make([]string, len(p2))

	for i, p := range p1 {
		ids1[i] = p.ID().Pretty()
	}

	for i, p := range p2 {
		ids2[i] = p.ID().Pretty()
	}

	sort.Sort(sort.StringSlice(ids1))
	sort.Sort(sort.StringSlice(ids2))

	for i := range ids1 {
		if ids1[i] != ids2[i] {
			t.Fatal("Didnt find expected peer", ids1[i], ids2)
		}
	}
}

512
func TestConnectCollision(t *testing.T) {
513 514 515
	if testing.Short() {
		t.SkipNow()
	}
516

517
	runTimes := 10
518

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
522 523
		ctx := context.Background()
		u.Debug = false
524
		addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/11235")
525 526 527
		if err != nil {
			t.Fatal(err)
		}
528
		addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/15679")
529 530 531 532
		if err != nil {
			t.Fatal(err)
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
533 534 535 536 537 538 539 540
		peerA := makePeer(addrA)
		peerB := makePeer(addrB)

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

		done := make(chan struct{})
		go func() {
541
			err := dhtA.Connect(ctx, peerB)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
542 543 544 545 546 547
			if err != nil {
				t.Fatal(err)
			}
			done <- struct{}{}
		}()
		go func() {
548
			err := dhtB.Connect(ctx, peerA)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
			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
567 568
		dhtA.Close()
		dhtB.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
569 570
		dhtA.dialer.(inet.Network).Close()
		dhtB.dialer.(inet.Network).Close()
571 572

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