test/transfer_test.go | 37 ++++++++++++++++++++++++++++--------- torrent_test.go | 1 + diff --git a/test/transfer_test.go b/test/transfer_test.go index b70cb46c7b1fe246a00a8802278e403467739fd3..a5573d72d4d66115d2142e9ea49a815659cf1fd6 100644 --- a/test/transfer_test.go +++ b/test/transfer_test.go @@ -26,10 +26,13 @@ "github.com/stretchr/testify/require" ) type testClientTransferParams struct { - Responsive bool - Readahead int64 - SetReadahead bool - LeecherStorage func(string) storage.ClientImplCloser + Responsive bool + Readahead int64 + SetReadahead bool + LeecherStorage func(string) storage.ClientImplCloser + // TODO: Use a generic option type. This is the capacity of the leecher storage for determining + // whether it's possible for the leecher to be Complete. 0 currently means no limit. + LeecherStorageCapacity int64 SeederStorage func(string) storage.ClientImplCloser SeederUploadRateLimiter *rate.Limiter LeecherDownloadRateLimiter *rate.Limiter @@ -65,6 +68,7 @@ greetingTempDir, mi := testutil.GreetingTestTorrent() defer os.RemoveAll(greetingTempDir) // Create seeder and a Torrent. cfg := torrent.TestingConfig(t) + //cfg.Debug = true cfg.Seed = true // Some test instances don't like this being on, even when there's no cache involved. cfg.DropMutuallyCompletePeers = false @@ -93,11 +97,11 @@ // Run a Stats right after Closing the Client. This will trigger the Stats // panic in #214 caused by RemoteAddr on Closed uTP sockets. defer seederTorrent.Stats() defer seeder.Close() - seederTorrent.VerifyData() + // Adding a torrent and setting the info should trigger piece checks for everything + // automatically. Wait until the seed Torrent agrees that everything is available. + <-seederTorrent.Complete.On() // Create leecher and a Torrent. - leecherDataDir, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(leecherDataDir) + leecherDataDir := t.TempDir() cfg = torrent.TestingConfig(t) // See the seeder client config comment. cfg.DropMutuallyCompletePeers = false @@ -132,6 +136,7 @@ } return }()) require.NoError(t, err) + assert.False(t, leecherTorrent.Complete.Bool()) assert.True(t, new) //// This was used when observing coalescing of piece state changes. @@ -158,6 +163,19 @@ if ps.SetReadahead { r.SetReadahead(ps.Readahead) } assertReadAllGreeting(t, r) + info, err := mi.UnmarshalInfo() + require.NoError(t, err) + canComplete := ps.LeecherStorageCapacity == 0 || ps.LeecherStorageCapacity >= info.TotalLength() + if !canComplete { + // Reading from a cache doesn't refresh older pieces until we fail to read those, so we need + // to force a refresh since we just read the contents from start to finish. + go leecherTorrent.VerifyData() + } + if canComplete { + <-leecherTorrent.Complete.On() + } else { + <-leecherTorrent.Complete.Off() + } assert.NotEmpty(t, seederTorrent.PeerConns()) leecherPeerConns := leecherTorrent.PeerConns() if cfg.DropMutuallyCompletePeers { @@ -268,7 +286,8 @@ // Going below the piece length means it can't complete a piece so // that it can be hashed. Capacity: 5, }), - SetReadahead: setReadahead, + LeecherStorageCapacity: 5, + SetReadahead: setReadahead, // Can't readahead too far or the cache will thrash and drop data we // thought we had. Readahead: readahead, diff --git a/torrent_test.go b/torrent_test.go index bd5db3d42ef1e175515e50f5a9631c14cd14aafd..64d90b691c5d4cc53fbc0529cdbd902af4945b21 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -128,6 +128,7 @@ require.NoError(t, err) defer tt.Drop() tt.DownloadAll() require.True(t, cl.WaitAll()) + assert.True(t, tt.Complete.Bool()) assert.True(t, missinggo.FilePathExists(fp)) }