public void OneProdOneClear() { BoundedQueue <int> queue = new BoundedQueue <int>(2); ParallelTasks ptasks = new ParallelTasks(); // PRODUCER thread inserts 4 elements ptasks.Add("Producer", () => { for (int i = 0; i < 4; i++) { queue.Enqueue(i); } }); // CLEAR thread clears queue once it reaches size 2 ptasks.Add("Clear", () => { while (queue.Size() < 2) { Thread.Sleep(0); } queue.Clear(); }); ptasks.Execute(); }
public void OneProdOneCons() { BoundedQueue <int> queue = new BoundedQueue <int>(2); ParallelTasks ptasks = new ParallelTasks(); // PRODUCER thread inserts 4 elements ptasks.Add("Producer", () => { for (int i = 0; i < 4; i++) { queue.Enqueue(i); } }); // CONSUMER thread removes 4 elements ptasks.Add("Consumer", () => { for (int i = 0; i < 4; i++) { Assert.AreEqual(i, queue.Dequeue()); } }); ptasks.Execute(); // check size: should be zero Assert.AreEqual(queue.Size(), 0); }
public void BoundedQueueTest() { BoundedQueue <int> queue = new BoundedQueue <int>(2); ParallelTasks ptasks = new ParallelTasks(); // PRODUCER thread 1 inserts 3 elements ptasks.Add("Producer1", () => { for (int i = 0; i < 3; i++) { queue.Enqueue(i); } }); // PRODUCER thread 2 inserts 3 elements ptasks.Add("Producer2", () => { for (int i = 0; i < 3; i++) { queue.Enqueue(i); } }); // CONSUMER thread removes 4 elements ptasks.Add("Consumer", () => { for (int i = 0; i < 4; i++) { System.Diagnostics.Debug.Assert(i >= queue.Dequeue()); } }); ptasks.Execute(); // check size: should be two Assert.AreEqual(2, queue.Size(), "The final size of the queue."); }