8 "github.com/anacrolix/missinggo"
9 "github.com/bradfitz/iter"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
13 "github.com/anacrolix/torrent/bencode"
14 "github.com/anacrolix/torrent/metainfo"
15 "github.com/anacrolix/torrent/peer_protocol"
18 func r(i, b, l peer_protocol.Integer) request {
19 return request{i, chunkSpec{b, l}}
22 // Check the given Request is correct for various torrent offsets.
23 func TestTorrentRequest(t *testing.T) {
24 const s = 472183431 // Length of torrent.
25 for _, _case := range []struct {
26 off int64 // An offset into the torrent.
27 req request // The expected Request. The zero value means !ok.
32 // One before the end of a piece.
33 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
34 // Offset beyond torrent length.
35 {472 * 1 << 20, request{}},
36 // One before the end of the torrent. Complicates the chunk length.
37 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
39 // One before end of chunk.
40 {16383, r(0, 0, 16384)},
42 {16384, r(0, 16384, 16384)},
44 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
45 if (_case.req == request{}) == ok {
46 t.Fatalf("expected %v, got %v", _case.req, req)
49 t.Fatalf("expected %v, got %v", _case.req, req)
54 func TestAppendToCopySlice(t *testing.T) {
55 orig := []int{1, 2, 3}
56 dupe := append([]int{}, orig...)
63 func TestTorrentString(t *testing.T) {
65 s := tor.InfoHash().HexString()
66 if s != "0000000000000000000000000000000000000000" {
71 // This benchmark is from the observation that a lot of overlapping Readers on
72 // a large torrent with small pieces had a lot of overhead in recalculating
73 // piece priorities everytime a reader (possibly in another Torrent) changed.
74 func BenchmarkUpdatePiecePriorities(b *testing.B) {
76 t := cl.newTorrent(metainfo.Hash{})
77 t.info = &metainfo.Info{
78 Pieces: make([]byte, 20*13410),
79 PieceLength: 256 << 10,
82 assert.EqualValues(b, 13410, t.numPieces())
85 r.SetReadahead(32 << 20)
88 assert.Len(b, t.readers, 7)
89 t.pendPieceRange(0, t.numPieces())
90 for i := 0; i < t.numPieces(); i += 3 {
91 t.completedPieces.Set(i, true)
93 for range iter.N(b.N) {
94 t.updatePiecePriorities()
98 func TestEmptyFilesAndZeroPieceLength(t *testing.T) {
99 cl, err := NewClient(&TestingConfig)
100 require.NoError(t, err)
102 ib, err := bencode.Marshal(metainfo.Info{
106 require.NoError(t, err)
107 fp := filepath.Join(TestingConfig.DataDir, "empty")
109 assert.False(t, missinggo.FilePathExists(fp))
110 tt, err := cl.AddTorrent(&metainfo.MetaInfo{
113 require.NoError(t, err)
116 require.True(t, cl.WaitAll())
117 assert.True(t, missinggo.FilePathExists(fp))