public void TestBlockAtEnqueueDueToLength() { ManualResetEvent allowJobProcessingHandlerToProceed = new ManualResetEvent(false); AutoResetEvent jobProcessed = new AutoResetEvent(false); // process handler for the jobs in queue. It blocks on a job till the queue gets full and the handler sets the // event allowHandlerToProceed. Action <string> processHandler = (job) => { allowJobProcessingHandlerToProceed.WaitOne(); if (job.Equals("job11", StringComparison.OrdinalIgnoreCase)) { jobProcessed.Set(); } }; using (JobQueueWrapper queue = new JobQueueWrapper(processHandler, 5, int.MaxValue, true, allowJobProcessingHandlerToProceed)) { // run the same thing multiple times to ensure that the queue isn't in a erroneous state after being blocked. for (int i = 0; i < 10; i++) { queue.QueueJob("job1", 0); queue.QueueJob("job2", 0); queue.QueueJob("job3", 0); queue.QueueJob("job4", 0); queue.QueueJob("job5", 0); // At this point only 5 jobs have been queued. Even if all are still in queue, still the need to block shouldn't have // risen. So queue.enteredBlockingMethod would be false. Assert.IsFalse(queue.IsEnqueueBlocked, "Entered the over-ridden blocking method at a wrong time."); queue.QueueJob("job6", 0); queue.QueueJob("job7", 0); queue.QueueJob("job8", 0); queue.QueueJob("job9", 0); queue.QueueJob("job10", 0); queue.QueueJob("job11", 0); // By this point surely the queue would have blocked atleast once, hence setting queue.enteredBlockingMethod true. Assert.IsTrue(queue.IsEnqueueBlocked, "Did not enter the over-ridden blocking method"); // We wait till all jobs are finished, so that for the next iteration the queue is in a deterministic state. jobProcessed.WaitOne(); // queue.enteredBlockingMethod is set to false to check it again in next iteration. Also // allowJobProcessingHandlerToProceed is reset to block the handler again in next iteration. queue.IsEnqueueBlocked = false; allowJobProcessingHandlerToProceed.Reset(); // if we reach here it means that the queue was successfully blocked at some point in between job6 and job11 // and subsequently unblocked. } } }
public void TestBlockingDisabled() { ManualResetEvent allowJobProcessingHandlerToProceed = new ManualResetEvent(false); AutoResetEvent jobProcessed = new AutoResetEvent(false); // process handler for the jobs in queue. It blocks on a job till the test method sets the // event allowHandlerToProceed. Action <string> processHandler = (job) => { allowJobProcessingHandlerToProceed.WaitOne(); if (job.Equals("job5", StringComparison.OrdinalIgnoreCase)) { jobProcessed.Set(); } }; using (JobQueueWrapper queue = new JobQueueWrapper(processHandler, 2, int.MaxValue, false, allowJobProcessingHandlerToProceed)) { // run the same thing multiple times to ensure that the queue isn't in a erroneous state after first run. for (int i = 0; i < 10; i++) { queue.QueueJob("job1", 0); queue.QueueJob("job2", 0); // At this point only 2 jobs have been queued. Even if all are still in queue, still the need to block shouldn't have // risen. So queue.enteredBlockingMethod would be false regardless of the blocking disabled or not. Assert.IsFalse(queue.IsEnqueueBlocked, "Entered the over-ridden blocking method at a wrong time."); queue.QueueJob("job3", 0); queue.QueueJob("job4", 0); queue.QueueJob("job5", 0); // queue.enteredBlockingMethod should still be false as the queue should not have blocked. Assert.IsFalse(queue.IsEnqueueBlocked, "Entered the over-ridden blocking method though blocking is disabled."); // allow handlers to proceed. allowJobProcessingHandlerToProceed.Set(); // We wait till all jobs are finished, so that for the next iteration the queue is in a deterministic state. jobProcessed.WaitOne(); // queue.enteredBlockingMethod is set to false to check it again in next iteration. Also // allowJobProcessingHandlerToProceed is reset to allow blocking the handler again in next iteration. queue.IsEnqueueBlocked = false; allowJobProcessingHandlerToProceed.Reset(); // if we reach here it means that the queue was never blocked. } } }
public void TestDisposeUnblocksBlockedThreads() { var allowJobProcessingHandlerToProceed = new ManualResetEvent(false); using (var gotBlocked = new ManualResetEvent(false)) { var job1Running = new ManualResetEvent(false); // process handler for the jobs in queue. It blocks on a job till the test method sets the // event allowHandlerToProceed. Action <string> processHandler = (job) => { if (job.Equals("job1", StringComparison.OrdinalIgnoreCase)) { job1Running.Set(); } allowJobProcessingHandlerToProceed.WaitOne(); }; var jobQueue = new JobQueueWrapper(processHandler, 1, int.MaxValue, true, gotBlocked); var queueThread = new Thread( source => { jobQueue.QueueJob("job1", 0); job1Running.WaitOne(); jobQueue.QueueJob("job2", 0); jobQueue.QueueJob("job3", 0); allowJobProcessingHandlerToProceed.Set(); }); queueThread.Start(); gotBlocked.WaitOne(); jobQueue.Dispose(); queueThread.Join(); } }