public void CancellationTokenWillCancel() { var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), "blah", 1000, verifyLength: false); var writeQueue = new ConcurrentQueue <ChunkedFilePart>(); byte[] sampleResponse = Encoding.UTF8.GetBytes("hello world"); var mockClient = new Mock <ISimpleHttpGetByRangeClient>(); mockClient.Setup(x => x.Get(It.IsAny <Uri>(), It.IsAny <long>(), It.IsAny <long>())) .Returns(() => { Thread.Sleep(200); return(new SimpleHttpResponse(206, sampleResponse, null)); }); int timesAskedForSlow = -1; var readStack = new ConcurrentStack <int>(); //add all of the chunks to the stack readStack.PushRange(Enumerable.Range(0, 5).Reverse().ToArray()); Func <int, bool> shouldSlw = i => { timesAskedForSlow++; return(false); }; var tokenSource = new CancellationTokenSource(); var failureToken = new FailureToken(); var bufferManager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, 1), new BufferQueueSetting((uint)parameters.MaxChunkSize) }); var task = new Downloader(bufferManager, parameters, writeQueue, readStack, shouldSlw, Downloader.ExpectedDownloadTimeInSeconds(parameters.MaxChunkSize), clientFactory: (x) => mockClient.Object, failureToken: failureToken, cancellation: tokenSource.Token); task.Start(); Thread.Sleep(500); tokenSource.Cancel(); task.Wait(TimeSpan.FromMinutes(2)); int current; readStack.TryPop(out current); Assert.True(current != 10); //we shouldn't get to 10 before the cancel works }
public void CancellationTokenWillCancel() { var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), "blah", 1000, verifyLength: false); var writeQueue = new ConcurrentQueue<ChunkedFilePart>(); byte[] sampleResponse = Encoding.UTF8.GetBytes("hello world"); var mockClient = new Mock<ISimpleHttpGetByRangeClient>(); mockClient.Setup(x => x.Get(It.IsAny<Uri>(), It.IsAny<long>(), It.IsAny<long>())) .Returns(() => { Thread.Sleep(200); return new SimpleHttpResponse(206, sampleResponse, null); }); int timesAskedForSlow = -1; var readStack = new ConcurrentStack<int>(); //add all of the chunks to the stack readStack.PushRange(Enumerable.Range(0, 5).Reverse().ToArray()); Func<int, bool> shouldSlw = i => { timesAskedForSlow++; return false; }; var tokenSource = new CancellationTokenSource(); var failureToken = new FailureToken(); var bufferManager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, 1), new BufferQueueSetting((uint)parameters.MaxChunkSize) }); var task = new Downloader(bufferManager, parameters, writeQueue, readStack, shouldSlw, Downloader.ExpectedDownloadTimeInSeconds(parameters.MaxChunkSize), clientFactory: (x) => mockClient.Object, failureToken: failureToken, cancellation: tokenSource.Token); task.Start(); Thread.Sleep(500); tokenSource.Cancel(); task.Wait(TimeSpan.FromMinutes(2)); int current; readStack.TryPop(out current); Assert.True(current != 10); //we shouldn't get to 10 before the cancel works }