public async void Support_cancelable_tasks() { const string taskName = nameof(Support_cancelable_tasks); // ReSharper disable once ObjectCreationAsStatement 0.Invoking(x => new TaskAction((Action<CancellationToken, IProgress<int>>) null, taskName)) .ShouldThrow<ArgumentNullException>(); using (var sut = new TaskAction((t,p) => {throw new NotImplementedException();}, taskName)) { await sut.Run().ShouldThrow<NotImplementedException>(); } using (var sut = new TaskAction((t, p) => { for (int i = 1; i <= 10; i++) { t.ThrowIfCancellationRequested(); Thread.Sleep(20); p?.Report(i * 10); } }, taskName)) { var task = sut.Run(new Progress<int>(p => Trace.TraceInformation($"{p}%"))); Thread.Sleep(100); sut.Cancel(); await task.ShouldThrow<OperationCanceledException>(); } }
public async void Support_blocking_threads() { // ReSharper disable once ObjectCreationAsStatement 0.Invoking(x => new TaskAction((Action) null, "test")) .ShouldThrow<ArgumentNullException>(); using (var sut = new TaskAction(() => { while (true) Thread.Sleep(500); // ReSharper disable once FunctionNeverReturns }, "test")) { var task = sut.Run(); Thread.Sleep(100); sut.Cancel(); await task.ShouldThrow<OperationCanceledException>(); } }