Beispiel #1
0
        private async Task TestWithSize(long size)
        {
            var rd          = new Random((int)(DateTime.Now.Ticks % int.MaxValue));
            var targetBytes = new byte[size];

            rd.NextBytes(targetBytes);
            int port = 8080;

            using (var server = new FakeHttpServer(port, targetBytes))
            {
                var folderProvider = new FakeFolderProvider();
                var cacheProvider  = new FakeCacheProvider();
                var bufferProvider = new FakeBufferProvider();
                var downloader     = new HttpDownloader(
                    new DownloadTask(
                        "test-task",
                        new HttpTarget(
                            new Uri($"http://localhost:{port}/"),
                            "test-file"
                            ),
                        "test-destination-file",
                        "test-destination-folder",
                        DateTime.Now,
                        false
                        ),
                    folderProvider,
                    cacheProvider,
                    bufferProvider
                    );

                downloader.Start();

                TimeSpan cancellationStep = TimeSpan.FromSeconds(1);
                CancellationTokenSource completeTokenSource = new CancellationTokenSource();

                downloader.StatusChanged += async(sender, newStatus) =>
                {
                    switch (downloader.Status)
                    {
                    case DownloaderStatus.Running:
                        var step = cancellationStep;
                        cancellationStep *= 2;
                        await Task.Delay(step);

                        if (downloader.CanCancel)
                        {
                            downloader.Cancel();
                        }
                        break;

                    case DownloaderStatus.Ready:
                        downloader.Start();
                        break;

                    case DownloaderStatus.Completed:
                        completeTokenSource.Cancel();
                        break;

                    case DownloaderStatus.Pending:
                        break;

                    case DownloaderStatus.Disposed:
                        break;

                    case DownloaderStatus.Error:
                        throw downloader.Errors.First();

                    default:
                        throw new Exception($"unknown status {downloader.Status}");
                    }
                };

                try
                {
                    await Task.Delay(TimeSpan.FromMinutes(5), completeTokenSource.Token);
                }
                catch (TaskCanceledException) { }

                Assert.AreEqual(downloader.Status, DownloaderStatus.Completed);

                if (downloader.Result is StorageFile file)
                {
                    using (var istream = await file.OpenReadAsync())
                    {
                        using (var ostream = new MemoryStream(targetBytes))
                        {
                            Assert.IsTrue(istream.AsStream().Compare(ostream));
                        }
                    }
                }

                await downloader.Result.DeleteAsync();

                await downloader.DisposeAsync();
            }
        }