From 74c70d852a9385e0fc9ee4d16f684ef2f022795d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 17 Dec 2021 19:12:10 +1100 Subject: [PATCH] Add TestHaveAllThenBitfield 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 | 27 +++++++++++++++++++++++++++ test_test.go | 23 +++++++++++++++++++++++ torrent.go | 8 ++++---- torrent_test.go | 4 +--- 4 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 test_test.go diff --git a/peerconn_test.go b/peerconn_test.go index 679648bb..93e512a0 100644 --- a/peerconn_test.go +++ b/peerconn_test.go @@ -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 index 00000000..6babc911 --- /dev/null +++ b/test_test.go @@ -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) +} diff --git a/torrent.go b/torrent.go index 9db3a794..ce4d7f59 100644 --- a/torrent.go +++ b/torrent.go @@ -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) diff --git a/torrent_test.go b/torrent_test.go index 38ea3d86..ce0af9f4 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -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))