]> Sergey Matveev's repositories - btrtrc.git/blob - torrent_test.go
Add test for issue #111 and #112
[btrtrc.git] / torrent_test.go
1 package torrent
2
3 import (
4         "os"
5         "path/filepath"
6         "testing"
7
8         "github.com/anacrolix/missinggo"
9         "github.com/bradfitz/iter"
10         "github.com/stretchr/testify/assert"
11         "github.com/stretchr/testify/require"
12
13         "github.com/anacrolix/torrent/bencode"
14         "github.com/anacrolix/torrent/metainfo"
15         "github.com/anacrolix/torrent/peer_protocol"
16 )
17
18 func r(i, b, l peer_protocol.Integer) request {
19         return request{i, chunkSpec{b, l}}
20 }
21
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.
28         }{
29                 // Invalid offset.
30                 {-1, request{}},
31                 {0, r(0, 0, 16384)},
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)},
38                 {1, r(0, 0, 16384)},
39                 // One before end of chunk.
40                 {16383, r(0, 0, 16384)},
41                 // Second chunk.
42                 {16384, r(0, 16384, 16384)},
43         } {
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)
47                 }
48                 if req != _case.req {
49                         t.Fatalf("expected %v, got %v", _case.req, req)
50                 }
51         }
52 }
53
54 func TestAppendToCopySlice(t *testing.T) {
55         orig := []int{1, 2, 3}
56         dupe := append([]int{}, orig...)
57         dupe[0] = 4
58         if orig[0] != 1 {
59                 t.FailNow()
60         }
61 }
62
63 func TestTorrentString(t *testing.T) {
64         tor := &Torrent{}
65         s := tor.InfoHash().HexString()
66         if s != "0000000000000000000000000000000000000000" {
67                 t.FailNow()
68         }
69 }
70
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) {
75         cl := &Client{}
76         t := cl.newTorrent(metainfo.Hash{})
77         t.info = &metainfo.Info{
78                 Pieces:      make([]byte, 20*13410),
79                 PieceLength: 256 << 10,
80         }
81         t.makePieces()
82         assert.EqualValues(b, 13410, t.numPieces())
83         for range iter.N(7) {
84                 r := t.NewReader()
85                 r.SetReadahead(32 << 20)
86                 r.Seek(3500000, 0)
87         }
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)
92         }
93         for range iter.N(b.N) {
94                 t.updatePiecePriorities()
95         }
96 }
97
98 func TestEmptyFilesAndZeroPieceLength(t *testing.T) {
99         cl, err := NewClient(&TestingConfig)
100         require.NoError(t, err)
101         defer cl.Close()
102         ib, err := bencode.Marshal(metainfo.Info{
103                 Name:   "empty",
104                 Length: 0,
105         })
106         require.NoError(t, err)
107         fp := filepath.Join(TestingConfig.DataDir, "empty")
108         os.Remove(fp)
109         assert.False(t, missinggo.FilePathExists(fp))
110         tt, err := cl.AddTorrent(&metainfo.MetaInfo{
111                 InfoBytes: ib,
112         })
113         require.NoError(t, err)
114         defer tt.Drop()
115         tt.DownloadAll()
116         require.True(t, cl.WaitAll())
117         assert.True(t, missinggo.FilePathExists(fp))
118 }