7 "bitbucket.org/anacrolix/go.torrent/peer_protocol"
10 func TestCancelRequestOptimized(t *testing.T) {
13 PeerPieces: []bool{false, true},
14 post: make(chan peer_protocol.Message),
15 writeCh: make(chan []byte),
17 if len(c.Requests) != 0 {
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")
26 // Posting this message should removing the pending Request.
27 if !c.Cancel(newRequest(1, 2, 3)) {
28 t.Fatal("request was not found")
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.
37 bb := string(<-c.writeCh)
39 t.Fatalf("received message %q is not expected: %q", bb, b)
43 // Drain the write channel until it closes.
44 for b := range c.writeCh {
46 if bs != "\x00\x00\x00\x00" {
47 t.Fatal("got unexpected non-keepalive")