]> Sergey Matveev's repositories - btrtrc.git/blobdiff - requesting_test.go
Drop support for go 1.20
[btrtrc.git] / requesting_test.go
index dd54df5d61884a27eadf15d1ea79d364c272539a..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) {
@@ -40,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})
+       })
+}