static async Task <List <T> > ReaderTask <T>(T startValueRead, CancellationToken cancelToken, ConcurrentAsyncQueue <T> queue, Action <T, T> afterReadValue) { T lastValueRead = startValueRead; var list = new List <T>(); while (!cancelToken.IsCancellationRequested) { T curValue = lastValueRead; try { curValue = await queue.DequeueAsync(cancelToken); } catch (OperationCanceledException) { break; } list.Add(curValue); afterReadValue(curValue, lastValueRead); lastValueRead = curValue; } return(list); }
async public Task <long> ConcurrentAsyncQueueEnqueueDequeue() { using (var asyncQueue = new ConcurrentAsyncQueue <int>()) { long sum = 0; for (int i = 0; i < N; ++i) { asyncQueue.Enqueue(i); sum += await asyncQueue.DequeueAsync(); } return(sum); } }
public void ConcurrentEnqueAndDequeueHappensInOrder() { Prop.ForAll <int[]>(xs => { var queue = new ConcurrentAsyncQueue <int>(); var writerTask = WriterTask(xs, queue); bool allSuccess = true; Task readerTask = Task.Run(async() => { foreach (var expected in xs) { var actual = await queue.DequeueAsync(); Assert.AreEqual(expected, actual); allSuccess = allSuccess && (expected == actual); } }); Task.WaitAll(writerTask, readerTask); return(allSuccess); }).Check(testConfig); }