public void StopResumeOnTheFlyDownloadTest() { var stopCount = 0; var downloadCompletedSuccessfully = false; var expectedFileSize = DownloadTestHelper.FileSize150Kb; // real bytes size var address = DownloadTestHelper.File150KbUrl; var file = new FileInfo(Path.GetTempFileName()); var config = new DownloadConfiguration() { BufferBlockSize = 1024, ChunkCount = 8, ParallelDownload = false, MaxTryAgainOnFailover = 100, OnTheFlyDownload = true }; var progressCount = config.ChunkCount * (int)Math.Ceiling((double)expectedFileSize / config.ChunkCount / config.BufferBlockSize); var downloader = new DownloadService(config); downloader.DownloadProgressChanged += delegate { Interlocked.Decrement(ref progressCount); }; downloader.DownloadFileCompleted += (s, e) => { if (e.Cancelled) { Interlocked.Increment(ref stopCount); } else if (e.Error == null) { downloadCompletedSuccessfully = true; } }; downloader.CancelAfterDownloading(10); // Stopping after start of downloading. downloader.DownloadFileAsync(address, file.FullName).Wait(); // wait to download stopped! Assert.AreEqual(1, stopCount); downloader.CancelAfterDownloading(10); // Stopping after resume of downloading. downloader.DownloadFileAsync(downloader.Package).Wait(); // resume download from stooped point. Assert.AreEqual(2, stopCount); Assert.IsFalse(downloadCompletedSuccessfully); downloader.DownloadFileAsync(downloader.Package).Wait(); // resume download from stooped point, again. Assert.IsTrue(file.Exists); Assert.AreEqual(expectedFileSize, downloader.Package.TotalFileSize); Assert.AreEqual(expectedFileSize, file.Length); Assert.IsTrue(progressCount <= 0); Assert.AreEqual(2, stopCount); Assert.IsTrue(downloadCompletedSuccessfully); file.Delete(); }