-
-
Notifications
You must be signed in to change notification settings - Fork 629
/
prioritized-peers_test.go
55 lines (52 loc) · 1.22 KB
/
prioritized-peers_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
package torrent
import (
"net"
"testing"
"github.com/google/btree"
"github.com/stretchr/testify/assert"
)
func TestPrioritizedPeers(t *testing.T) {
pp := prioritizedPeers{
om: btree.New(3),
getPrio: func(p PeerInfo) peerPriority {
return bep40PriorityIgnoreError(p.addr(), IpPort{IP: net.ParseIP("0.0.0.0")})
},
}
_, ok := pp.DeleteMin()
assert.Panics(t, func() { pp.PopMax() })
assert.False(t, ok)
ps := []PeerInfo{
{Addr: ipPortAddr{IP: net.ParseIP("1.2.3.4")}},
{Addr: ipPortAddr{IP: net.ParseIP("1::2")}},
{Addr: ipPortAddr{IP: net.ParseIP("")}},
{Addr: ipPortAddr{IP: net.ParseIP("")}, Trusted: true},
}
for i, p := range ps {
t.Logf("peer %d priority: %08x trusted: %t\n", i, pp.getPrio(p), p.Trusted)
assert.False(t, pp.Add(p))
assert.True(t, pp.Add(p))
assert.Equal(t, i+1, pp.Len())
}
pop := func(expected *PeerInfo) {
if expected == nil {
assert.Panics(t, func() { pp.PopMax() })
} else {
assert.Equal(t, *expected, pp.PopMax())
}
}
min := func(expected *PeerInfo) {
i, ok := pp.DeleteMin()
if expected == nil {
assert.False(t, ok)
} else {
assert.True(t, ok)
assert.Equal(t, *expected, i.p)
}
}
pop(&ps[3])
pop(&ps[1])
min(&ps[2])
pop(&ps[0])
min(nil)
pop(nil)
}