public async Task DeleteBeforeHashing()
        {
            TestWriter testWriter = null;

            using var tmpDir = TempDir.Create();
            using var engine = new ClientEngine(new EngineSettingsBuilder(EngineSettingsBuilder.CreateForTests())
            {
                AutoSaveLoadFastResume = true,
                CacheDirectory         = tmpDir.Path,
            }.ToSettings(),
                                                Factories.Default.WithPieceWriterCreator(maxOpenFiles => (testWriter = new TestWriter()))
                                                );

            var first  = new TaskCompletionSource <object> ();
            var second = new TaskCompletionSource <object> ();

            var torrent = TestRig.CreatePrivate();
            var path    = engine.Settings.GetFastResumePath(torrent.InfoHashes);

            Directory.CreateDirectory(Path.GetDirectoryName(path));
            File.WriteAllBytes(path, new FastResume(torrent.InfoHashes, new BitField(torrent.PieceCount).SetAll(true), new BitField(torrent.PieceCount)).Encode());
            var manager = await engine.AddAsync(torrent, "savedir");

            testWriter.FilesThatExist = new System.Collections.Generic.List <ITorrentManagerFile> (manager.Files);
            Assert.IsTrue(manager.HashChecked);
            manager.Engine.DiskManager.GetHashAsyncOverride = (torrent, pieceIndex, dest) => {
                first.SetResult(null);
                second.Task.Wait();
                new byte[20].CopyTo(dest.V1Hash);
                return(Task.FromResult(true));
            };
            var hashCheckTask = manager.HashCheckAsync(false);
            await first.Task.WithTimeout();

            Assert.IsFalse(File.Exists(path));

            second.SetResult(null);
            await hashCheckTask.WithTimeout();

            Assert.IsTrue(File.Exists(path));
        }
Esempio n. 2
0
        public async Task RetainAfterSeeding()
        {
            using var tmpDir = TempDir.Create();
            using var engine = new ClientEngine(new EngineSettingsBuilder(EngineSettingsBuilder.CreateForTests())
            {
                AutoSaveLoadFastResume = true,
                CacheDirectory         = tmpDir.Path,
            }.ToSettings());

            var torrent = TestRig.CreatePrivate();
            var path    = engine.Settings.GetFastResumePath(torrent.InfoHash);

            Directory.CreateDirectory(Path.GetDirectoryName(path));
            File.WriteAllBytes(path, new FastResume(torrent.InfoHash, new BitField(torrent.Pieces.Count).SetAll(true), new BitField(torrent.Pieces.Count)).Encode().Encode());
            var manager = await engine.AddAsync(torrent, "savedir");

            Assert.IsTrue(manager.HashChecked);
            await manager.StartAsync();

            await manager.WaitForState(TorrentState.Downloading);

            Assert.IsFalse(File.Exists(path));
        }