]> Sergey Matveev's repositories - btrtrc.git/blob - connection_test.go
Reorder some imports
[btrtrc.git] / connection_test.go
1 package torrent
2
3 import (
4         "testing"
5         "time"
6
7         "bitbucket.org/anacrolix/go.torrent/peer_protocol"
8 )
9
10 func TestCancelRequestOptimized(t *testing.T) {
11         c := &connection{
12                 PeerMaxRequests: 1,
13                 PeerPieces:      []bool{false, true},
14                 post:            make(chan peer_protocol.Message),
15                 writeCh:         make(chan []byte),
16         }
17         if len(c.Requests) != 0 {
18                 t.FailNow()
19         }
20         // Keepalive timeout of 0 works because I'm just that good.
21         go c.writeOptimizer(0 * time.Millisecond)
22         c.Request(newRequest(1, 2, 3))
23         if len(c.Requests) != 1 {
24                 t.Fatal("request was not posted")
25         }
26         // Posting this message should removing the pending Request.
27         if !c.Cancel(newRequest(1, 2, 3)) {
28                 t.Fatal("request was not found")
29         }
30         // Check that the write optimization has filtered out the Request message.
31         for _, b := range []string{
32                 // The initial request triggers an Interested message.
33                 "\x00\x00\x00\x01\x02",
34                 // Let a keep-alive through to verify there were no pending messages.
35                 "\x00\x00\x00\x00",
36         } {
37                 bb := string(<-c.writeCh)
38                 if b != bb {
39                         t.Fatalf("received message %q is not expected: %q", bb, b)
40                 }
41         }
42         close(c.post)
43         // Drain the write channel until it closes.
44         for b := range c.writeCh {
45                 bs := string(b)
46                 if bs != "\x00\x00\x00\x00" {
47                         t.Fatal("got unexpected non-keepalive")
48                 }
49         }
50 }