6 "github.com/bradfitz/iter"
7 "github.com/stretchr/testify/assert"
9 "github.com/anacrolix/torrent/metainfo"
10 "github.com/anacrolix/torrent/peer_protocol"
13 func r(i, b, l peer_protocol.Integer) request {
14 return request{i, chunkSpec{b, l}}
17 // Check the given Request is correct for various torrent offsets.
18 func TestTorrentRequest(t *testing.T) {
19 const s = 472183431 // Length of torrent.
20 for _, _case := range []struct {
21 off int64 // An offset into the torrent.
22 req request // The expected Request. The zero value means !ok.
27 // One before the end of a piece.
28 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
29 // Offset beyond torrent length.
30 {472 * 1 << 20, request{}},
31 // One before the end of the torrent. Complicates the chunk length.
32 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
34 // One before end of chunk.
35 {16383, r(0, 0, 16384)},
37 {16384, r(0, 16384, 16384)},
39 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
40 if (_case.req == request{}) == ok {
41 t.Fatalf("expected %v, got %v", _case.req, req)
44 t.Fatalf("expected %v, got %v", _case.req, req)
49 func TestAppendToCopySlice(t *testing.T) {
50 orig := []int{1, 2, 3}
51 dupe := append([]int{}, orig...)
58 func TestTorrentString(t *testing.T) {
60 s := tor.InfoHash().HexString()
61 if s != "0000000000000000000000000000000000000000" {
66 // This benchmark is from the observation that a lot of overlapping Readers on
67 // a large torrent with small pieces had a lot of overhead in recalculating
68 // piece priorities everytime a reader (possibly in another Torrent) changed.
69 func BenchmarkUpdatePiecePriorities(b *testing.B) {
71 t := cl.newTorrent(metainfo.Hash{})
72 t.info = &metainfo.Info{
73 Pieces: make([]byte, 20*13410),
74 PieceLength: 256 << 10,
77 assert.EqualValues(b, 13410, t.numPieces())
80 r.SetReadahead(32 << 20)
83 assert.Len(b, t.readers, 7)
84 t.pendPieceRange(0, t.numPieces())
85 for i := 0; i < t.numPieces(); i += 3 {
86 t.completedPieces.Set(i, true)
88 for range iter.N(b.N) {
89 t.updatePiecePriorities()