]> Sergey Matveev's repositories - btrtrc.git/blob - requesting_test.go
Include count of peer conns in status
[btrtrc.git] / requesting_test.go
1 package torrent
2
3 import (
4         "testing"
5
6         pp "github.com/anacrolix/torrent/peer_protocol"
7         "github.com/bradfitz/iter"
8         qt "github.com/frankban/quicktest"
9 )
10
11 func keysAsSlice(m map[Request]struct{}) (sl []Request) {
12         for k := range m {
13                 sl = append(sl, k)
14         }
15         return
16 }
17
18 func makeTypicalRequests() map[Request]struct{} {
19         m := make(map[Request]struct{})
20         for p := pp.Integer(0); p < 4; p++ {
21                 for c := pp.Integer(0); c < 16; c++ {
22                         m[Request{p, ChunkSpec{c * defaultChunkSize, defaultChunkSize}}] = struct{}{}
23                 }
24         }
25         return m
26 }
27
28 func TestLogExampleRequestMapOrdering(t *testing.T) {
29         for k := range makeTypicalRequests() {
30                 t.Log(k)
31         }
32 }
33
34 func TestRequestMapOrderingPersistent(t *testing.T) {
35         m := makeTypicalRequests()
36         // Shows that map order is persistent across separate range statements.
37         qt.Assert(t, keysAsSlice(m), qt.ContentEquals, keysAsSlice(m))
38 }
39
40 func TestRequestMapOrderAcrossInstances(t *testing.T) {
41         // This shows that different map instances with the same contents can have the same range order.
42         qt.Assert(t, keysAsSlice(makeTypicalRequests()), qt.ContentEquals, keysAsSlice(makeTypicalRequests()))
43 }
44
45 // Added for testing repeating loop iteration after shuffling in Peer.applyRequestState.
46 func TestForLoopRepeatItem(t *testing.T) {
47         t.Run("ExplicitLoopVar", func(t *testing.T) {
48                 once := false
49                 var seen []int
50                 for i := 0; i < 4; i++ {
51                         seen = append(seen, i)
52                         if !once && i == 2 {
53                                 once = true
54                                 i--
55                                 // Will i++ still run?
56                                 continue
57                         }
58                 }
59                 // We can mutate i and it's observed by the loop. No special treatment of the loop var.
60                 qt.Assert(t, seen, qt.DeepEquals, []int{0, 1, 2, 2, 3})
61         })
62         t.Run("Range", func(t *testing.T) {
63                 once := false
64                 var seen []int
65                 for i := range iter.N(4) {
66                         seen = append(seen, i)
67                         if !once && i == 2 {
68                                 once = true
69                                 // Can we actually modify the next value of i produced by the range?
70                                 i--
71                                 continue
72                         }
73                 }
74                 // Range ignores any mutation to i.
75                 qt.Assert(t, seen, qt.DeepEquals, []int{0, 1, 2, 3})
76         })
77 }