// Assert we can take exactly the numberOfTakes /// <exception cref="System.Exception"/> public virtual void AssertCanTake(CallQueueManager <TestCallQueueManager.FakeCall> cq, int numberOfTakes, int takeAttempts) { TestCallQueueManager.Taker taker = new TestCallQueueManager.Taker(this, cq, takeAttempts , -1); Thread t = new Thread(taker); t.Start(); t.Join(100); Assert.Equal(taker.callsTaken, numberOfTakes); t.Interrupt(); }
// Fails since it's empty /// <exception cref="System.Exception"/> public virtual void TestSwapUnderContention() { manager = new CallQueueManager <TestCallQueueManager.FakeCall>(queueClass, 5000, string.Empty , null); AList <TestCallQueueManager.Putter> producers = new AList <TestCallQueueManager.Putter >(); AList <TestCallQueueManager.Taker> consumers = new AList <TestCallQueueManager.Taker >(); Dictionary <Runnable, Thread> threads = new Dictionary <Runnable, Thread >(); // Create putters and takers for (int i = 0; i < 50; i++) { TestCallQueueManager.Putter p = new TestCallQueueManager.Putter(this, manager, -1 , -1); Thread pt = new Thread(p); producers.AddItem(p); threads[p] = pt; pt.Start(); } for (int i_1 = 0; i_1 < 20; i_1++) { TestCallQueueManager.Taker t = new TestCallQueueManager.Taker(this, manager, -1, -1); Thread tt = new Thread(t); consumers.AddItem(t); threads[t] = tt; tt.Start(); } Thread.Sleep(10); for (int i_2 = 0; i_2 < 5; i_2++) { manager.SwapQueue(queueClass, 5000, string.Empty, null); } // Stop the producers foreach (TestCallQueueManager.Putter p_1 in producers) { p_1.Stop(); } // Wait for consumers to wake up, then consume Thread.Sleep(2000); Assert.Equal(0, manager.Size()); // Ensure no calls were dropped long totalCallsCreated = 0; foreach (TestCallQueueManager.Putter p_2 in producers) { threads[p_2].Interrupt(); } foreach (TestCallQueueManager.Putter p_3 in producers) { threads[p_3].Join(); totalCallsCreated += p_3.callsAdded; } long totalCallsConsumed = 0; foreach (TestCallQueueManager.Taker t_1 in consumers) { threads[t_1].Interrupt(); } foreach (TestCallQueueManager.Taker t_2 in consumers) { threads[t_2].Join(); totalCallsConsumed += t_2.callsTaken; } Assert.Equal(totalCallsConsumed, totalCallsCreated); }