"testing"
"github.com/frankban/quicktest"
+ qt "github.com/frankban/quicktest"
"github.com/stretchr/testify/require"
"github.com/anacrolix/torrent/metainfo"
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},
+ })
+}
--- /dev/null
+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)
+}
}
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)
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))