Exemplo n.º 1
0
func TestDaemonLoopConnectionErrors(t *testing.T) {
	d, quit := setupDaemonLoop()
	defer closeDaemon(d, quit)
	go d.Start(quit)
	d.pendingConnections[addr] = pex.NewPeer(addr)
	d.connectionErrors <- ConnectionError{addr, errors.New("failed")}
	wait()
	assert.Equal(t, len(d.pendingConnections), 0)
	assert.Nil(t, d.pendingConnections[addr])
}
Exemplo n.º 2
0
func testDaemonLoopClearOldPeersTicker(t *testing.T, d *Daemon, quit chan int,
	count int) {
	p := pex.NewPeer(addr)
	p.LastSeen = util.ZeroTime()
	d.Peers.Peers.Peerlist[addr] = p
	d.Peers.Config.CullRate = time.Millisecond * 10
	go d.Start(quit)
	time.Sleep(time.Millisecond * 15)
	assert.Equal(t, len(d.Peers.Peers.Peerlist), count)
}
Exemplo n.º 3
0
func TestDaemonLoopOnConnectEvent(t *testing.T) {
	d, quit := setupDaemonLoop()
	defer closeDaemon(d, quit)
	go d.Start(quit)
	d.pendingConnections[addr] = pex.NewPeer(addr)
	d.onConnectEvent <- ConnectEvent{addr, false}
	wait()
	assert.Equal(t, len(d.pendingConnections), 0)
	assert.Nil(t, d.pendingConnections[addr])
}
Exemplo n.º 4
0
func TestConnectToPeer(t *testing.T) {
	d := newDefaultDaemon()
	defer shutdown(d)
	defer cleanupPeers()
	d.Pool.Pool.Config.DialTimeout = 1 // nanosecond

	assert.Equal(t, len(d.Peers.Peers.Peerlist), 0)

	// PEX somehow has invalid peer
	p := &pex.Peer{Addr: "xcasca"}
	d.Peers.Peers.Peerlist["xcasca"] = p
	assert.NotPanics(t, func() { d.connectToPeer(p) })
	assert.Equal(t, len(d.pendingConnections), 0)
	assert.Equal(t, len(d.connectionErrors), 0)
	delete(d.Peers.Peers.Peerlist, "xcasca")

	// Disabled outgoing conns
	d.Config.DisableOutgoingConnections = true
	p, err := d.Peers.Peers.AddPeer(addr)
	assert.Nil(t, err)
	assert.NotPanics(t, func() { d.connectToPeer(p) })
	assert.Equal(t, len(d.pendingConnections), 0)
	assert.Equal(t, len(d.connectionErrors), 0)
	d.Config.DisableOutgoingConnections = false

	// Localhost only, and peer isn't
	d.Config.LocalhostOnly = true
	assert.NotPanics(t, func() { d.connectToPeer(p) })
	assert.Equal(t, len(d.pendingConnections), 0)
	assert.Equal(t, len(d.connectionErrors), 0)
	delete(d.Peers.Peers.Peerlist, addr)

	// Valid attempt to connect to localhost
	d.Peers.Peers.AllowLocalhost = true
	p, err = d.Peers.Peers.AddPeer(localAddr)
	assert.Nil(t, err)
	if err != nil {
		t.Logf("Error with local addr: %v\n", err)
	}
	t.Logf("Peerlist: %v\n", d.Peers.Peers.Peerlist)
	assert.NotPanics(t, func() { d.connectToPeer(p) })
	wait()
	assert.Equal(t, len(d.pendingConnections), 1)
	assert.Equal(t, len(d.connectionErrors), 1)
	if len(d.connectionErrors) == 0 {
		shutdown(d)
		cleanupPeers()
		t.Fatalf("connectionErrors empty, would block")
	}
	ce := <-d.connectionErrors
	assert.Equal(t, ce.Addr, localAddr)
	assert.NotNil(t, ce.Error)
	delete(d.pendingConnections, localAddr)
	delete(d.Peers.Peers.Peerlist, localAddr)
	d.Config.LocalhostOnly = false
	d.Peers.Peers.AllowLocalhost = false

	// Valid attempt to connect
	p, err = d.Peers.Peers.AddPeer(addr)
	assert.Nil(t, err)
	assert.NotPanics(t, func() { d.connectToPeer(p) })
	wait()
	assert.Equal(t, len(d.pendingConnections), 1)
	assert.Equal(t, len(d.connectionErrors), 1)
	if len(d.connectionErrors) == 0 {
		t.Fatalf("connectionErrors empty, would block")
	}
	ce = <-d.connectionErrors
	assert.Equal(t, ce.Addr, addr)
	assert.NotNil(t, ce.Error)
	delete(d.pendingConnections, addr)

	// Two peers, one successful connect attempt and one skipped
	p, err = d.Peers.Peers.AddPeer(addrb)
	assert.Nil(t, err)
	assert.NotPanics(t, func() { d.connectToPeer(p) })
	wait()
	assert.Equal(t, len(d.pendingConnections), 1)
	assert.Equal(t, len(d.connectionErrors), 1)
	if len(d.connectionErrors) == 0 {
		t.Fatalf("connectionErrors empty, would block")
	}
	ce = <-d.connectionErrors
	assert.True(t, (ce.Addr == addr) || (ce.Addr == addrb))
	assert.NotNil(t, ce.Error)
	delete(d.pendingConnections, addr)
	delete(d.pendingConnections, addrb)
	delete(d.Peers.Peers.Peerlist, addrb)

	// Already connected, skip
	p, err = d.Peers.Peers.AddPeer(addr)
	assert.Nil(t, err)
	d.Pool.Pool.Addresses[addr] = gnetConnection(addr)
	assert.NotPanics(t, func() { d.connectToPeer(p) })
	assert.Equal(t, len(d.pendingConnections), 0)
	assert.Equal(t, len(d.connectionErrors), 0)
	delete(d.Pool.Pool.Addresses, addr)

	// Pending connection, skip
	p = pex.NewPeer(addr)
	d.pendingConnections[addr] = p
	assert.NotPanics(t, func() { d.connectToPeer(p) })
	assert.Equal(t, len(d.pendingConnections), 1)
	assert.Equal(t, len(d.connectionErrors), 0)
	delete(d.pendingConnections, addr)

	// Already connected to this base IP at least once, skip
	d.ipCounts[addrIP] = 1
	assert.NotPanics(t, func() { d.connectToPeer(p) })
	assert.Equal(t, len(d.ipCounts), 1)
	assert.Equal(t, d.ipCounts[addrIP], 1)
	assert.Equal(t, len(d.pendingConnections), 0)
	assert.Equal(t, len(d.connectionErrors), 0)
	delete(d.ipCounts, addrIP)
}