public void StopResumeDownloadFromLastPositionTest() { // arrange var expectedStopCount = 5; var stopCount = 0; var downloadFileExecutionCounter = 0; var totalDownloadSize = 0L; var config = (DownloadConfiguration)Config.Clone(); config.BufferBlockSize = 1024; var downloader = new DownloadService(Config); downloader.DownloadProgressChanged += (s, e) => { totalDownloadSize += e.ReceivedBytes.Length; if (expectedStopCount > stopCount) { // Stopping after start of downloading downloader.CancelAsync(); stopCount++; } }; // act downloader.DownloadFileTaskAsync(DownloadTestHelper.File16KbUrl).Wait(); while (expectedStopCount > downloadFileExecutionCounter++) { downloader.DownloadFileTaskAsync(downloader.Package).Wait(); // resume download from stopped point. } // assert Assert.AreEqual(DownloadTestHelper.FileSize16Kb, downloader.Package.TotalFileSize); Assert.AreEqual(DownloadTestHelper.FileSize16Kb, totalDownloadSize); }
private static void AddKeyboardHandler() { Console.WriteLine("\nPress Esc to Stop current file download"); Console.WriteLine("\nPress Up Arrow to Increase download speed 2X"); Console.WriteLine("\nPress Down Arrow to Decrease download speed 2X"); Console.WriteLine(); while (true) // continue download other files of the list { while (!Console.KeyAvailable) { Thread.Sleep(50); } if (Console.ReadKey(true).Key == ConsoleKey.Escape) { _currentDownloadService?.CancelAsync(); } if (Console.ReadKey(true).Key == ConsoleKey.UpArrow) { _currentDownloadConfiguration.MaximumBytesPerSecond *= 2; } if (Console.ReadKey(true).Key == ConsoleKey.DownArrow) { _currentDownloadConfiguration.MaximumBytesPerSecond /= 2; } } }
public void TestTotalReceivedBytesOnResumeDownloadWhenLostDownloadedData() { // arrange var canStopDownload = true; var totalDownloadSize = 0L; var lastProgressPercentage = 0.0; var config = (DownloadConfiguration)Config.Clone(); config.BufferBlockSize = 1024; config.ChunkCount = 1; var downloader = new DownloadService(config); downloader.DownloadProgressChanged += (s, e) => { totalDownloadSize = e.ReceivedBytesSize; lastProgressPercentage = e.ProgressPercentage; if (canStopDownload && totalDownloadSize > DownloadTestHelper.FileSize16Kb / 2) { // Stopping after start of downloading downloader.CancelAsync(); canStopDownload = false; } }; // act downloader.DownloadFileTaskAsync(DownloadTestHelper.File16KbUrl).Wait(); downloader.Package.Chunks[0].Storage.Clear(); // set position to zero downloader.DownloadFileTaskAsync(downloader.Package).Wait(); // resume download from stopped point. // assert Assert.AreEqual(DownloadTestHelper.FileSize16Kb, downloader.Package.TotalFileSize); Assert.AreEqual(DownloadTestHelper.FileSize16Kb, totalDownloadSize); Assert.AreEqual(100.0, lastProgressPercentage); }
private static void AddEscapeHandler() { while (true) { while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape)) { Thread.Sleep(100); } _currentDownloadService?.CancelAsync(); } }
public void StopResumeDownloadOverFirstPackagePositionTest() { // arrange var packageCheckPoint = new DownloadPackage() { Address = DummyFileHelper.GetFileUrl(DummyFileHelper.FileSize16Kb) }; var stopThreshold = 4100; var totalReceivedBytes = 0L; var downloader = new DownloadService(Config); var isSavingStateOnCancel = false; var isSavingStateBeforCancel = false; downloader.DownloadProgressChanged += (s, e) => { totalReceivedBytes += e.ReceivedBytes.Length; isSavingStateBeforCancel |= downloader.Package.IsSaving; if (e.ReceivedBytesSize > stopThreshold) { // Stopping after start of downloading downloader.CancelAsync(); stopThreshold *= 2; // check point of package for once time packageCheckPoint.Chunks ??= downloader.Package.Chunks.Clone() as Chunk[]; } }; // act downloader.DownloadFileTaskAsync(packageCheckPoint.Address).Wait(); while (downloader.IsCancelled) { isSavingStateOnCancel |= downloader.Package.IsSaving; var firstCheckPointClone = new DownloadPackage() { Address = packageCheckPoint.Address, Chunks = packageCheckPoint.Chunks.Clone() as Chunk[] }; // resume download from first stopped point. downloader.DownloadFileTaskAsync(firstCheckPointClone).Wait(); } // assert Assert.IsTrue(downloader.Package.IsSaveComplete); Assert.IsFalse(downloader.Package.IsSaving); Assert.IsFalse(isSavingStateOnCancel); Assert.IsTrue(isSavingStateBeforCancel); Assert.AreEqual(DummyFileHelper.FileSize16Kb, downloader.Package.TotalFileSize); Assert.AreEqual(DummyFileHelper.FileSize16Kb, totalReceivedBytes); }
public void StopResumeDownloadTest() { // arrange var expectedStopCount = 5; var stopCount = 0; var cancellationsOccurrenceCount = 0; var downloadFileExecutionCounter = 0; var downloadCompletedSuccessfully = false; var downloader = new DownloadService(Config); downloader.DownloadFileCompleted += (s, e) => { if (e.Cancelled && e.Error != null) { cancellationsOccurrenceCount++; } else { downloadCompletedSuccessfully = true; } }; downloader.DownloadStarted += delegate { if (expectedStopCount > stopCount) { // Stopping after start of downloading downloader.CancelAsync(); stopCount++; } }; // act downloader.DownloadFileTaskAsync(DownloadTestHelper.File150KbUrl, Path.GetTempFileName()).Wait(); while (expectedStopCount > downloadFileExecutionCounter++) { downloader.DownloadFileTaskAsync(downloader.Package).Wait(); // resume download from stopped point. } // assert Assert.IsTrue(File.Exists(downloader.Package.FileName)); Assert.AreEqual(DownloadTestHelper.FileSize150Kb, downloader.Package.TotalFileSize); Assert.AreEqual(expectedStopCount, stopCount); Assert.AreEqual(expectedStopCount, cancellationsOccurrenceCount); Assert.IsTrue(downloadCompletedSuccessfully); File.Delete(downloader.Package.FileName); }
private void toogleDownload_Checked(object sender, RoutedEventArgs e) { SetToogleDownloadContent(); if (toogleDownload.IsChecked.Value) { if (Helper.Settings.InstallMode == InstallMode.Internal) { DownloadPackageWithInternalDownloader(); } else { DownloadPackageWithWinGet(); } } else { downloaderService?.CancelAsync(); } }
private void OnStopDownload() { _downloader?.CancelAsync(); Model.IsReady = true; }
public void Cancel() { _downloader?.CancelAsync(); _simpleDownloader?.Cancel(); }