2014-05-28 23:27:48 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2014-12-26 14:17:00 +08:00
|
|
|
|
2015-03-20 13:37:44 +08:00
|
|
|
"github.com/anacrolix/torrent/peer_protocol"
|
2014-05-28 23:27:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCancelRequestOptimized(t *testing.T) {
|
|
|
|
c := &connection{
|
|
|
|
PeerMaxRequests: 1,
|
|
|
|
PeerPieces: []bool{false, true},
|
|
|
|
post: make(chan peer_protocol.Message),
|
2014-07-18 00:37:33 +08:00
|
|
|
writeCh: make(chan []byte),
|
2014-05-28 23:27:48 +08: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-18 00:37:33 +08:00
|
|
|
bb := string(<-c.writeCh)
|
2014-05-28 23:27:48 +08:00
|
|
|
if b != bb {
|
|
|
|
t.Fatalf("received message %q is not expected: %q", bb, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(c.post)
|
2014-06-26 15:30:54 +08:00
|
|
|
// Drain the write channel until it closes.
|
2014-07-18 00:37:33 +08:00
|
|
|
for b := range c.writeCh {
|
2014-06-26 15:30:54 +08:00
|
|
|
bs := string(b)
|
|
|
|
if bs != "\x00\x00\x00\x00" {
|
|
|
|
t.Fatal("got unexpected non-keepalive")
|
|
|
|
}
|
2014-05-28 23:27:48 +08:00
|
|
|
}
|
|
|
|
}
|