forked from nictuku/dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht_test.go
148 lines (135 loc) · 3.45 KB
/
dht_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package dht
import (
"expvar"
"fmt"
"math/rand"
"net"
"strconv"
"testing"
"time"
"github.com/nictuku/nettools"
)
func ExampleDHT() {
port := rand.Intn(10000) + 40000
d, err := NewDHTNode(port, 100, false)
if err != nil {
fmt.Println(err)
return
}
go d.DoDHT()
infoHash, err := DecodeInfoHash("d1c5676ae7ac98e8b19f63565905105e3c4c37a2")
if err != nil {
fmt.Printf("DecodeInfoHash faiure: %v", err)
return
}
// Give the DHT some time to "warm-up" its routing table.
time.Sleep(5 * time.Second)
d.PeersRequest(string(infoHash), false)
timeout := time.After(30 * time.Second)
var infoHashPeers map[InfoHash][]string
select {
case infoHashPeers = <-d.PeersRequestResults:
break
case <-timeout:
fmt.Printf("Could not find new peers: timed out")
return
}
for ih, peers := range infoHashPeers {
if len(peers) > 0 {
fmt.Printf("peer found for infohash [%x]\n", ih)
// Peers are encoded in binary format. Decoding example using github.com/nictuku/nettools:
// for _, peer := range peers {
// fmt.Println(DecodePeerAddress(peer))
// }
return
}
}
// Output:
// peer found for infohash [d1c5676ae7ac98e8b19f63565905105e3c4c37a2]
}
func startDHTNode(t *testing.T) *DHT {
port := rand.Intn(10000) + 40000
node, err := NewDHTNode(port, 100, false)
node.nodeId = "abcdefghij0123456789"
if err != nil {
t.Errorf("NewDHTNode(): %v", err)
}
go node.DoDHT()
return node
}
// Requires Internet access and can be flaky if the server or the internet is
// slow.
func TestDHTLarge(t *testing.T) {
node := startDHTNode(t)
realDHTNodes := []string{
"1.a.magnets.im",
}
for _, addr := range realDHTNodes {
ip, err := net.LookupHost(addr)
if err != nil {
t.Error(err)
continue
}
node.AddNode(ip[0] + ":6881")
}
// Test that we can reach at least one node.
success := false
var (
reachable int
v expvar.Var
err error
)
for i := 0; i < 10; i++ {
v = expvar.Get("totalReachableNodes")
reachable, err = strconv.Atoi(v.String())
if err != nil {
t.Errorf("totalReachableNodes conversion to int failed: %v", err)
continue
}
if reachable > 0 {
t.Logf("Contacted %d DHT nodes.", reachable)
success = true
break
}
time.Sleep(time.Second)
}
if !success {
t.Fatal("No external DHT node could be contacted.")
}
// Test that we can find peers for a known torrent in a timely fashion.
//
// Torrent from: http://www.clearbits.net/torrents/244-time-management-for-anarchists-1
infoHash := InfoHash("\xb4\x62\xc0\xa8\xbc\xef\x1c\xe5\xbb\x56\xb9\xfd\xb8\xcf\x37\xff\xd0\x2f\x5f\x59")
go node.PeersRequest(string(infoHash), true)
timeout := make(chan bool, 1)
go func() {
time.Sleep(10 * time.Second)
timeout <- true
}()
var infoHashPeers map[InfoHash][]string
select {
case infoHashPeers = <-node.PeersRequestResults:
t.Logf("Found %d peers.", len(infoHashPeers[infoHash]))
case <-timeout:
t.Fatal("Could not find new peers: timed out")
}
for ih, peers := range infoHashPeers {
if infoHash != ih {
t.Fatal("Unexpected infohash returned")
}
if len(peers) == 0 {
t.Fatal("Could not find new torrent peers.")
}
for _, peer := range peers {
t.Logf("peer found: %v", nettools.BinaryToDottedPort(peer))
}
}
t.Logf("=== Stats ===")
t.Logf("totalReachableNodes: %v", totalReachableNodes)
t.Logf("totalDupes: %v", totalDupes)
t.Logf("totalPeers: %v", totalPeers)
t.Logf("totalSentGetPeers: %v", totalSentGetPeers)
}
func init() {
rand.Seed((time.Now().Unix() % (1e9 - 1)))
}