2014-05-28 15:27:48 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2014-12-26 06:17:00 +00:00
|
|
|
|
2015-03-18 07:37:52 +00:00
|
|
|
"github.com/bradfitz/iter"
|
2015-08-03 06:23:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-03-18 07:37:52 +00:00
|
|
|
|
2015-03-20 05:37:44 +00:00
|
|
|
"github.com/anacrolix/torrent/internal/pieceordering"
|
|
|
|
"github.com/anacrolix/torrent/peer_protocol"
|
2014-05-28 15:27:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCancelRequestOptimized(t *testing.T) {
|
|
|
|
c := &connection{
|
|
|
|
PeerMaxRequests: 1,
|
|
|
|
PeerPieces: []bool{false, true},
|
|
|
|
post: make(chan peer_protocol.Message),
|
2014-07-17 16:37:33 +00:00
|
|
|
writeCh: make(chan []byte),
|
2014-05-28 15:27:48 +00:00
|
|
|
}
|
|
|
|
if len(c.Requests) != 0 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Keepalive timeout of 0 works because I'm just that good.
|
|
|
|
go c.writeOptimizer(0 * time.Millisecond)
|
|
|
|
c.Request(newRequest(1, 2, 3))
|
|
|
|
if len(c.Requests) != 1 {
|
|
|
|
t.Fatal("request was not posted")
|
|
|
|
}
|
|
|
|
// Posting this message should removing the pending Request.
|
|
|
|
if !c.Cancel(newRequest(1, 2, 3)) {
|
|
|
|
t.Fatal("request was not found")
|
|
|
|
}
|
|
|
|
// Check that the write optimization has filtered out the Request message.
|
|
|
|
for _, b := range []string{
|
|
|
|
// The initial request triggers an Interested message.
|
|
|
|
"\x00\x00\x00\x01\x02",
|
|
|
|
// Let a keep-alive through to verify there were no pending messages.
|
|
|
|
"\x00\x00\x00\x00",
|
|
|
|
} {
|
2014-07-17 16:37:33 +00:00
|
|
|
bb := string(<-c.writeCh)
|
2014-05-28 15:27:48 +00:00
|
|
|
if b != bb {
|
|
|
|
t.Fatalf("received message %q is not expected: %q", bb, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(c.post)
|
2014-06-26 07:30:54 +00:00
|
|
|
// Drain the write channel until it closes.
|
2014-07-17 16:37:33 +00:00
|
|
|
for b := range c.writeCh {
|
2014-06-26 07:30:54 +00:00
|
|
|
bs := string(b)
|
|
|
|
if bs != "\x00\x00\x00\x00" {
|
|
|
|
t.Fatal("got unexpected non-keepalive")
|
|
|
|
}
|
2014-05-28 15:27:48 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-18 07:37:52 +00:00
|
|
|
|
2015-03-19 23:52:55 +00:00
|
|
|
func pieceOrderingAsSlice(po *pieceordering.Instance) (ret []int) {
|
|
|
|
for e := po.First(); e != nil; e = e.Next() {
|
|
|
|
ret = append(ret, e.Piece())
|
2015-03-18 07:37:52 +00:00
|
|
|
}
|
2015-03-19 23:52:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-03 06:23:05 +00:00
|
|
|
func testRequestOrder(expected []int, ro *pieceordering.Instance, t *testing.T) {
|
|
|
|
assert.EqualValues(t, pieceOrderingAsSlice(ro), expected)
|
2015-03-18 07:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests the request ordering based on a connections priorities.
|
2015-08-03 06:23:05 +00:00
|
|
|
func TestPieceRequestOrder(t *testing.T) {
|
2015-03-18 07:37:52 +00:00
|
|
|
c := connection{
|
|
|
|
pieceRequestOrder: pieceordering.New(),
|
|
|
|
piecePriorities: []int{1, 4, 0, 3, 2},
|
|
|
|
}
|
|
|
|
testRequestOrder(nil, c.pieceRequestOrder, t)
|
2015-09-26 07:27:35 +00:00
|
|
|
c.pendPiece(2, PiecePriorityNone, nil)
|
2015-03-18 07:37:52 +00:00
|
|
|
testRequestOrder(nil, c.pieceRequestOrder, t)
|
2015-09-26 07:27:35 +00:00
|
|
|
c.pendPiece(1, PiecePriorityNormal, nil)
|
|
|
|
c.pendPiece(2, PiecePriorityNormal, nil)
|
2015-03-18 07:37:52 +00:00
|
|
|
testRequestOrder([]int{2, 1}, c.pieceRequestOrder, t)
|
2015-09-26 07:27:35 +00:00
|
|
|
c.pendPiece(0, PiecePriorityNormal, nil)
|
2015-03-18 07:37:52 +00:00
|
|
|
testRequestOrder([]int{2, 0, 1}, c.pieceRequestOrder, t)
|
2015-09-26 07:27:35 +00:00
|
|
|
c.pendPiece(1, PiecePriorityReadahead, nil)
|
2015-03-18 07:37:52 +00:00
|
|
|
testRequestOrder([]int{1, 2, 0}, c.pieceRequestOrder, t)
|
2015-09-26 07:27:35 +00:00
|
|
|
c.pendPiece(4, PiecePriorityNow, nil)
|
2015-03-19 23:52:55 +00:00
|
|
|
// now(4), r(1), normal(0, 2)
|
2015-03-18 07:37:52 +00:00
|
|
|
testRequestOrder([]int{4, 1, 2, 0}, c.pieceRequestOrder, t)
|
2015-09-26 07:27:35 +00:00
|
|
|
c.pendPiece(2, PiecePriorityReadahead, nil)
|
2015-03-18 07:37:52 +00:00
|
|
|
// N(4), R(1, 2), N(0)
|
2015-03-19 23:52:55 +00:00
|
|
|
testRequestOrder([]int{4, 2, 1, 0}, c.pieceRequestOrder, t)
|
2015-09-26 07:27:35 +00:00
|
|
|
c.pendPiece(1, PiecePriorityNow, nil)
|
2015-03-19 23:52:55 +00:00
|
|
|
// now(4, 1), readahead(2), normal(0)
|
|
|
|
// in the same order, the keys will be: -15+6, -15+12, -5, 1
|
|
|
|
// so we test that a very low priority (for this connection), "now"
|
|
|
|
// piece has been placed after a readahead piece.
|
|
|
|
testRequestOrder([]int{4, 2, 1, 0}, c.pieceRequestOrder, t)
|
2015-03-18 07:37:52 +00:00
|
|
|
// Note this intentially sets to None a piece that's not in the order.
|
|
|
|
for i := range iter.N(5) {
|
2015-09-26 07:27:35 +00:00
|
|
|
c.pendPiece(i, PiecePriorityNone, nil)
|
2015-03-18 07:37:52 +00:00
|
|
|
}
|
|
|
|
testRequestOrder(nil, c.pieceRequestOrder, t)
|
|
|
|
}
|