reflector.go/dht/dht_test.go

145 lines
3 KiB
Go
Raw Normal View History

2018-04-05 17:35:57 +02:00
package dht
import (
"math/rand"
"net"
"sync"
"testing"
"time"
log "github.com/sirupsen/logrus"
)
2018-04-25 00:12:17 +02:00
// TODO: make a dht with X nodes, have them all join, then ensure that every node appears at least once in another node's routing table
2018-04-05 17:35:57 +02:00
func TestNodeFinder_FindNodes(t *testing.T) {
2018-05-13 22:02:46 +02:00
bs, dhts := TestingCreateDHT(3)
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-04-28 02:16:12 +02:00
nf := newContactFinder(dhts[2].node, RandomBitmapP(), false)
2018-04-05 17:35:57 +02:00
res, err := nf.Find()
if err != nil {
t.Fatal(err)
}
2018-04-28 02:16:12 +02:00
foundNodes, found := res.Contacts, res.Found
2018-04-05 17:35:57 +02:00
if found {
t.Fatal("something was found, but it should not have been")
}
2018-05-13 22:02:46 +02:00
if len(foundNodes) != 3 {
t.Errorf("expected 3 node, found %d", len(foundNodes))
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
for _, n := range foundNodes {
2018-05-13 22:02:46 +02:00
if n.id.Equals(bs.id) {
foundBootstrap = true
}
2018-04-05 17:35:57 +02:00
if n.id.Equals(dhts[0].node.id) {
foundOne = true
}
2018-05-13 22:02:46 +02:00
if n.id.Equals(dhts[1].node.id) {
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 := TestingCreateDHTNoBootstrap(3, nil)
defer func() {
for i := range dhts {
dhts[i].Shutdown()
}
}()
nf := newContactFinder(dhts[2].node, RandomBitmapP(), false)
_, err := nf.Find()
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) {
2018-05-13 22:02:46 +02:00
bs, dhts := TestingCreateDHT(3)
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
}()
blobHashToFind := RandomBitmapP()
2018-04-28 02:16:12 +02:00
nodeToFind := Contact{id: RandomBitmapP(), ip: net.IPv4(1, 2, 3, 4), port: 5678}
dhts[0].node.store.Upsert(blobHashToFind, nodeToFind)
2018-04-05 17:35:57 +02:00
2018-04-28 02:16:12 +02:00
nf := newContactFinder(dhts[2].node, blobHashToFind, true)
2018-04-05 17:35:57 +02:00
res, err := nf.Find()
if err != nil {
t.Fatal(err)
}
2018-04-28 02:16:12 +02:00
foundNodes, found := res.Contacts, res.Found
2018-04-05 17:35:57 +02:00
if !found {
t.Fatal("node was not found")
}
if len(foundNodes) != 1 {
t.Fatalf("expected one node, found %d", len(foundNodes))
}
if !foundNodes[0].id.Equals(nodeToFind.id) {
t.Fatalf("found node id %s, expected %s", foundNodes[0].id.Hex(), nodeToFind.id.Hex())
}
}
func TestDHT_LargeDHT(t *testing.T) {
rand.Seed(time.Now().UnixNano())
log.Println("if this takes longer than 20 seconds, its stuck. idk why it gets stuck sometimes, but its a bug.")
nodes := 100
2018-05-13 22:02:46 +02:00
bs, dhts := TestingCreateDHT(nodes)
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{}
numIDs := nodes / 2
ids := make([]Bitmap, numIDs)
2018-04-05 17:35:57 +02:00
for i := 0; i < numIDs; i++ {
ids[i] = RandomBitmapP()
2018-04-05 17:35:57 +02:00
}
for i := 0; i < numIDs; i++ {
go func(i int) {
r := rand.Intn(nodes)
wg.Add(1)
defer wg.Done()
dhts[r].Announce(ids[i])
}(i)
}
wg.Wait()
2018-05-13 22:02:46 +02:00
dhts[len(dhts)-1].PrintState()
2018-04-05 17:35:57 +02:00
}