public void DownloadFile_RemovesDownloadsFromDownloadDictionaryOnCompletion() { TestScheduler testScheduler = new TestScheduler(); ITestableObservable <double> testObservable = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnCompleted <double>())); ITestableObservable <double> testObservable2 = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnCompleted <double>())); IHttpService httpService = Substitute.For <IHttpService>(); IStorage storage = Substitute.For <IStorage>(); var downloadManager = new Managers.DownloadManager(httpService, storage); for (int i = 0; i < downloadManager.NumberOfConcurrentDownloads + 1; ++i) { string url = $"http://www.someplace.com/file_{i}.bin"; if (i < downloadManager.NumberOfConcurrentDownloads) { httpService.DownloadFileAsync(url, Arg.Any <string>(), Arg.Any <Stream>()).Returns(testObservable); } else { httpService.DownloadFileAsync(url, Arg.Any <string>(), Arg.Any <Stream>()) .Returns(testObservable2); } var download = downloadManager.DownloadFile(url); } testScheduler.AdvanceBy(1); Assert.AreEqual(1, downloadManager.CurrentDownloadDictionary.Count); }
public void DownloadFile_DownloadRemovedOnFailure() { TestScheduler testScheduler = new TestScheduler(); ITestableObservable <double> testObservable = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnError <double>(new TestException())), new Recorded <Notification <double> >(2, Notification.CreateOnError <double>(new TestException())), new Recorded <Notification <double> >(3, Notification.CreateOnError <double>(new TestException()))); IStorage storage = Substitute.For <IStorage>(); IHttpService httpService = Substitute.For <IHttpService>(); httpService.DownloadFileAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Stream>()) .Returns(testObservable); var downloadManager = new Managers.DownloadManager(httpService, storage); downloadManager.MaxRetryCount = 3; string url = "http://www.someplace.com/file.bin"; var download = downloadManager.DownloadFile(url); testScheduler.AdvanceBy(1); testScheduler.AdvanceBy(1); testScheduler.AdvanceBy(1); var queueDownload = downloadManager.DownloadQueue.FirstOrDefault(); Assert.IsNull(queueDownload); }
public void DownloadFile_ReturnsExistingDownload() { IStorage storage = Substitute.For <IStorage>(); IHttpService httpService = Substitute.For <IHttpService>(); httpService.DownloadFileAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Stream>()) .Returns(Observable.Never(0.5)); var downloadManager = new Managers.DownloadManager(httpService, storage); string url = "http://www.someplace.com/file.bin"; var download = downloadManager.DownloadFile(url); var secondDownload = downloadManager.DownloadFile(url); Assert.AreSame(download, secondDownload); }
public void DownloadFile_StartsDownload() { IHttpService httpService = Substitute.For <IHttpService>(); httpService.DownloadFileAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Stream>()) .Returns(Observable.Never(0.5)); IStorage storage = Substitute.For <IStorage>(); var downloadManager = new Managers.DownloadManager(httpService, storage); string url = "http://www.someplace.com/file.bin"; var download = downloadManager.DownloadFile(url); KeyValuePair <string, Tuple <IDisposable, IDownload> > queueDownload = downloadManager.CurrentDownloadDictionary.FirstOrDefault(); Assert.AreSame(download, queueDownload.Value.Item2); }
public void CancelDownload_WithDownloadObject_RemovesDownload() { IHttpService httpService = Substitute.For <IHttpService>(); httpService.DownloadFileAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Stream>()) .Returns(Observable.Never(0.5)); IStorage storage = Substitute.For <IStorage>(); var downloadManager = new Managers.DownloadManager(httpService, storage); string url = $"http://www.someplace.com/file.bin"; var download = downloadManager.DownloadFile(url); downloadManager.CancelDownload(download); var removedDownload = downloadManager.DownloadQueue.FirstOrDefault(x => x.Url == url); Assert.Null(removedDownload); }
public void DownloadFile_DoesNotRunMoreDownloadsThenNumberOfConcurrentDownloads() { TestScheduler testScheduler = new TestScheduler(); ITestableObservable <double> testObservable = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnError <double>(new TestException()))); IHttpService httpService = Substitute.For <IHttpService>(); httpService.DownloadFileAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Stream>()) .Returns(testObservable); IStorage storage = Substitute.For <IStorage>(); var downloadManager = new Managers.DownloadManager(httpService, storage); for (int i = 0; i < downloadManager.NumberOfConcurrentDownloads + 1; ++i) { string url = $"http://www.someplace.com/file_{i}.bin"; var download = downloadManager.DownloadFile(url); } Assert.AreEqual(1, downloadManager.DownloadQueue.Count); }
public async Task DownloadFile_Download_ReportsCorrectProgressWhenContentLengthIsKnown() { TestScheduler testScheduler = new TestScheduler(); ITestableObservable <double> testObservable = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnNext(0.1)), new Recorded <Notification <double> >(2, Notification.CreateOnNext(0.2)), new Recorded <Notification <double> >(3, Notification.CreateOnNext(0.3)), new Recorded <Notification <double> >(4, Notification.CreateOnNext(0.4)), new Recorded <Notification <double> >(5, Notification.CreateOnNext(0.5))); IHttpService httpService = Substitute.For <IHttpService>(); IStorage storage = Substitute.For <IStorage>(); httpService.DownloadFileAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Stream>()) .Returns(testObservable); var downloadManager = new Managers.DownloadManager(httpService, storage); TaskCompletionSource <bool> taskCompletionSource = new TaskCompletionSource <bool>(); string url = $"http://www.someplace.com/file.bin"; var download = downloadManager.DownloadFile(url); List <double> results = new List <double>(); download.Progress.Subscribe(d => { results.Add(d); if (results.Count == 5) { taskCompletionSource.TrySetResult(true); } }); testScheduler.AdvanceBy(5); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1)); cancellationTokenSource.Token.Register(() => { taskCompletionSource.TrySetCanceled(); }); var result = await taskCompletionSource.Task; Assert.AreEqual(6, results.Count); }