예제 #1
0
        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);
        }
예제 #2
0
파일: Program.cs 프로젝트: ywscr/Downloader
        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;
                }
            }
        }
예제 #3
0
        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);
        }
예제 #4
0
파일: Program.cs 프로젝트: aslyr/Downloader
        private static void AddEscapeHandler()
        {
            while (true)
            {
                while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
                {
                    Thread.Sleep(100);
                }

                _currentDownloadService?.CancelAsync();
            }
        }
예제 #5
0
        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);
        }
예제 #6
0
        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();
     }
 }
예제 #8
0
 private void OnStopDownload()
 {
     _downloader?.CancelAsync();
     Model.IsReady = true;
 }
예제 #9
0
 public void Cancel()
 {
     _downloader?.CancelAsync();
     _simpleDownloader?.Cancel();
 }