2018-04-14 19:44:03 +08:00
|
|
|
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),
|
2020-05-29 17:44:48 +08:00
|
|
|
getPrio: func(p PeerInfo) peerPriority {
|
2018-11-16 07:35:30 +08:00
|
|
|
return bep40PriorityIgnoreError(p.addr(), IpPort{IP: net.ParseIP("0.0.0.0")})
|
2018-04-14 19:44:03 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
_, ok := pp.DeleteMin()
|
|
|
|
assert.Panics(t, func() { pp.PopMax() })
|
|
|
|
assert.False(t, ok)
|
2020-05-29 17:44:48 +08:00
|
|
|
ps := []PeerInfo{
|
2020-02-20 13:47:37 +08:00
|
|
|
{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},
|
2018-04-14 19:44:03 +08:00
|
|
|
}
|
|
|
|
for i, p := range ps {
|
2019-12-23 11:04:07 +08:00
|
|
|
t.Logf("peer %d priority: %08x trusted: %t\n", i, pp.getPrio(p), p.Trusted)
|
2018-04-14 19:44:03 +08:00
|
|
|
assert.False(t, pp.Add(p))
|
|
|
|
assert.True(t, pp.Add(p))
|
|
|
|
assert.Equal(t, i+1, pp.Len())
|
|
|
|
}
|
2020-05-29 17:44:48 +08:00
|
|
|
pop := func(expected *PeerInfo) {
|
2018-04-14 19:44:03 +08:00
|
|
|
if expected == nil {
|
|
|
|
assert.Panics(t, func() { pp.PopMax() })
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, *expected, pp.PopMax())
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 17:44:48 +08:00
|
|
|
min := func(expected *PeerInfo) {
|
2018-04-14 19:44:03 +08:00
|
|
|
i, ok := pp.DeleteMin()
|
|
|
|
if expected == nil {
|
|
|
|
assert.False(t, ok)
|
|
|
|
} else {
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, *expected, i.p)
|
|
|
|
}
|
|
|
|
}
|
2019-12-23 11:04:07 +08:00
|
|
|
pop(&ps[3])
|
2018-04-14 19:44:03 +08:00
|
|
|
pop(&ps[1])
|
2019-12-23 11:04:07 +08:00
|
|
|
min(&ps[2])
|
|
|
|
pop(&ps[0])
|
2018-04-14 19:44:03 +08:00
|
|
|
min(nil)
|
|
|
|
pop(nil)
|
|
|
|
}
|