public void TestQueueIsEmptyAfterDequeueingEnqueuedElements(IBlockingArrayQueue <int> queue) { queue.Clear(); const int n = 10; var elements = new List <int>(); var producers = new List <Task>(n); for (var i = 0; i < n; i++) { var value = i + 1; elements.Add(value); producers.Add(new Task(() => queue.Enqueue(value))); } producers.ForEach(p => p.Start()); producers.ForEach(p => p.Wait()); var consumers = new List <Task>(10); for (var i = 0; i < n; i++) { consumers.Add(new Task(() => queue.Dequeue())); } consumers.ForEach(c => c.Start()); consumers.ForEach(c => c.Wait()); Assert.True(queue.IsEmpty()); }
public void TestQueueIsNotEmptyAfterEnqueueingElements(IBlockingArrayQueue <int> queue) { queue.Clear(); const int n = 10; var producers = new List <Task>(n); for (var i = 0; i < n; i++) { var value = i + 1; producers.Add(new Task(() => queue.Enqueue(value))); } producers.ForEach(p => p.Start()); producers.ForEach(p => p.Wait()); Assert.False(queue.IsEmpty()); }
public void TestQueueRemainsEmptyAfterSameNumberOfSimultaneousEnqueuesAndDequeues(IBlockingArrayQueue <int> queue) { queue.Clear(); const int n = 10; var elements = new List <int>(); var producersAndConsumers = new List <Task>(n); for (var i = 0; i < n; i++) { var value = i + 1; elements.Add(value); producersAndConsumers.Add(new Task(() => queue.Enqueue(value))); producersAndConsumers.Add(new Task(() => queue.Dequeue())); } producersAndConsumers.ForEach(p => p.Start()); producersAndConsumers.ForEach(p => p.Wait()); Assert.True(queue.IsEmpty()); }
public void TestQueueContainsEnqueuedElements(IBlockingArrayQueue <int> queue) { queue.Clear(); const int n = 10; var elements = new List <int>(); var producers = new List <Task>(n); for (var i = 0; i < n; i++) { var value = i + 1; elements.Add(value); producers.Add(new Task(() => queue.Enqueue(value))); } producers.ForEach(p => p.Start()); producers.ForEach(p => p.Wait()); while (!queue.IsEmpty()) { Assert.True(elements.Contains(queue.Dequeue())); } }