12 "github.com/anacrolix/missinggo/v2/filecache"
13 "github.com/anacrolix/torrent"
14 "github.com/anacrolix/torrent/internal/testutil"
15 "github.com/anacrolix/torrent/storage"
16 "golang.org/x/time/rate"
18 "github.com/stretchr/testify/assert"
19 "github.com/stretchr/testify/require"
22 type testClientTransferParams struct {
26 LeecherStorage func(string) storage.ClientImplCloser
27 SeederStorage func(string) storage.ClientImplCloser
28 SeederUploadRateLimiter *rate.Limiter
29 LeecherDownloadRateLimiter *rate.Limiter
30 ConfigureSeeder ConfigureClient
31 ConfigureLeecher ConfigureClient
33 LeecherStartsWithoutMetadata bool
36 func assertReadAllGreeting(t *testing.T, r io.ReadSeeker) {
37 pos, err := r.Seek(0, io.SeekStart)
38 assert.NoError(t, err)
39 assert.EqualValues(t, 0, pos)
40 _greeting, err := ioutil.ReadAll(r)
41 assert.NoError(t, err)
42 assert.EqualValues(t, testutil.GreetingFileContents, string(_greeting))
45 // Creates a seeder and a leecher, and ensures the data transfers when a read
46 // is attempted on the leecher.
47 func testClientTransfer(t *testing.T, ps testClientTransferParams) {
48 greetingTempDir, mi := testutil.GreetingTestTorrent()
49 defer os.RemoveAll(greetingTempDir)
50 // Create seeder and a Torrent.
51 cfg := torrent.TestingConfig()
53 if ps.SeederUploadRateLimiter != nil {
54 cfg.UploadRateLimiter = ps.SeederUploadRateLimiter
56 // cfg.ListenAddr = "localhost:4000"
57 if ps.SeederStorage != nil {
58 storage := ps.SeederStorage(greetingTempDir)
60 cfg.DefaultStorage = storage
62 cfg.DataDir = greetingTempDir
64 if ps.ConfigureSeeder.Config != nil {
65 ps.ConfigureSeeder.Config(cfg)
67 seeder, err := torrent.NewClient(cfg)
68 require.NoError(t, err)
69 if ps.ConfigureSeeder.Client != nil {
70 ps.ConfigureSeeder.Client(seeder)
72 defer testutil.ExportStatusWriter(seeder, "s", t)()
73 seederTorrent, _, _ := seeder.AddTorrentSpec(torrent.TorrentSpecFromMetaInfo(mi))
74 // Run a Stats right after Closing the Client. This will trigger the Stats
75 // panic in #214 caused by RemoteAddr on Closed uTP sockets.
76 defer seederTorrent.Stats()
78 seederTorrent.VerifyData()
79 // Create leecher and a Torrent.
80 leecherDataDir, err := ioutil.TempDir("", "")
81 require.NoError(t, err)
82 defer os.RemoveAll(leecherDataDir)
83 cfg = torrent.TestingConfig()
84 if ps.LeecherStorage == nil {
85 cfg.DataDir = leecherDataDir
87 storage := ps.LeecherStorage(leecherDataDir)
89 cfg.DefaultStorage = storage
91 if ps.LeecherDownloadRateLimiter != nil {
92 cfg.DownloadRateLimiter = ps.LeecherDownloadRateLimiter
95 if ps.ConfigureLeecher.Config != nil {
96 ps.ConfigureLeecher.Config(cfg)
98 leecher, err := torrent.NewClient(cfg)
99 require.NoError(t, err)
100 defer leecher.Close()
101 if ps.ConfigureLeecher.Client != nil {
102 ps.ConfigureLeecher.Client(leecher)
104 defer testutil.ExportStatusWriter(leecher, "l", t)()
105 leecherTorrent, new, err := leecher.AddTorrentSpec(func() (ret *torrent.TorrentSpec) {
106 ret = torrent.TorrentSpecFromMetaInfo(mi)
108 if ps.LeecherStartsWithoutMetadata {
113 require.NoError(t, err)
116 //// This was used when observing coalescing of piece state changes.
117 //logPieceStateChanges(leecherTorrent)
119 // Now do some things with leecher and seeder.
120 added := leecherTorrent.AddClientPeer(seeder)
121 assert.False(t, leecherTorrent.Seeding())
122 // The leecher will use peers immediately if it doesn't have the metadata. Otherwise, they
123 // should be sitting idle until we demand data.
124 if !ps.LeecherStartsWithoutMetadata {
125 assert.EqualValues(t, added, leecherTorrent.Stats().PendingPeers)
127 if ps.LeecherStartsWithoutMetadata {
128 <-leecherTorrent.GotInfo()
130 r := leecherTorrent.NewReader()
132 go leecherTorrent.SetInfoBytes(mi.InfoBytes)
137 r.SetReadahead(ps.Readahead)
139 assertReadAllGreeting(t, r)
140 assert.NotEmpty(t, seederTorrent.PeerConns())
141 leecherPeerConns := leecherTorrent.PeerConns()
142 assert.NotEmpty(t, leecherPeerConns)
144 for _, pc := range leecherPeerConns {
145 completed := pc.PeerPieces().Len()
146 t.Logf("peer conn %v has %v completed pieces", pc, completed)
147 if completed == leecherTorrent.Info().NumPieces() {
152 t.Errorf("didn't find seeder amongst leecher peer conns")
155 seederStats := seederTorrent.Stats()
156 assert.True(t, 13 <= seederStats.BytesWrittenData.Int64())
157 assert.True(t, 8 <= seederStats.ChunksWritten.Int64())
159 leecherStats := leecherTorrent.Stats()
160 assert.True(t, 13 <= leecherStats.BytesReadData.Int64())
161 assert.True(t, 8 <= leecherStats.ChunksRead.Int64())
163 // Try reading through again for the cases where the torrent data size
164 // exceeds the size of the cache.
165 assertReadAllGreeting(t, r)
168 type fileCacheClientStorageFactoryParams struct {
171 Wrapper func(*filecache.Cache) storage.ClientImplCloser
174 func newFileCacheClientStorageFactory(ps fileCacheClientStorageFactoryParams) storageFactory {
175 return func(dataDir string) storage.ClientImplCloser {
176 fc, err := filecache.NewCache(dataDir)
181 fc.SetCapacity(ps.Capacity)
183 return ps.Wrapper(fc)
187 type storageFactory func(string) storage.ClientImplCloser
189 func TestClientTransferDefault(t *testing.T) {
190 testClientTransfer(t, testClientTransferParams{
191 LeecherStorage: newFileCacheClientStorageFactory(fileCacheClientStorageFactoryParams{
192 Wrapper: fileCachePieceResourceStorage,
197 func TestClientTransferDefaultNoMetadata(t *testing.T) {
198 testClientTransfer(t, testClientTransferParams{
199 LeecherStorage: newFileCacheClientStorageFactory(fileCacheClientStorageFactoryParams{
200 Wrapper: fileCachePieceResourceStorage,
202 LeecherStartsWithoutMetadata: true,
206 func TestClientTransferRateLimitedUpload(t *testing.T) {
207 started := time.Now()
208 testClientTransfer(t, testClientTransferParams{
209 // We are uploading 13 bytes (the length of the greeting torrent). The
210 // chunks are 2 bytes in length. Then the smallest burst we can run
211 // with is 2. Time taken is (13-burst)/rate.
212 SeederUploadRateLimiter: rate.NewLimiter(11, 2),
214 require.True(t, time.Since(started) > time.Second)
217 func TestClientTransferRateLimitedDownload(t *testing.T) {
218 testClientTransfer(t, testClientTransferParams{
219 LeecherDownloadRateLimiter: rate.NewLimiter(512, 512),
223 func fileCachePieceResourceStorage(fc *filecache.Cache) storage.ClientImplCloser {
224 return storage.NewResourcePieces(fc.AsResourceProvider())
227 func testClientTransferSmallCache(t *testing.T, setReadahead bool, readahead int64) {
228 testClientTransfer(t, testClientTransferParams{
229 LeecherStorage: newFileCacheClientStorageFactory(fileCacheClientStorageFactoryParams{
231 // Going below the piece length means it can't complete a piece so
232 // that it can be hashed.
234 Wrapper: fileCachePieceResourceStorage,
236 SetReadahead: setReadahead,
237 // Can't readahead too far or the cache will thrash and drop data we
239 Readahead: readahead,
241 // These tests don't work well with more than 1 connection to the seeder.
242 ConfigureLeecher: ConfigureClient{
243 Config: func(cfg *torrent.ClientConfig) {
244 cfg.DropDuplicatePeerIds = true
245 //cfg.DisableIPv6 = true
246 //cfg.DisableUTP = true
252 func TestClientTransferSmallCachePieceSizedReadahead(t *testing.T) {
253 testClientTransferSmallCache(t, true, 5)
256 func TestClientTransferSmallCacheLargeReadahead(t *testing.T) {
257 testClientTransferSmallCache(t, true, 15)
260 func TestClientTransferSmallCacheDefaultReadahead(t *testing.T) {
261 testClientTransferSmallCache(t, false, -1)
264 func TestClientTransferVarious(t *testing.T) {
266 for _, ls := range []struct {
270 {"Filecache", newFileCacheClientStorageFactory(fileCacheClientStorageFactoryParams{
271 Wrapper: fileCachePieceResourceStorage,
273 {"Boltdb", storage.NewBoltDB},
275 t.Run(fmt.Sprintf("LeecherStorage=%s", ls.name), func(t *testing.T) {
277 for _, ss := range []struct {
281 {"File", storage.NewFile},
282 {"Mmap", storage.NewMMap},
284 t.Run(fmt.Sprintf("%sSeederStorage", ss.name), func(t *testing.T) {
285 for _, responsive := range []bool{false, true} {
286 t.Run(fmt.Sprintf("Responsive=%v", responsive), func(t *testing.T) {
287 t.Run("NoReadahead", func(t *testing.T) {
288 testClientTransfer(t, testClientTransferParams{
289 Responsive: responsive,
291 LeecherStorage: ls.f,
294 for _, readahead := range []int64{-1, 0, 1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14, 15, 20} {
295 t.Run(fmt.Sprintf("readahead=%v", readahead), func(t *testing.T) {
296 testClientTransfer(t, testClientTransferParams{
298 Responsive: responsive,
300 Readahead: readahead,
301 LeecherStorage: ls.f,
313 // Check that after completing leeching, a leecher transitions to a seeding
314 // correctly. Connected in a chain like so: Seeder <-> Leecher <-> LeecherLeecher.
315 func TestSeedAfterDownloading(t *testing.T) {
316 greetingTempDir, mi := testutil.GreetingTestTorrent()
317 defer os.RemoveAll(greetingTempDir)
319 cfg := torrent.TestingConfig()
321 cfg.DataDir = greetingTempDir
322 seeder, err := torrent.NewClient(cfg)
323 require.NoError(t, err)
325 defer testutil.ExportStatusWriter(seeder, "s", t)()
326 seederTorrent, ok, err := seeder.AddTorrentSpec(torrent.TorrentSpecFromMetaInfo(mi))
327 require.NoError(t, err)
329 seederTorrent.VerifyData()
331 cfg = torrent.TestingConfig()
333 cfg.DataDir, err = ioutil.TempDir("", "")
334 require.NoError(t, err)
335 defer os.RemoveAll(cfg.DataDir)
336 leecher, err := torrent.NewClient(cfg)
337 require.NoError(t, err)
338 defer leecher.Close()
339 defer testutil.ExportStatusWriter(leecher, "l", t)()
341 cfg = torrent.TestingConfig()
343 cfg.DataDir, err = ioutil.TempDir("", "")
344 require.NoError(t, err)
345 defer os.RemoveAll(cfg.DataDir)
346 leecherLeecher, _ := torrent.NewClient(cfg)
347 require.NoError(t, err)
348 defer leecherLeecher.Close()
349 defer testutil.ExportStatusWriter(leecherLeecher, "ll", t)()
350 leecherGreeting, ok, err := leecher.AddTorrentSpec(func() (ret *torrent.TorrentSpec) {
351 ret = torrent.TorrentSpecFromMetaInfo(mi)
355 require.NoError(t, err)
357 llg, ok, err := leecherLeecher.AddTorrentSpec(func() (ret *torrent.TorrentSpec) {
358 ret = torrent.TorrentSpecFromMetaInfo(mi)
362 require.NoError(t, err)
364 // Simultaneously DownloadAll in Leecher, and read the contents
365 // consecutively in LeecherLeecher. This non-deterministically triggered a
366 // case where the leecher wouldn't unchoke the LeecherLeecher.
367 var wg sync.WaitGroup
373 b, err := ioutil.ReadAll(r)
374 require.NoError(t, err)
375 assert.EqualValues(t, testutil.GreetingFileContents, b)
377 done := make(chan struct{})
379 go leecherGreeting.AddClientPeer(seeder)
380 go leecherGreeting.AddClientPeer(leecherLeecher)
384 leecherGreeting.DownloadAll()
390 type ConfigureClient struct {
391 Config func(*torrent.ClientConfig)
392 Client func(*torrent.Client)