public void CanBeCanceled() { using (TestEnvironment testEnvironment = TestEnvironment.Create()) { TransientTestFolder folder = testEnvironment.CreateFolder(createFolder: true); DownloadFile downloadFile = new DownloadFile { BuildEngine = _mockEngine, DestinationFolder = new TaskItem(folder.Path), HttpMessageHandler = new MockHttpMessageHandler((message, token) => new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(new String('!', 10000000)), RequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://largedownload/foo.txt") }), SourceUrl = "http://largedownload/foo.txt" }; Task <bool> task = Task.Run(() => downloadFile.Execute()); downloadFile.Cancel(); task.Wait(TimeSpan.FromSeconds(1)).ShouldBeTrue(); task.Result.ShouldBeFalse(); } }
public async Task NoRunawayLoop() { DownloadFile downloadFile = null; bool failed = false; downloadFile = new DownloadFile() { BuildEngine = _mockEngine, HttpMessageHandler = new MockHttpMessageHandler((message, token) => { token.ThrowIfCancellationRequested(); downloadFile.Cancel(); if (!failed) { failed = true; return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("Success!"), RequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://success/foo.txt") }); }), Retries = 2, RetryDelayMilliseconds = 100, SourceUrl = "http://notfound/foo.txt" }; var runaway = Task.Run(() => downloadFile.Execute()); await Task.Delay(TimeSpan.FromSeconds(1)); runaway.IsCompleted.ShouldBeTrue("Task did not cancel"); var result = await runaway; result.ShouldBeFalse(() => _mockEngine.Log); }