reflector.go/dht/dht_test.go

170 lines
3.5 KiB
Go
Raw Normal View History

2018-04-05 17:35:57 +02:00
package dht
import (
"net"
"sync"
"testing"
"time"
2018-06-14 17:48:02 +02:00
"github.com/lbryio/reflector.go/dht/bits"
2018-04-05 17:35:57 +02:00
)
func TestNodeFinder_FindNodes(t *testing.T) {
bs, dhts := TestingCreateDHT(t, 3, true, false)
2018-04-05 17:35:57 +02:00
defer func() {
for i := range dhts {
dhts[i].Shutdown()
}
2018-05-13 22:02:46 +02:00
bs.Shutdown()
2018-04-05 17:35:57 +02:00
}()
2018-06-14 17:48:02 +02:00
contacts, found, err := FindContacts(dhts[2].node, bits.Rand(), false, nil)
2018-04-05 17:35:57 +02:00
if err != nil {
t.Fatal(err)
}
if found {
t.Fatal("something was found, but it should not have been")
}
2018-06-13 18:45:47 +02:00
if len(contacts) != 3 {
t.Errorf("expected 3 node, found %d", len(contacts))
2018-04-05 17:35:57 +02:00
}
2018-05-13 22:02:46 +02:00
foundBootstrap := false
2018-04-05 17:35:57 +02:00
foundOne := false
2018-05-13 22:02:46 +02:00
foundTwo := false
2018-04-05 17:35:57 +02:00
2018-06-13 18:45:47 +02:00
for _, n := range contacts {
if n.ID.Equals(bs.id) {
2018-05-13 22:02:46 +02:00
foundBootstrap = true
}
if n.ID.Equals(dhts[0].node.id) {
2018-04-05 17:35:57 +02:00
foundOne = true
}
if n.ID.Equals(dhts[1].node.id) {
2018-05-13 22:02:46 +02:00
foundTwo = true
}
2018-04-05 17:35:57 +02:00
}
2018-05-13 22:02:46 +02:00
if !foundBootstrap {
t.Errorf("did not find bootstrap node %s", bs.id.Hex())
}
2018-04-05 17:35:57 +02:00
if !foundOne {
t.Errorf("did not find first node %s", dhts[0].node.id.Hex())
2018-04-05 17:35:57 +02:00
}
2018-05-13 22:02:46 +02:00
if !foundTwo {
t.Errorf("did not find second node %s", dhts[1].node.id.Hex())
}
}
func TestNodeFinder_FindNodes_NoBootstrap(t *testing.T) {
_, dhts := TestingCreateDHT(t, 3, false, false)
2018-05-13 22:02:46 +02:00
defer func() {
for i := range dhts {
dhts[i].Shutdown()
}
}()
2018-06-14 17:48:02 +02:00
_, _, err := FindContacts(dhts[2].node, bits.Rand(), false, nil)
2018-05-13 22:02:46 +02:00
if err == nil {
t.Fatal("contact finder should have errored saying that there are no contacts in the routing table")
}
2018-04-05 17:35:57 +02:00
}
func TestNodeFinder_FindValue(t *testing.T) {
bs, dhts := TestingCreateDHT(t, 3, true, false)
2018-04-05 17:35:57 +02:00
defer func() {
for i := range dhts {
dhts[i].Shutdown()
}
2018-05-13 22:02:46 +02:00
bs.Shutdown()
2018-04-05 17:35:57 +02:00
}()
2018-06-14 17:48:02 +02:00
blobHashToFind := bits.Rand()
nodeToFind := Contact{ID: bits.Rand(), IP: net.IPv4(1, 2, 3, 4), Port: 5678}
2018-04-28 02:16:12 +02:00
dhts[0].node.store.Upsert(blobHashToFind, nodeToFind)
2018-04-05 17:35:57 +02:00
2018-06-13 18:45:47 +02:00
contacts, found, err := FindContacts(dhts[2].node, blobHashToFind, true, nil)
2018-04-05 17:35:57 +02:00
if err != nil {
t.Fatal(err)
}
if !found {
t.Fatal("node was not found")
}
2018-06-13 18:45:47 +02:00
if len(contacts) != 1 {
t.Fatalf("expected one node, found %d", len(contacts))
2018-04-05 17:35:57 +02:00
}
2018-06-13 18:45:47 +02:00
if !contacts[0].ID.Equals(nodeToFind.ID) {
t.Fatalf("found node id %s, expected %s", contacts[0].ID.Hex(), nodeToFind.ID.Hex())
2018-04-05 17:35:57 +02:00
}
}
func TestDHT_LargeDHT(t *testing.T) {
nodes := 100
bs, dhts := TestingCreateDHT(t, nodes, true, true)
2018-04-05 17:35:57 +02:00
defer func() {
for _, d := range dhts {
go d.Shutdown()
}
2018-05-13 22:02:46 +02:00
bs.Shutdown()
2018-04-05 17:35:57 +02:00
time.Sleep(1 * time.Second)
}()
wg := &sync.WaitGroup{}
2018-06-14 17:48:02 +02:00
ids := make([]bits.Bitmap, nodes)
2018-05-14 03:38:30 +02:00
for i := range ids {
2018-06-14 17:48:02 +02:00
ids[i] = bits.Rand()
wg.Add(1)
go func(index int) {
2018-04-05 17:35:57 +02:00
defer wg.Done()
err := dhts[index].announce(ids[index])
if err != nil {
t.Error("error announcing random bitmap - ", err)
}
2018-04-05 17:35:57 +02:00
}(i)
}
wg.Wait()
2018-05-14 03:38:30 +02:00
// check that each node is in at learst 1 other routing table
2018-06-14 17:48:02 +02:00
rtCounts := make(map[bits.Bitmap]int)
2018-05-14 03:38:30 +02:00
for _, d := range dhts {
for _, d2 := range dhts {
if d.node.id.Equals(d2.node.id) {
continue
}
c := d2.node.rt.GetClosest(d.node.id, 1)
if len(c) > 1 {
t.Error("rt returned more than one node when only one requested")
} else if len(c) == 1 && c[0].ID.Equals(d.node.id) {
2018-05-14 03:38:30 +02:00
rtCounts[d.node.id]++
}
}
}
for k, v := range rtCounts {
if v == 0 {
t.Errorf("%s was not in any routing tables", k.HexShort())
}
}
// check that each ID is stored by at least 3 nodes
2018-06-14 17:48:02 +02:00
storeCounts := make(map[bits.Bitmap]int)
2018-05-14 03:38:30 +02:00
for _, d := range dhts {
for _, id := range ids {
if len(d.node.store.Get(id)) > 0 {
storeCounts[id]++
}
}
}
for k, v := range storeCounts {
if v == 0 {
t.Errorf("%s was not stored by any nodes", k.HexShort())
}
}
2018-04-05 17:35:57 +02:00
}