Exemplo n.º 1
0
        public void DownloadAsync_WhenRequestsFileBeingDownloadedAtTheMoment_WaitsForExistingRequestToFinishAndReturnsItsResult()
        {
            // Arrange
            int                 delay      = 2000;
            IFileDownloader     downloader = CreateFileDownloader(delay);
            FileDownloadManager manager    = new FileDownloadManager(downloader);

            string            file     = "a file";
            Task <FileResult> request1 = manager.DownloadAsync(file);
            Task <FileResult> request2 = manager.DownloadAsync(file);

            // Act
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            Task.WaitAll(Task.Run(async() => await request1),
                         Task.Run(async() => await request2));
            stopwatch.Stop();

            //Assert
            FileResult result1   = request1.Result;
            FileResult result2   = request2.Result;
            int        timeRatio = GetTimeRation(stopwatch, delay);

            A.CallTo(() => downloader.DownloadAsync(A <string> .Ignored)).MustHaveHappenedOnceExactly();
            result1.FileDownloadName.Should().Be(file);
            result2.FileDownloadName.Should().Be(file);
            result1.Should().BeSameAs(result2);
            timeRatio.Should().Be(1);
        }
Exemplo n.º 2
0
        public async void DownloadAsync_WhenRequestsFileAlreadyDownloaded_DownloadsIndependently()
        {
            // Arrange
            int                 delay      = 2000;
            IFileDownloader     downloader = CreateFileDownloader(delay);
            FileDownloadManager manager    = new FileDownloadManager(downloader);

            string            file     = "a file";
            Task <FileResult> request2 = manager.DownloadAsync(file);

            // Act
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            FileResult result1 = await manager.DownloadAsync(file);

            FileResult result2 = await manager.DownloadAsync(file);

            stopwatch.Stop();

            //Assert
            int timeRatio = GetTimeRation(stopwatch, delay);

            A.CallTo(() => downloader.DownloadAsync(A <string> .Ignored)).MustHaveHappenedTwiceExactly();
            result1.FileDownloadName.Should().Be(file);
            result2.FileDownloadName.Should().Be(file);
            result1.Should().NotBeSameAs(result2);
            timeRatio.Should().Be(2);
        }
Exemplo n.º 3
0
        public void DownloadAsync_WhenRequestsTwoDifferentFilesSimultaneously_DownloadsConcurrently()
        {
            // Arrange
            int                 delay      = 2000;
            IFileDownloader     downloader = CreateFileDownloader(delay);
            FileDownloadManager manager    = new FileDownloadManager(downloader);

            string            file1    = "a file";
            Task <FileResult> request1 = manager.DownloadAsync(file1);
            string            file2    = "another file";
            Task <FileResult> request2 = manager.DownloadAsync(file2);

            // Act
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            Task.WaitAll(Task.Run(async() => await request1),
                         Task.Run(async() => await request2));
            stopwatch.Stop();

            //Assert
            FileResult result1   = request1.Result;
            FileResult result2   = request2.Result;
            int        timeRatio = GetTimeRation(stopwatch, delay);

            A.CallTo(() => downloader.DownloadAsync(A <string> .Ignored)).MustHaveHappenedTwiceExactly();
            result1.FileDownloadName.Should().Be(file1);
            result2.FileDownloadName.Should().Be(file2);
            result1.Should().NotBeSameAs(result2);
            timeRatio.Should().Be(1);
        }