]> Sergey Matveev's repositories - btrtrc.git/blob - prioritized_peers_test.go
Trace some webrtcNetConn use
[btrtrc.git] / prioritized_peers_test.go
1 package torrent
2
3 import (
4         "net"
5         "testing"
6
7         "github.com/google/btree"
8         "github.com/stretchr/testify/assert"
9 )
10
11 func TestPrioritizedPeers(t *testing.T) {
12         pp := prioritizedPeers{
13                 om: btree.New(3),
14                 getPrio: func(p PeerInfo) peerPriority {
15                         return bep40PriorityIgnoreError(p.addr(), IpPort{IP: net.ParseIP("0.0.0.0")})
16                 },
17         }
18         _, ok := pp.DeleteMin()
19         assert.Panics(t, func() { pp.PopMax() })
20         assert.False(t, ok)
21         ps := []PeerInfo{
22                 {Addr: ipPortAddr{IP: net.ParseIP("1.2.3.4")}},
23                 {Addr: ipPortAddr{IP: net.ParseIP("1::2")}},
24                 {Addr: ipPortAddr{IP: net.ParseIP("")}},
25                 {Addr: ipPortAddr{IP: net.ParseIP("")}, Trusted: true},
26         }
27         for i, p := range ps {
28                 t.Logf("peer %d priority: %08x trusted: %t\n", i, pp.getPrio(p), p.Trusted)
29                 assert.False(t, pp.Add(p))
30                 assert.True(t, pp.Add(p))
31                 assert.Equal(t, i+1, pp.Len())
32         }
33         pop := func(expected *PeerInfo) {
34                 if expected == nil {
35                         assert.Panics(t, func() { pp.PopMax() })
36                 } else {
37                         assert.Equal(t, *expected, pp.PopMax())
38                 }
39         }
40         min := func(expected *PeerInfo) {
41                 i, ok := pp.DeleteMin()
42                 if expected == nil {
43                         assert.False(t, ok)
44                 } else {
45                         assert.True(t, ok)
46                         assert.Equal(t, *expected, i.p)
47                 }
48         }
49         pop(&ps[3])
50         pop(&ps[1])
51         min(&ps[2])
52         pop(&ps[0])
53         min(nil)
54         pop(nil)
55 }