]> Sergey Matveev's repositories - btrtrc.git/blob - connection_test.go
Replace go-check with testify
[btrtrc.git] / connection_test.go
1 package torrent
2
3 import (
4         "testing"
5         "time"
6
7         "github.com/bradfitz/iter"
8         "github.com/stretchr/testify/assert"
9
10         "github.com/anacrolix/torrent/internal/pieceordering"
11         "github.com/anacrolix/torrent/peer_protocol"
12 )
13
14 func TestCancelRequestOptimized(t *testing.T) {
15         c := &connection{
16                 PeerMaxRequests: 1,
17                 PeerPieces:      []bool{false, true},
18                 post:            make(chan peer_protocol.Message),
19                 writeCh:         make(chan []byte),
20         }
21         if len(c.Requests) != 0 {
22                 t.FailNow()
23         }
24         // Keepalive timeout of 0 works because I'm just that good.
25         go c.writeOptimizer(0 * time.Millisecond)
26         c.Request(newRequest(1, 2, 3))
27         if len(c.Requests) != 1 {
28                 t.Fatal("request was not posted")
29         }
30         // Posting this message should removing the pending Request.
31         if !c.Cancel(newRequest(1, 2, 3)) {
32                 t.Fatal("request was not found")
33         }
34         // Check that the write optimization has filtered out the Request message.
35         for _, b := range []string{
36                 // The initial request triggers an Interested message.
37                 "\x00\x00\x00\x01\x02",
38                 // Let a keep-alive through to verify there were no pending messages.
39                 "\x00\x00\x00\x00",
40         } {
41                 bb := string(<-c.writeCh)
42                 if b != bb {
43                         t.Fatalf("received message %q is not expected: %q", bb, b)
44                 }
45         }
46         close(c.post)
47         // Drain the write channel until it closes.
48         for b := range c.writeCh {
49                 bs := string(b)
50                 if bs != "\x00\x00\x00\x00" {
51                         t.Fatal("got unexpected non-keepalive")
52                 }
53         }
54 }
55
56 func pieceOrderingAsSlice(po *pieceordering.Instance) (ret []int) {
57         for e := po.First(); e != nil; e = e.Next() {
58                 ret = append(ret, e.Piece())
59         }
60         return
61 }
62
63 func testRequestOrder(expected []int, ro *pieceordering.Instance, t *testing.T) {
64         assert.EqualValues(t, pieceOrderingAsSlice(ro), expected)
65 }
66
67 // Tests the request ordering based on a connections priorities.
68 func TestPieceRequestOrder(t *testing.T) {
69         c := connection{
70                 pieceRequestOrder: pieceordering.New(),
71                 piecePriorities:   []int{1, 4, 0, 3, 2},
72         }
73         testRequestOrder(nil, c.pieceRequestOrder, t)
74         c.pendPiece(2, PiecePriorityNone)
75         testRequestOrder(nil, c.pieceRequestOrder, t)
76         c.pendPiece(1, PiecePriorityNormal)
77         c.pendPiece(2, PiecePriorityNormal)
78         testRequestOrder([]int{2, 1}, c.pieceRequestOrder, t)
79         c.pendPiece(0, PiecePriorityNormal)
80         testRequestOrder([]int{2, 0, 1}, c.pieceRequestOrder, t)
81         c.pendPiece(1, PiecePriorityReadahead)
82         testRequestOrder([]int{1, 2, 0}, c.pieceRequestOrder, t)
83         c.pendPiece(4, PiecePriorityNow)
84         // now(4), r(1), normal(0, 2)
85         testRequestOrder([]int{4, 1, 2, 0}, c.pieceRequestOrder, t)
86         c.pendPiece(2, PiecePriorityReadahead)
87         // N(4), R(1, 2), N(0)
88         testRequestOrder([]int{4, 2, 1, 0}, c.pieceRequestOrder, t)
89         c.pendPiece(1, PiecePriorityNow)
90         // now(4, 1), readahead(2), normal(0)
91         // in the same order, the keys will be: -15+6, -15+12, -5, 1
92         // so we test that a very low priority (for this connection), "now"
93         // piece has been placed after a readahead piece.
94         testRequestOrder([]int{4, 2, 1, 0}, c.pieceRequestOrder, t)
95         // Note this intentially sets to None a piece that's not in the order.
96         for i := range iter.N(5) {
97                 c.pendPiece(i, PiecePriorityNone)
98         }
99         testRequestOrder(nil, c.pieceRequestOrder, t)
100 }