public async Task DisposeIsHarmless() { Func <int, Task> taskCreator = m => Task.Run(() => XAssert.Fail()); // start queue in "paused" state with 0 parallelism var q = new ConcurrentDrainingPriorityQueue <int, Task>(taskCreator, 0); q.Enqueue(0, 0); q.Dispose(); await Task.Yield(); // even after calling Dispose, all public members are still callable // set queue to "sequential" mode with 1 degree of parallelism q.MaxDegreeOfParallelism = 1; q.Enqueue(0, 0); await Task.Yield(); XAssert.AreEqual(0, q.MaxRunning); XAssert.AreEqual(1, q.MaxDegreeOfParallelism); XAssert.AreEqual(2, q.PriorityQueued); XAssert.AreEqual(0, q.SemaphoreQueued); XAssert.AreEqual(0, q.Running); // no tasks should ever have started running, and thus all running tasks have completed await q.WhenAllTasksCompleted(); // however, some When...tasks will never complete since we had a pending task in the queue that got never scheduled XAssert.IsFalse(q.WhenDone().IsCompleted); XAssert.IsFalse(q.WhenQueueDrained().IsCompleted); }
public async Task MaxRunning() { const int N = 42; var e = new TaskCompletionSource <int>(); Func <int, Task> taskCreator = _ => e.Task; using (var q = new ConcurrentDrainingPriorityQueue <int, Task>(taskCreator, N)) { for (int i = 0; i < N; i++) { q.Enqueue(i, i); } await q.WhenQueueDrained(); e.SetCanceled(); await q.WhenDone(); XAssert.AreEqual(N, q.MaxRunning); } }