Пример #1
0
 /// <summary>
 /// Checks if there is space in disk for this next download. In case there isn't, an exception will
 /// </summary>
 /// <param name="download"></param>
 internal virtual void CheckSpaceToAddDownload(Download download)
 {
     if (download.Destination.GetTotalFreeSpace() - TotalSizeBytesRemainingToDownload(download.RemoteFileInfo.SizeBytes) <= 0)
     {
         download.ChangeState(DownloadState.Error, false, Enums.GetEnumDescription(ErrorType.InsufficientDiskSpace));
         AllDownloads.Add(download);
         throw new DownloaderUCException(ErrorType.InsufficientDiskSpaceFor, download.RemoteFileInfo.FileFullName, download.RemoteFileInfo.SizeInUnit);
     }
     AllDownloads.Add(download);
 }
 protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
 {
     Contract.Assert(stream != null);
     PrepareContent();
     return(Task.Run(() =>
     {
         var buffer = new Byte[this.bufferSize];
         var size = content.Length;
         var uploaded = 0;
         downloader.ChangeState(DownloadState.PendingUpload);
         using (content) while (true)
             {
                 var length = content.Read(buffer, 0, buffer.Length);
                 if (length <= 0)
                 {
                     break;
                 }
                 downloader.Uploaded = uploaded += length;
                 stream.Write(buffer, 0, length);
                 downloader.ChangeState(DownloadState.Uploading);
             }
         downloader.ChangeState(DownloadState.PendingResponse);
     }));
 }
        public void DownloadsControllerUnitTests()
        {
            //DATA testing

            downloadController = new DownloadsController();


            //testing the remaining bytes. will add 3 downloads, and set one to started and receive 10 bytes on it.
            download1 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 50));
            download2 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 500));
            download3 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 100));

            downloadController.AddDownloadToList(download1);
            downloadController.AddDownloadToList(download2);
            downloadController.AddDownloadToList(download3);

            Assert.AreEqual(downloadController.DownloadsCollection.Count, 3);

            download2.BytesReceived = 10;
            download2.ChangeState(DownloadState.Started);

            Assert.AreEqual(downloadController.TotalSizeBytesRemainingToDownload(), 640);


            downloadController = new DownloadsController();

            //testing TotalSizeBytes
            download1 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 100));
            download2 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 2000));
            download3 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 1500));
            download4 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 5000));

            downloadController.AddDownloadToList(download1);
            downloadController.AddDownloadToList(download2);
            downloadController.AddDownloadToList(download3);
            downloadController.AddDownloadToList(download4);

            Assert.AreEqual(downloadController.TotalSizeBytes(), 8600);

            download1.ChangeState(DownloadState.Closed);
            download2.ChangeState(DownloadState.Deleted);
            download3.BytesReceived = 1500;
            download3.ChangeState(DownloadState.Completed);
            download4.ChangeState(DownloadState.Started);
            download4.BytesReceived = 300;

            Assert.AreEqual(downloadController.TotalSizeBytes(), 6500);

            //testing percent completed

            downloadController = new DownloadsController();

            download1 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 200));
            download2 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 200));
            download3 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 200));
            download4 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 200));
            download5 = new Download(destination, new RemoteFileInfo("https://www.google.com", "anyname.exe", 200));

            downloadController.AddDownloadToList(download1); //will be completed
            downloadController.AddDownloadToList(download2); //will be have progress, but then canceled
            downloadController.AddDownloadToList(download3); //will be deleted
            downloadController.AddDownloadToList(download4); //suffer an error
            downloadController.AddDownloadToList(download5); // half way to go


            download1.ChangeState(DownloadState.Completed);

            download2.ChangeState(DownloadState.Started);
            download2.BytesReceived = 100;
            download2.ChangeState(DownloadState.Canceled);

            download3.ChangeState(DownloadState.Deleted);
            download4.ChangeState(DownloadState.Error, false);

            download5.BytesReceived = 100;

            Assert.AreEqual(downloadController.PercentCompleted(), 50);

            Assert.AreNotEqual(downloadController.GetRemainingTimeString(10), "");
            Assert.AreEqual(downloadController.GetRemainingTimeString(201), "");

            Assert.Throws(typeof(ArgumentOutOfRangeException),
                          delegate
            {
                downloadController.CancelDownloads(new List <int>()
                {
                    6, 7, 8
                });
            });
            downloadController.CancelDownloads(new List <int>()
            {
                0, 1, 2, 3, 4
            });
            var deleted = downloadController.DownloadsCollection.Where(o => o.DownloadState == DownloadState.Deleted).Count();

            Assert.AreEqual(deleted, 2);

            downloadController.ClearDownloads();
            var closedCount = downloadController.DownloadsCollection.Where(o => o.DownloadState == DownloadState.Closed).Count();

            Assert.AreEqual(closedCount, 1);
        }
