use of org.apache.hadoop.hbase.executor.ExecutorService.Executor in project hbase by apache.
the class TestExecutorService method testExecutorService.
@Test
public void testExecutorService() throws Exception {
int maxThreads = 5;
int maxTries = 10;
int sleepInterval = 10;
Server mockedServer = mock(Server.class);
when(mockedServer.getConfiguration()).thenReturn(HBaseConfiguration.create());
// Start an executor service pool with max 5 threads
ExecutorService executorService = new ExecutorService("unit_test");
executorService.startExecutorService(ExecutorType.MASTER_SERVER_OPERATIONS, maxThreads);
Executor executor = executorService.getExecutor(ExecutorType.MASTER_SERVER_OPERATIONS);
ThreadPoolExecutor pool = executor.threadPoolExecutor;
// Assert no threads yet
assertEquals(0, pool.getPoolSize());
AtomicBoolean lock = new AtomicBoolean(true);
AtomicInteger counter = new AtomicInteger(0);
// Submit maxThreads executors.
for (int i = 0; i < maxThreads; i++) {
executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter));
}
// The TestEventHandler will increment counter when it starts.
int tries = 0;
while (counter.get() < maxThreads && tries < maxTries) {
LOG.info("Waiting for all event handlers to start...");
Thread.sleep(sleepInterval);
tries++;
}
// Assert that pool is at max threads.
assertEquals(maxThreads, counter.get());
assertEquals(maxThreads, pool.getPoolSize());
ExecutorStatus status = executor.getStatus();
assertTrue(status.queuedEvents.isEmpty());
assertEquals(5, status.running.size());
checkStatusDump(status);
// Now interrupt the running Executor
synchronized (lock) {
lock.set(false);
lock.notifyAll();
}
// Executor increments counter again on way out so.... test that happened.
while (counter.get() < (maxThreads * 2) && tries < maxTries) {
System.out.println("Waiting for all event handlers to finish...");
Thread.sleep(sleepInterval);
tries++;
}
assertEquals(maxThreads * 2, counter.get());
assertEquals(maxThreads, pool.getPoolSize());
// Make sure we don't get RejectedExecutionException.
for (int i = 0; i < (2 * maxThreads); i++) {
executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter));
}
// Now interrupt the running Executor
synchronized (lock) {
lock.set(false);
lock.notifyAll();
}
// Make sure threads are still around even after their timetolive expires.
Thread.sleep(ExecutorService.Executor.keepAliveTimeInMillis * 2);
assertEquals(maxThreads, pool.getPoolSize());
executorService.shutdown();
assertEquals(0, executorService.getAllExecutorStatuses().size());
// Test that submit doesn't throw NPEs
executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter));
}
Aggregations