How does one unit test that a new thread was spawned for a Runnable task when using an ExecutorService?
Basically, I have a static thread pool for my application.
public static final ExecutorService executorService = Executors.newCachedThreadPool();
I'd like to use this thread pool for the my unit tests, rather than mocking one out or injecting a new thread pool, since different thread pools can dramatically alter the behavior of my application (fixed vs. cached vs. scheduled, etc); I want to ensure that I test the application's behavior with its run-time thread pool.
Cached thread pool seems to work best for me. The problem is that since it's static and the threads are cached for 60 seconds, only the first test will actually spawn a new thread in the pool, while subsequent tests reuse that thread.
Unit test code:
public void testDoCallExecutesTaskInAnotherThread() {
final Client client = this.createClient();
final ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) client.getExecutorService(); // gets the static thread pool
final int initPoolSize = threadPoolExecutor.getPoolSize();
final Response response = client.doCall();
Assert.assertEquals(initPoolSize + 1, threadPoolExecutor.getPoolSize());
}
Advice on how to get this working, or another approach altogether would be appreciated.
We use the Executors. newSingleThreadExecutor() method to create an ExecutorService that uses a single worker thread for executing tasks. If a task is submitted for execution and the thread is currently busy executing another task, then the new task will wait in a queue until the thread is free to execute it.
To test multi-thread functionality, let the multiple instances of the application or program to be tested be active at the same time. Run the multi-thread program on different hardware. Thread testing is a form of session testing for which sessions are formed of threads.
We can assign tasks to the ExecutorService using several methods including execute(), which is inherited from the Executor interface, and also submit(), invokeAny() and invokeAll().
The ExecutorService helps in maintaining a pool of threads and assigns them tasks. It also provides the facility to queue up tasks until there is a free thread available if the number of tasks is more than the threads available.
Mock the ThreadFactory
:
ThreadFactory mock = new CustomObservableThreadFactory();
ExecutorService executorService = Executors.newCachedThreadPool(mock);
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