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.
This commit is contained in:
Matt Joiner 2021-12-17 19:12:10 +11:00
parent b831060d6e
commit 74c70d852a
4 changed files with 55 additions and 7 deletions

View 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},
})
}

23
test_test.go Normal file
View File

@ -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)
}

View 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)

View 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))