11 "github.com/anacrolix/missinggo/v2/bitmap"
12 "github.com/frankban/quicktest"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "golang.org/x/time/rate"
17 "github.com/anacrolix/torrent"
18 "github.com/anacrolix/torrent/internal/testutil"
19 "github.com/anacrolix/torrent/storage"
22 type LeecherStorageTestCase struct {
24 Factory StorageFactory
28 type StorageFactory func(string) storage.ClientImplCloser
30 func TestLeecherStorage(t *testing.T, ls LeecherStorageTestCase) {
32 for _, ss := range []struct {
36 {"File", storage.NewFile},
37 {"Mmap", storage.NewMMap},
39 t.Run(fmt.Sprintf("%sSeederStorage", ss.name), func(t *testing.T) {
40 for _, responsive := range []bool{false, true} {
41 t.Run(fmt.Sprintf("Responsive=%v", responsive), func(t *testing.T) {
42 t.Run("NoReadahead", func(t *testing.T) {
43 testClientTransfer(t, testClientTransferParams{
44 Responsive: responsive,
46 LeecherStorage: ls.Factory,
47 GOMAXPROCS: ls.GoMaxProcs,
50 for _, readahead := range []int64{-1, 0, 1, 2, 9, 20} {
51 t.Run(fmt.Sprintf("readahead=%v", readahead), func(t *testing.T) {
52 testClientTransfer(t, testClientTransferParams{
54 Responsive: responsive,
57 LeecherStorage: ls.Factory,
58 GOMAXPROCS: ls.GoMaxProcs,
68 type ConfigureClient struct {
69 Config func(cfg *torrent.ClientConfig)
70 Client func(cl *torrent.Client)
73 type testClientTransferParams struct {
77 LeecherStorage func(string) storage.ClientImplCloser
78 // TODO: Use a generic option type. This is the capacity of the leecher storage for determining
79 // whether it's possible for the leecher to be Complete. 0 currently means no limit.
80 LeecherStorageCapacity int64
81 SeederStorage func(string) storage.ClientImplCloser
82 SeederUploadRateLimiter *rate.Limiter
83 LeecherDownloadRateLimiter *rate.Limiter
84 ConfigureSeeder ConfigureClient
85 ConfigureLeecher ConfigureClient
88 LeecherStartsWithoutMetadata bool
91 // Creates a seeder and a leecher, and ensures the data transfers when a read
92 // is attempted on the leecher.
93 func testClientTransfer(t *testing.T, ps testClientTransferParams) {
96 prevGOMAXPROCS := runtime.GOMAXPROCS(ps.GOMAXPROCS)
97 newGOMAXPROCS := prevGOMAXPROCS
98 if ps.GOMAXPROCS > 0 {
99 newGOMAXPROCS = ps.GOMAXPROCS
102 quicktest.Check(t, runtime.GOMAXPROCS(prevGOMAXPROCS), quicktest.ContentEquals, newGOMAXPROCS)
105 greetingTempDir, mi := testutil.GreetingTestTorrent()
106 defer os.RemoveAll(greetingTempDir)
107 // Create seeder and a Torrent.
108 cfg := torrent.TestingConfig(t)
111 // Less than a piece, more than a single request.
112 cfg.MaxAllocPeerRequestDataPerConn = 4
113 // Some test instances don't like this being on, even when there's no cache involved.
114 cfg.DropMutuallyCompletePeers = false
115 if ps.SeederUploadRateLimiter != nil {
116 cfg.UploadRateLimiter = ps.SeederUploadRateLimiter
118 // cfg.ListenAddr = "localhost:4000"
119 if ps.SeederStorage != nil {
120 storage := ps.SeederStorage(greetingTempDir)
121 defer storage.Close()
122 cfg.DefaultStorage = storage
124 cfg.DataDir = greetingTempDir
126 if ps.ConfigureSeeder.Config != nil {
127 ps.ConfigureSeeder.Config(cfg)
129 seeder, err := torrent.NewClient(cfg)
130 require.NoError(t, err)
131 if ps.ConfigureSeeder.Client != nil {
132 ps.ConfigureSeeder.Client(seeder)
134 defer testutil.ExportStatusWriter(seeder, "s", t)()
135 seederTorrent, _, _ := seeder.AddTorrentSpec(torrent.TorrentSpecFromMetaInfo(mi))
136 // Run a Stats right after Closing the Client. This will trigger the Stats
137 // panic in #214 caused by RemoteAddr on Closed uTP sockets.
138 defer seederTorrent.Stats()
140 // Adding a torrent and setting the info should trigger piece checks for everything
141 // automatically. Wait until the seed Torrent agrees that everything is available.
142 <-seederTorrent.Complete.On()
143 // Create leecher and a Torrent.
144 leecherDataDir := t.TempDir()
145 cfg = torrent.TestingConfig(t)
146 // See the seeder client config comment.
147 cfg.DropMutuallyCompletePeers = false
148 if ps.LeecherStorage == nil {
149 cfg.DataDir = leecherDataDir
151 storage := ps.LeecherStorage(leecherDataDir)
152 defer storage.Close()
153 cfg.DefaultStorage = storage
155 if ps.LeecherDownloadRateLimiter != nil {
156 cfg.DownloadRateLimiter = ps.LeecherDownloadRateLimiter
160 if ps.ConfigureLeecher.Config != nil {
161 ps.ConfigureLeecher.Config(cfg)
163 leecher, err := torrent.NewClient(cfg)
164 require.NoError(t, err)
165 defer leecher.Close()
166 if ps.ConfigureLeecher.Client != nil {
167 ps.ConfigureLeecher.Client(leecher)
169 defer testutil.ExportStatusWriter(leecher, "l", t)()
170 leecherTorrent, new, err := leecher.AddTorrentSpec(func() (ret *torrent.TorrentSpec) {
171 ret = torrent.TorrentSpecFromMetaInfo(mi)
173 if ps.LeecherStartsWithoutMetadata {
178 require.NoError(t, err)
179 assert.False(t, leecherTorrent.Complete.Bool())
182 //// This was used when observing coalescing of piece state changes.
183 //logPieceStateChanges(leecherTorrent)
185 // Now do some things with leecher and seeder.
186 added := leecherTorrent.AddClientPeer(seeder)
187 assert.False(t, leecherTorrent.Seeding())
188 // The leecher will use peers immediately if it doesn't have the metadata. Otherwise, they
189 // should be sitting idle until we demand data.
190 if !ps.LeecherStartsWithoutMetadata {
191 assert.EqualValues(t, added, leecherTorrent.Stats().PendingPeers)
193 if ps.LeecherStartsWithoutMetadata {
194 <-leecherTorrent.GotInfo()
196 r := leecherTorrent.NewReader()
198 go leecherTorrent.SetInfoBytes(mi.InfoBytes)
203 r.SetReadahead(ps.Readahead)
205 assertReadAllGreeting(t, r)
206 info, err := mi.UnmarshalInfo()
207 require.NoError(t, err)
208 canComplete := ps.LeecherStorageCapacity == 0 || ps.LeecherStorageCapacity >= info.TotalLength()
210 // Reading from a cache doesn't refresh older pieces until we fail to read those, so we need
211 // to force a refresh since we just read the contents from start to finish.
212 go leecherTorrent.VerifyData()
215 <-leecherTorrent.Complete.On()
217 <-leecherTorrent.Complete.Off()
219 assert.NotEmpty(t, seederTorrent.PeerConns())
220 leecherPeerConns := leecherTorrent.PeerConns()
221 if cfg.DropMutuallyCompletePeers {
222 // I don't think we can assume it will be empty already, due to timing.
223 // assert.Empty(t, leecherPeerConns)
225 assert.NotEmpty(t, leecherPeerConns)
228 for _, pc := range leecherPeerConns {
229 completed := pc.PeerPieces().GetCardinality()
230 t.Logf("peer conn %v has %v completed pieces", pc, completed)
231 if completed == bitmap.BitRange(leecherTorrent.Info().NumPieces()) {
236 t.Errorf("didn't find seeder amongst leecher peer conns")
239 seederStats := seederTorrent.Stats()
240 assert.True(t, 13 <= seederStats.BytesWrittenData.Int64())
241 assert.True(t, 8 <= seederStats.ChunksWritten.Int64())
243 leecherStats := leecherTorrent.Stats()
244 assert.True(t, 13 <= leecherStats.BytesReadData.Int64())
245 assert.True(t, 8 <= leecherStats.ChunksRead.Int64())
247 // Try reading through again for the cases where the torrent data size
248 // exceeds the size of the cache.
249 assertReadAllGreeting(t, r)
252 func assertReadAllGreeting(t *testing.T, r io.ReadSeeker) {
253 pos, err := r.Seek(0, io.SeekStart)
254 assert.NoError(t, err)
255 assert.EqualValues(t, 0, pos)
256 quicktest.Check(t, iotest.TestReader(r, []byte(testutil.GreetingFileContents)), quicktest.IsNil)