Пример #4
0
        public void DownloadUnitTests()
        {
            Assert.DoesNotThrow(
                delegate
            {
                Download download      = new Download(new BatchDownloaderUC.Models.Destination(""), new RemoteFileInfo("", "", 100));
                download.BytesReceived = 40;
                int percent            = download.PercentCompleted();
                Assert.AreEqual(percent, 40);
                download.BytesReceived = 0;
            });

            Assert.DoesNotThrow(
                delegate
            {
                Download download      = new Download(new BatchDownloaderUC.Models.Destination(""), new RemoteFileInfo("", "", 100));
                download.BytesReceived = -1;
                int percent            = download.PercentCompleted();
                Assert.Less(percent, 0);

                download = new Download(new BatchDownloaderUC.Models.Destination(""), new RemoteFileInfo("", "", -20));
                download.BytesReceived = 20;
                percent = download.PercentCompleted();
                Assert.Less(percent, 0);
            });


            string       downloadsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
            string       fileName        = "universaldownloader-filformodelsunnittest.txt";
            string       filePath        = Path.Combine(downloadsFolder, fileName);
            StreamWriter writer          = File.CreateText(filePath);

            writer.Write("123123");
            writer.Flush();
            writer.Close();

            Download downloadState = new Download(new BatchDownloaderUC.Models.Destination(downloadsFolder), new RemoteFileInfo("", fileName, 100));

            Assert.DoesNotThrow(
                delegate
            {
                downloadState.ChangeState(Utilities.BatchDownloaderUC.Enums.DownloadState.Started);
                Assert.AreEqual(downloadState.DownloadState, Utilities.BatchDownloaderUC.Enums.DownloadState.Started);

                downloadState.ChangeState(Utilities.BatchDownloaderUC.Enums.DownloadState.Deleted);
                Assert.AreEqual(downloadState.DownloadState, Utilities.BatchDownloaderUC.Enums.DownloadState.Deleted);

                downloadState.ChangeState(Utilities.BatchDownloaderUC.Enums.DownloadState.Error);
                Assert.AreEqual(downloadState.DownloadState, Utilities.BatchDownloaderUC.Enums.DownloadState.Error);

                downloadState.ChangeState(Utilities.BatchDownloaderUC.Enums.DownloadState.Canceled);
                Assert.AreEqual(downloadState.DownloadState, Utilities.BatchDownloaderUC.Enums.DownloadState.Error);
            });
            Assert.Throws(typeof(IOException),
                          delegate
            {
                downloadState = new Download(new Destination(downloadsFolder), new RemoteFileInfo("", fileName, 123));
                writer        = File.CreateText(filePath);
                writer.Write("123123");
                Assert.AreEqual(downloadState.DownloadState, Utilities.BatchDownloaderUC.Enums.DownloadState.Pending);
                downloadState.ChangeState(Utilities.BatchDownloaderUC.Enums.DownloadState.Started);
                downloadState.ChangeState(Utilities.BatchDownloaderUC.Enums.DownloadState.Canceled);
                Assert.AreEqual(downloadState.DownloadState, Utilities.BatchDownloaderUC.Enums.DownloadState.Canceled);
            });


            downloadState = new Download(new Destination("C:/"), new RemoteFileInfo("", "anyname.txt", 100));
            downloadState.BytesReceived = 20;

            Assert.DoesNotThrow(
                delegate
            {
                Assert.AreEqual(downloadState.GetRemainingTimeString(20).Trim(), "4 seconds");
            });
            downloadState.BytesReceived = 0;
            Assert.DoesNotThrow(
                delegate
            {
                Assert.AreEqual(downloadState.GetRemainingTimeString(0), "");
            });
        }