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"
16 "github.com/anacrolix/torrent/storage"
19 func r(i, b, l peer_protocol.Integer) request {
20 return request{i, chunkSpec{b, l}}
23 // Check the given Request is correct for various torrent offsets.
24 func TestTorrentRequest(t *testing.T) {
25 const s = 472183431 // Length of torrent.
26 for _, _case := range []struct {
27 off int64 // An offset into the torrent.
28 req request // The expected Request. The zero value means !ok.
33 // One before the end of a piece.
34 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
35 // Offset beyond torrent length.
36 {472 * 1 << 20, request{}},
37 // One before the end of the torrent. Complicates the chunk length.
38 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
40 // One before end of chunk.
41 {16383, r(0, 0, 16384)},
43 {16384, r(0, 16384, 16384)},
45 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
46 if (_case.req == request{}) == ok {
47 t.Fatalf("expected %v, got %v", _case.req, req)
50 t.Fatalf("expected %v, got %v", _case.req, req)
55 func TestAppendToCopySlice(t *testing.T) {
56 orig := []int{1, 2, 3}
57 dupe := append([]int{}, orig...)
64 func TestTorrentString(t *testing.T) {
66 s := tor.InfoHash().HexString()
67 if s != "0000000000000000000000000000000000000000" {
72 // This benchmark is from the observation that a lot of overlapping Readers on
73 // a large torrent with small pieces had a lot of overhead in recalculating
74 // piece priorities everytime a reader (possibly in another Torrent) changed.
75 func BenchmarkUpdatePiecePriorities(b *testing.B) {
77 t := cl.newTorrent(metainfo.Hash{})
78 t.info = &metainfo.Info{
79 Pieces: make([]byte, 20*13410),
80 PieceLength: 256 << 10,
83 assert.EqualValues(b, 13410, t.numPieces())
86 r.SetReadahead(32 << 20)
89 assert.Len(b, t.readers, 7)
90 t.pendPieceRange(0, t.numPieces())
91 for i := 0; i < t.numPieces(); i += 3 {
92 t.completedPieces.Set(i, true)
94 for range iter.N(b.N) {
95 t.updatePiecePriorities()
99 // Check that a torrent containing zero-length file(s) will start, and that
100 // they're created in the filesystem. The client storage is assumed to be
101 // file-based on the native filesystem based.
102 func testEmptyFilesAndZeroPieceLength(t *testing.T, cs storage.ClientImpl) {
104 cfg.DefaultStorage = cs
105 cl, err := NewClient(&TestingConfig)
106 require.NoError(t, err)
108 ib, err := bencode.Marshal(metainfo.Info{
113 require.NoError(t, err)
114 fp := filepath.Join(TestingConfig.DataDir, "empty")
116 assert.False(t, missinggo.FilePathExists(fp))
117 tt, err := cl.AddTorrent(&metainfo.MetaInfo{
120 require.NoError(t, err)
123 require.True(t, cl.WaitAll())
124 assert.True(t, missinggo.FilePathExists(fp))
127 func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
128 testEmptyFilesAndZeroPieceLength(t, storage.NewFile(TestingConfig.DataDir))
131 func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) {
132 testEmptyFilesAndZeroPieceLength(t, storage.NewMMap(TestingConfig.DataDir))