table_test.go 4.1 KB
Newer Older
1 2 3 4 5 6 7
package dht

import (
	crand "crypto/rand"
	"crypto/sha256"
	"math/rand"
	"testing"
8
	"time"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

	peer "github.com/jbenet/go-ipfs/peer"
)

func _randPeer() *peer.Peer {
	p := new(peer.Peer)
	p.ID = make(peer.ID, 16)
	crand.Read(p.ID)
	return p
}

func _randID() ID {
	buf := make([]byte, 16)
	crand.Read(buf)

	hash := sha256.Sum256(buf)
	return ID(hash[:])
}

// Test basic features of the bucket struct
func TestBucket(t *testing.T) {
30
	b := newBucket()
31 32 33 34

	peers := make([]*peer.Peer, 100)
	for i := 0; i < 100; i++ {
		peers[i] = _randPeer()
35
		b.pushFront(peers[i])
36 37 38
	}

	local := _randPeer()
Chas Leichner's avatar
Chas Leichner committed
39
	localID := ConvertPeerID(local.ID)
40 41

	i := rand.Intn(len(peers))
42
	e := b.find(peers[i].ID)
43 44 45 46
	if e == nil {
		t.Errorf("Failed to find peer: %v", peers[i])
	}

Chas Leichner's avatar
Chas Leichner committed
47
	spl := b.Split(0, ConvertPeerID(local.ID))
48
	llist := b.list
49
	for e := llist.Front(); e != nil; e = e.Next() {
Chas Leichner's avatar
Chas Leichner committed
50
		p := ConvertPeerID(e.Value.(*peer.Peer).ID)
51
		cpl := xor(p, localID).commonPrefixLen()
52 53 54 55 56
		if cpl > 0 {
			t.Fatalf("Split failed. found id with cpl > 0 in 0 bucket")
		}
	}

57
	rlist := spl.list
58
	for e := rlist.Front(); e != nil; e = e.Next() {
Chas Leichner's avatar
Chas Leichner committed
59
		p := ConvertPeerID(e.Value.(*peer.Peer).ID)
60
		cpl := xor(p, localID).commonPrefixLen()
61 62 63 64 65 66 67 68 69
		if cpl == 0 {
			t.Fatalf("Split failed. found id with cpl == 0 in non 0 bucket")
		}
	}
}

// Right now, this just makes sure that it doesnt hang or crash
func TestTableUpdate(t *testing.T) {
	local := _randPeer()
Chas Leichner's avatar
Chas Leichner committed
70
	rt := NewRoutingTable(10, ConvertPeerID(local.ID), time.Hour)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

	peers := make([]*peer.Peer, 100)
	for i := 0; i < 100; i++ {
		peers[i] = _randPeer()
	}

	// Testing Update
	for i := 0; i < 10000; i++ {
		p := rt.Update(peers[rand.Intn(len(peers))])
		if p != nil {
			t.Log("evicted peer.")
		}
	}

	for i := 0; i < 100; i++ {
		id := _randID()
		ret := rt.NearestPeers(id, 5)
		if len(ret) == 0 {
			t.Fatal("Failed to find node near ID.")
		}
	}
}

func TestTableFind(t *testing.T) {
	local := _randPeer()
Chas Leichner's avatar
Chas Leichner committed
96
	rt := NewRoutingTable(10, ConvertPeerID(local.ID), time.Hour)
97 98 99 100 101 102 103 104

	peers := make([]*peer.Peer, 100)
	for i := 0; i < 5; i++ {
		peers[i] = _randPeer()
		rt.Update(peers[i])
	}

	t.Logf("Searching for peer: '%s'", peers[2].ID.Pretty())
Chas Leichner's avatar
Chas Leichner committed
105
	found := rt.NearestPeer(ConvertPeerID(peers[2].ID))
106 107 108 109 110 111 112
	if !found.ID.Equal(peers[2].ID) {
		t.Fatalf("Failed to lookup known node...")
	}
}

func TestTableFindMultiple(t *testing.T) {
	local := _randPeer()
Chas Leichner's avatar
Chas Leichner committed
113
	rt := NewRoutingTable(20, ConvertPeerID(local.ID), time.Hour)
114 115 116 117 118 119 120 121

	peers := make([]*peer.Peer, 100)
	for i := 0; i < 18; i++ {
		peers[i] = _randPeer()
		rt.Update(peers[i])
	}

	t.Logf("Searching for peer: '%s'", peers[2].ID.Pretty())
Chas Leichner's avatar
Chas Leichner committed
122
	found := rt.NearestPeers(ConvertPeerID(peers[2].ID), 15)
123 124 125 126
	if len(found) != 15 {
		t.Fatalf("Got back different number of peers than we expected.")
	}
}
127 128 129 130 131 132

// Looks for race conditions in table operations. For a more 'certain'
// test, increase the loop counter from 1000 to a much higher number
// and set GOMAXPROCS above 1
func TestTableMultithreaded(t *testing.T) {
	local := peer.ID("localPeer")
Chas Leichner's avatar
Chas Leichner committed
133
	tab := NewRoutingTable(20, ConvertPeerID(local), time.Hour)
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 168 169
	var peers []*peer.Peer
	for i := 0; i < 500; i++ {
		peers = append(peers, _randPeer())
	}

	done := make(chan struct{})
	go func() {
		for i := 0; i < 1000; i++ {
			n := rand.Intn(len(peers))
			tab.Update(peers[n])
		}
		done <- struct{}{}
	}()

	go func() {
		for i := 0; i < 1000; i++ {
			n := rand.Intn(len(peers))
			tab.Update(peers[n])
		}
		done <- struct{}{}
	}()

	go func() {
		for i := 0; i < 1000; i++ {
			n := rand.Intn(len(peers))
			tab.Find(peers[n].ID)
		}
		done <- struct{}{}
	}()
	<-done
	<-done
	<-done
}

func BenchmarkUpdates(b *testing.B) {
	b.StopTimer()
Chas Leichner's avatar
Chas Leichner committed
170 171
	local := ConvertKey("localKey")
	tab := NewRoutingTable(20, local, time.Hour)
172 173 174 175 176 177 178 179 180 181 182 183 184 185

	var peers []*peer.Peer
	for i := 0; i < b.N; i++ {
		peers = append(peers, _randPeer())
	}

	b.StartTimer()
	for i := 0; i < b.N; i++ {
		tab.Update(peers[i])
	}
}

func BenchmarkFinds(b *testing.B) {
	b.StopTimer()
Chas Leichner's avatar
Chas Leichner committed
186 187
	local := ConvertKey("localKey")
	tab := NewRoutingTable(20, local, time.Hour)
188 189 190 191 192 193 194 195 196 197 198 199

	var peers []*peer.Peer
	for i := 0; i < b.N; i++ {
		peers = append(peers, _randPeer())
		tab.Update(peers[i])
	}

	b.StartTimer()
	for i := 0; i < b.N; i++ {
		tab.Find(peers[i].ID)
	}
}