]> Sergey Matveev's repositories - btrtrc.git/blobdiff - requesting_test.go
Drop support for go 1.20
[btrtrc.git] / requesting_test.go
index 8599086e75572380ef3dcc3ea6c9ba995b1d2246..6c791a58c05994bd2375fb6fb3d2750ca5defac3 100644 (file)
@@ -3,8 +3,10 @@ package torrent
 import (
        "testing"
 
-       pp "github.com/anacrolix/torrent/peer_protocol"
+       "github.com/bradfitz/iter"
        qt "github.com/frankban/quicktest"
+
+       pp "github.com/anacrolix/torrent/peer_protocol"
 )
 
 func keysAsSlice(m map[Request]struct{}) (sl []Request) {
@@ -28,7 +30,6 @@ func TestLogExampleRequestMapOrdering(t *testing.T) {
        for k := range makeTypicalRequests() {
                t.Log(k)
        }
-
 }
 
 func TestRequestMapOrderingPersistent(t *testing.T) {
@@ -41,3 +42,37 @@ func TestRequestMapOrderAcrossInstances(t *testing.T) {
        // This shows that different map instances with the same contents can have the same range order.
        qt.Assert(t, keysAsSlice(makeTypicalRequests()), qt.ContentEquals, keysAsSlice(makeTypicalRequests()))
 }
+
+// Added for testing repeating loop iteration after shuffling in Peer.applyRequestState.
+func TestForLoopRepeatItem(t *testing.T) {
+       t.Run("ExplicitLoopVar", func(t *testing.T) {
+               once := false
+               var seen []int
+               for i := 0; i < 4; i++ {
+                       seen = append(seen, i)
+                       if !once && i == 2 {
+                               once = true
+                               i--
+                               // Will i++ still run?
+                               continue
+                       }
+               }
+               // We can mutate i and it's observed by the loop. No special treatment of the loop var.
+               qt.Assert(t, seen, qt.DeepEquals, []int{0, 1, 2, 2, 3})
+       })
+       t.Run("Range", func(t *testing.T) {
+               once := false
+               var seen []int
+               for i := range iter.N(4) {
+                       seen = append(seen, i)
+                       if !once && i == 2 {
+                               once = true
+                               // Can we actually modify the next value of i produced by the range?
+                               i--
+                               continue
+                       }
+               }
+               // Range ignores any mutation to i.
+               qt.Assert(t, seen, qt.DeepEquals, []int{0, 1, 2, 3})
+       })
+}