I have a program that operates pulls work items off a blocking queue and works on them concurrently. I realise this is a VERY loose description, but can anyone suggest a unit testing strategy/ideas on what to test?
BlockingQueue is a java Queue that support operations that wait for the queue to become non-empty when retrieving and removing an element, and wait for space to become available in the queue when adding an element.
Put() Implementation in Blocking Queue This implementation is very similar to enQueue() method. Once the capacity is reached, the thread is blocked or else it's a simple enQueue operation using LinkedList. Once the element is queued, we notify in case other waiting threads are blocked due to an empty queue.
BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control.
Each consumer will take an element from a BlockingQueue using take() method so it will block until there is an element in a queue. After taking an Integer from a queue it checks if the message is a poison pill, if yes then execution of a thread is finished.
Use the worker queue mechanisms in java.util.concurrent and rely on them to test their code. Then just test your worker to make sure it does the correct thing. If it is independent of other threads that should be enough. Otherwise you will likely need to come up with an integration testing strategy on hardware/os/jvm configurations that match your production environment.
You need to design it to be tested.
So specific suggestions:
hmm, it all stems from being able to inject mocks/test classes into the queue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With