public async Task EnsureTasksCancelledWhenDisposed() { const int NumberOfTasks = 10; var sequencer = new SequentialTaskExecutor(); var tasks = new List <Task>(); for (int i = 0; i < NumberOfTasks; i++) { int num = i; tasks.Add(sequencer.ExecuteTask(async() => {
public async Task EnsureTasksCancelledWhenDisposed() { const int NumberOfTasks = 10; var sequencer = new SequentialTaskExecutor(); var tasks = new List <Task>(); for (int i = 0; i < NumberOfTasks; i++) { int num = i; tasks.Add(sequencer.ExecuteTask(async() => { async Task func() { await Task.Delay(100); } await func(); })); } sequencer.Dispose(); bool mustBeCancelled = false; try { await Task.WhenAll(tasks.ToArray()); } catch (OperationCanceledException) { for (int i = 0; i < NumberOfTasks; i++) { // The first task or two may already be running. So we skip completed tasks until we find // one that is is cancelled if (mustBeCancelled) { Assert.True(tasks[i].IsCanceled); } else { // All remaining tasks should be cancelled mustBeCancelled = tasks[i].IsCanceled; } } } Assert.True(mustBeCancelled); }