]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add TestHaveAllThenBitfield
authorMatt Joiner <anacrolix@gmail.com>
Fri, 17 Dec 2021 08:12:10 +0000 (19:12 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 23 Dec 2021 03:00:00 +0000 (14:00 +1100)
The result of a misguided attempt to reduce piece peer availability increment and decrement overhead for have all/full-bitfield and conn closes.

peerconn_test.go
test_test.go [new file with mode: 0644]
torrent.go
torrent_test.go

index 679648bbd098c9bcdc6a0e05620bac9f5ef89ec8..93e512a0ccdce66db359625ffd30d581697a3aad 100644 (file)
@@ -8,6 +8,7 @@ import (
        "testing"
 
        "github.com/frankban/quicktest"
+       qt "github.com/frankban/quicktest"
        "github.com/stretchr/testify/require"
 
        "github.com/anacrolix/torrent/metainfo"
@@ -209,3 +210,29 @@ func TestConnPexEvent(t *testing.T) {
                require.EqualValues(t, tc.e, e, i)
        }
 }
+
+func TestHaveAllThenBitfield(t *testing.T) {
+       c := qt.New(t)
+       cl := newTestingClient(t)
+       tt := cl.newTorrentForTesting()
+       // cl.newConnection()
+       pc := PeerConn{
+               Peer: Peer{t: tt},
+       }
+       pc.peerImpl = &pc
+       tt.conns[&pc] = struct{}{}
+       c.Assert(pc.onPeerSentHaveAll(), qt.IsNil)
+       pc.peerSentBitfield([]bool{false, false, true, false, true, true, false, false})
+       c.Check(pc.peerMinPieces, qt.Equals, 6)
+       c.Assert(pc.t.setInfo(&metainfo.Info{
+               PieceLength: 0,
+               Pieces:      make([]byte, pieceHash.Size()*7),
+       }), qt.IsNil)
+       pc.t.onSetInfo()
+       c.Check(tt.numPieces(), qt.Equals, 7)
+       c.Check(tt.pieceAvailabilityRuns(), qt.DeepEquals, []pieceAvailabilityRun{
+               // The last element of the bitfield is irrelevant, as the Torrent actually only has 7
+               // pieces.
+               {2, 0}, {1, 1}, {1, 0}, {2, 1}, {1, 0},
+       })
+}
diff --git a/test_test.go b/test_test.go
new file mode 100644 (file)
index 0000000..6babc91
--- /dev/null
@@ -0,0 +1,23 @@
+package torrent
+
+// Helpers for testing
+
+import (
+       "testing"
+
+       "github.com/anacrolix/torrent/metainfo"
+)
+
+func newTestingClient(t testing.TB) *Client {
+       cl := new(Client)
+       cl.init(TestingConfig(t))
+       t.Cleanup(func() {
+               cl.Close()
+       })
+       cl.initLogger()
+       return cl
+}
+
+func (cl *Client) newTorrentForTesting() *Torrent {
+       return cl.newTorrent(metainfo.Hash{}, nil)
+}
index 9db3a79434fa0d133ac804c150252b537e054476..ce4d7f598140786b886ec4b0fceac4246f02001e 100644 (file)
@@ -570,17 +570,17 @@ func (t *Torrent) newMetadataExtensionMessage(c *PeerConn, msgType pp.ExtendedMe
 }
 
 type pieceAvailabilityRun struct {
-       count        pieceIndex
-       availability int64
+       Count        pieceIndex
+       Availability int64
 }
 
 func (me pieceAvailabilityRun) String() string {
-       return fmt.Sprintf("%v(%v)", me.count, me.availability)
+       return fmt.Sprintf("%v(%v)", me.Count, me.Availability)
 }
 
 func (t *Torrent) pieceAvailabilityRuns() (ret []pieceAvailabilityRun) {
        rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) {
-               ret = append(ret, pieceAvailabilityRun{availability: el.(int64), count: int(count)})
+               ret = append(ret, pieceAvailabilityRun{Availability: el.(int64), Count: int(count)})
        })
        for i := range t.pieces {
                rle.Append(t.pieces[i].availability, 1)
index 38ea3d868499ff5541737da3a4091a4490e9fcc2..ce0af9f4e7693a47fee41b18503a6581ce1d4d09 100644 (file)
@@ -143,9 +143,7 @@ func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
 
 func TestPieceHashFailed(t *testing.T) {
        mi := testutil.GreetingMetaInfo()
-       cl := new(Client)
-       cl.config = TestingConfig(t)
-       cl.initLogger()
+       cl := newTestingClient(t)
        tt := cl.newTorrent(mi.HashInfoBytes(), badStorage{})
        tt.setChunkSize(2)
        require.NoError(t, tt.setInfoBytesLocked(mi.InfoBytes))