15 "github.com/anacrolix/missinggo/v2/filecache"
16 "github.com/anacrolix/torrent"
17 "github.com/anacrolix/torrent/internal/testutil"
18 "github.com/anacrolix/torrent/storage"
19 sqliteStorage "github.com/anacrolix/torrent/storage/sqlite"
20 "github.com/frankban/quicktest"
21 "golang.org/x/time/rate"
23 "github.com/stretchr/testify/assert"
24 "github.com/stretchr/testify/require"
27 type testClientTransferParams struct {
31 LeecherStorage func(string) storage.ClientImplCloser
32 SeederStorage func(string) storage.ClientImplCloser
33 SeederUploadRateLimiter *rate.Limiter
34 LeecherDownloadRateLimiter *rate.Limiter
35 ConfigureSeeder ConfigureClient
36 ConfigureLeecher ConfigureClient
39 LeecherStartsWithoutMetadata bool
42 func assertReadAllGreeting(t *testing.T, r io.ReadSeeker) {
43 pos, err := r.Seek(0, io.SeekStart)
44 assert.NoError(t, err)
45 assert.EqualValues(t, 0, pos)
46 quicktest.Check(t, iotest.TestReader(r, []byte(testutil.GreetingFileContents)), quicktest.IsNil)
49 // Creates a seeder and a leecher, and ensures the data transfers when a read
50 // is attempted on the leecher.
51 func testClientTransfer(t *testing.T, ps testClientTransferParams) {
53 prevGOMAXPROCS := runtime.GOMAXPROCS(ps.GOMAXPROCS)
54 newGOMAXPROCS := prevGOMAXPROCS
55 if ps.GOMAXPROCS > 0 {
56 newGOMAXPROCS = ps.GOMAXPROCS
59 quicktest.Check(t, runtime.GOMAXPROCS(prevGOMAXPROCS), quicktest.ContentEquals, newGOMAXPROCS)
62 greetingTempDir, mi := testutil.GreetingTestTorrent()
63 defer os.RemoveAll(greetingTempDir)
64 // Create seeder and a Torrent.
65 cfg := torrent.TestingConfig(t)
67 // Some test instances don't like this being on, even when there's no cache involved.
68 cfg.DropMutuallyCompletePeers = false
69 if ps.SeederUploadRateLimiter != nil {
70 cfg.UploadRateLimiter = ps.SeederUploadRateLimiter
72 // cfg.ListenAddr = "localhost:4000"
73 if ps.SeederStorage != nil {
74 storage := ps.SeederStorage(greetingTempDir)
76 cfg.DefaultStorage = storage
78 cfg.DataDir = greetingTempDir
80 if ps.ConfigureSeeder.Config != nil {
81 ps.ConfigureSeeder.Config(cfg)
83 seeder, err := torrent.NewClient(cfg)
84 require.NoError(t, err)
85 if ps.ConfigureSeeder.Client != nil {
86 ps.ConfigureSeeder.Client(seeder)
88 defer testutil.ExportStatusWriter(seeder, "s", t)()
89 seederTorrent, _, _ := seeder.AddTorrentSpec(torrent.TorrentSpecFromMetaInfo(mi))
90 // Run a Stats right after Closing the Client. This will trigger the Stats
91 // panic in #214 caused by RemoteAddr on Closed uTP sockets.
92 defer seederTorrent.Stats()
94 seederTorrent.VerifyData()
95 // Create leecher and a Torrent.
96 leecherDataDir, err := ioutil.TempDir("", "")
97 require.NoError(t, err)
98 defer os.RemoveAll(leecherDataDir)
99 cfg = torrent.TestingConfig(t)
100 // See the seeder client config comment.
101 cfg.DropMutuallyCompletePeers = false
102 if ps.LeecherStorage == nil {
103 cfg.DataDir = leecherDataDir
105 storage := ps.LeecherStorage(leecherDataDir)
106 defer storage.Close()
107 cfg.DefaultStorage = storage
109 if ps.LeecherDownloadRateLimiter != nil {
110 cfg.DownloadRateLimiter = ps.LeecherDownloadRateLimiter
114 if ps.ConfigureLeecher.Config != nil {
115 ps.ConfigureLeecher.Config(cfg)
117 leecher, err := torrent.NewClient(cfg)
118 require.NoError(t, err)
119 defer leecher.Close()
120 if ps.ConfigureLeecher.Client != nil {
121 ps.ConfigureLeecher.Client(leecher)
123 defer testutil.ExportStatusWriter(leecher, "l", t)()
124 leecherTorrent, new, err := leecher.AddTorrentSpec(func() (ret *torrent.TorrentSpec) {
125 ret = torrent.TorrentSpecFromMetaInfo(mi)
127 if ps.LeecherStartsWithoutMetadata {
132 require.NoError(t, err)
135 //// This was used when observing coalescing of piece state changes.
136 //logPieceStateChanges(leecherTorrent)
138 // Now do some things with leecher and seeder.
139 added := leecherTorrent.AddClientPeer(seeder)
140 assert.False(t, leecherTorrent.Seeding())
141 // The leecher will use peers immediately if it doesn't have the metadata. Otherwise, they
142 // should be sitting idle until we demand data.
143 if !ps.LeecherStartsWithoutMetadata {
144 assert.EqualValues(t, added, leecherTorrent.Stats().PendingPeers)
146 if ps.LeecherStartsWithoutMetadata {
147 <-leecherTorrent.GotInfo()
149 r := leecherTorrent.NewReader()
151 go leecherTorrent.SetInfoBytes(mi.InfoBytes)
156 r.SetReadahead(ps.Readahead)
158 assertReadAllGreeting(t, r)
159 assert.NotEmpty(t, seederTorrent.PeerConns())
160 leecherPeerConns := leecherTorrent.PeerConns()
161 if cfg.DropMutuallyCompletePeers {
162 // I don't think we can assume it will be empty already, due to timing.
163 //assert.Empty(t, leecherPeerConns)
165 assert.NotEmpty(t, leecherPeerConns)
168 for _, pc := range leecherPeerConns {
169 completed := pc.PeerPieces().Len()
170 t.Logf("peer conn %v has %v completed pieces", pc, completed)
171 if completed == leecherTorrent.Info().NumPieces() {
176 t.Errorf("didn't find seeder amongst leecher peer conns")
179 seederStats := seederTorrent.Stats()
180 assert.True(t, 13 <= seederStats.BytesWrittenData.Int64())
181 assert.True(t, 8 <= seederStats.ChunksWritten.Int64())
183 leecherStats := leecherTorrent.Stats()
184 assert.True(t, 13 <= leecherStats.BytesReadData.Int64())
185 assert.True(t, 8 <= leecherStats.ChunksRead.Int64())
187 // Try reading through again for the cases where the torrent data size
188 // exceeds the size of the cache.
189 assertReadAllGreeting(t, r)
192 type fileCacheClientStorageFactoryParams struct {
195 Wrapper func(*filecache.Cache) storage.ClientImplCloser
198 func newFileCacheClientStorageFactory(ps fileCacheClientStorageFactoryParams) storageFactory {
199 return func(dataDir string) storage.ClientImplCloser {
200 fc, err := filecache.NewCache(dataDir)
205 fc.SetCapacity(ps.Capacity)
207 return ps.Wrapper(fc)
211 type storageFactory func(string) storage.ClientImplCloser
213 func TestClientTransferDefault(t *testing.T) {
214 testClientTransfer(t, testClientTransferParams{
215 LeecherStorage: newFileCacheClientStorageFactory(fileCacheClientStorageFactoryParams{
216 Wrapper: fileCachePieceResourceStorage,
221 func TestClientTransferDefaultNoMetadata(t *testing.T) {
222 testClientTransfer(t, testClientTransferParams{
223 LeecherStorage: newFileCacheClientStorageFactory(fileCacheClientStorageFactoryParams{
224 Wrapper: fileCachePieceResourceStorage,
226 LeecherStartsWithoutMetadata: true,
230 func TestClientTransferRateLimitedUpload(t *testing.T) {
231 started := time.Now()
232 testClientTransfer(t, testClientTransferParams{
233 // We are uploading 13 bytes (the length of the greeting torrent). The
234 // chunks are 2 bytes in length. Then the smallest burst we can run
235 // with is 2. Time taken is (13-burst)/rate.
236 SeederUploadRateLimiter: rate.NewLimiter(11, 2),
238 require.True(t, time.Since(started) > time.Second)
241 func TestClientTransferRateLimitedDownload(t *testing.T) {
242 testClientTransfer(t, testClientTransferParams{
243 LeecherDownloadRateLimiter: rate.NewLimiter(512, 512),
247 func fileCachePieceResourceStorage(fc *filecache.Cache) storage.ClientImplCloser {
252 storage.NewResourcePieces(fc.AsResourceProvider()),
253 ioutil.NopCloser(nil),
257 func testClientTransferSmallCache(t *testing.T, setReadahead bool, readahead int64) {
258 testClientTransfer(t, testClientTransferParams{
259 LeecherStorage: newFileCacheClientStorageFactory(fileCacheClientStorageFactoryParams{
261 // Going below the piece length means it can't complete a piece so
262 // that it can be hashed.
264 Wrapper: fileCachePieceResourceStorage,
266 SetReadahead: setReadahead,
267 // Can't readahead too far or the cache will thrash and drop data we
269 Readahead: readahead,
271 // These tests don't work well with more than 1 connection to the seeder.
272 ConfigureLeecher: ConfigureClient{
273 Config: func(cfg *torrent.ClientConfig) {
274 cfg.DropDuplicatePeerIds = true
275 //cfg.DisableIPv6 = true
276 //cfg.DisableUTP = true
282 func TestClientTransferSmallCachePieceSizedReadahead(t *testing.T) {
283 testClientTransferSmallCache(t, true, 5)
286 func TestClientTransferSmallCacheLargeReadahead(t *testing.T) {
287 testClientTransferSmallCache(t, true, 15)
290 func TestClientTransferSmallCacheDefaultReadahead(t *testing.T) {
291 testClientTransferSmallCache(t, false, -1)
294 func sqliteClientStorageFactory(optsMaker func(dataDir string) sqliteStorage.NewPiecesStorageOpts) storageFactory {
295 return func(dataDir string) storage.ClientImplCloser {
296 opts := optsMaker(dataDir)
297 //log.Printf("opening sqlite db: %#v", opts)
298 ret, err := sqliteStorage.NewPiecesStorage(opts)
306 type leecherStorageTestCase struct {
312 func sqliteLeecherStorageTestCase(numConns int) leecherStorageTestCase {
313 return leecherStorageTestCase{
314 fmt.Sprintf("SqliteFile,NumConns=%v", numConns),
315 sqliteClientStorageFactory(func(dataDir string) (opts sqliteStorage.NewPiecesStorageOpts) {
316 opts.Path = filepath.Join(dataDir, "sqlite.db")
317 opts.NumConns = numConns
324 func TestClientTransferVarious(t *testing.T) {
326 for _, ls := range []leecherStorageTestCase{
327 {"Filecache", newFileCacheClientStorageFactory(fileCacheClientStorageFactoryParams{
328 Wrapper: fileCachePieceResourceStorage,
330 {"Boltdb", storage.NewBoltDB, 0},
331 {"SqliteDirect", func(s string) storage.ClientImplCloser {
332 path := filepath.Join(s, "sqlite3.db")
333 var opts sqliteStorage.NewDirectStorageOpts
335 cl, err := sqliteStorage.NewDirectStorage(opts)
341 sqliteLeecherStorageTestCase(1),
342 sqliteLeecherStorageTestCase(2),
343 // This should use a number of connections equal to the number of CPUs
344 sqliteLeecherStorageTestCase(0),
345 {"SqliteMemory", sqliteClientStorageFactory(func(dataDir string) (opts sqliteStorage.NewPiecesStorageOpts) {
350 t.Run(fmt.Sprintf("LeecherStorage=%s", ls.name), func(t *testing.T) {
352 for _, ss := range []struct {
356 {"File", storage.NewFile},
357 {"Mmap", storage.NewMMap},
359 t.Run(fmt.Sprintf("%sSeederStorage", ss.name), func(t *testing.T) {
360 for _, responsive := range []bool{false, true} {
361 t.Run(fmt.Sprintf("Responsive=%v", responsive), func(t *testing.T) {
362 t.Run("NoReadahead", func(t *testing.T) {
363 testClientTransfer(t, testClientTransferParams{
364 Responsive: responsive,
366 LeecherStorage: ls.f,
367 GOMAXPROCS: ls.gomaxprocs,
370 for _, readahead := range []int64{-1, 0, 1, 2, 9, 20} {
371 t.Run(fmt.Sprintf("readahead=%v", readahead), func(t *testing.T) {
372 testClientTransfer(t, testClientTransferParams{
374 Responsive: responsive,
376 Readahead: readahead,
377 LeecherStorage: ls.f,
378 GOMAXPROCS: ls.gomaxprocs,
390 // Check that after completing leeching, a leecher transitions to a seeding
391 // correctly. Connected in a chain like so: Seeder <-> Leecher <-> LeecherLeecher.
392 func TestSeedAfterDownloading(t *testing.T) {
393 greetingTempDir, mi := testutil.GreetingTestTorrent()
394 defer os.RemoveAll(greetingTempDir)
396 cfg := torrent.TestingConfig(t)
398 cfg.DataDir = greetingTempDir
399 seeder, err := torrent.NewClient(cfg)
400 require.NoError(t, err)
402 defer testutil.ExportStatusWriter(seeder, "s", t)()
403 seederTorrent, ok, err := seeder.AddTorrentSpec(torrent.TorrentSpecFromMetaInfo(mi))
404 require.NoError(t, err)
406 seederTorrent.VerifyData()
408 cfg = torrent.TestingConfig(t)
410 cfg.DataDir, err = ioutil.TempDir("", "")
411 require.NoError(t, err)
412 defer os.RemoveAll(cfg.DataDir)
413 leecher, err := torrent.NewClient(cfg)
414 require.NoError(t, err)
415 defer leecher.Close()
416 defer testutil.ExportStatusWriter(leecher, "l", t)()
418 cfg = torrent.TestingConfig(t)
420 cfg.DataDir, err = ioutil.TempDir("", "")
421 require.NoError(t, err)
422 defer os.RemoveAll(cfg.DataDir)
423 leecherLeecher, _ := torrent.NewClient(cfg)
424 require.NoError(t, err)
425 defer leecherLeecher.Close()
426 defer testutil.ExportStatusWriter(leecherLeecher, "ll", t)()
427 leecherGreeting, ok, err := leecher.AddTorrentSpec(func() (ret *torrent.TorrentSpec) {
428 ret = torrent.TorrentSpecFromMetaInfo(mi)
432 require.NoError(t, err)
434 llg, ok, err := leecherLeecher.AddTorrentSpec(func() (ret *torrent.TorrentSpec) {
435 ret = torrent.TorrentSpecFromMetaInfo(mi)
439 require.NoError(t, err)
441 // Simultaneously DownloadAll in Leecher, and read the contents
442 // consecutively in LeecherLeecher. This non-deterministically triggered a
443 // case where the leecher wouldn't unchoke the LeecherLeecher.
444 var wg sync.WaitGroup
450 quicktest.Check(t, iotest.TestReader(r, []byte(testutil.GreetingFileContents)), quicktest.IsNil)
452 done := make(chan struct{})
454 go leecherGreeting.AddClientPeer(seeder)
455 go leecherGreeting.AddClientPeer(leecherLeecher)
459 leecherGreeting.DownloadAll()
465 type ConfigureClient struct {
466 Config func(*torrent.ClientConfig)
467 Client func(*torrent.Client)