use of org.mycore.common.processing.MCRProcessableStatus in project mycore by MyCoRe-Org.
the class MCRProcessableFactoryTest method newPool.
@Test
public void newPool() throws Exception {
MCRProcessableRegistry registry = new MCRCentralProcessableRegistry();
MCRProcessableCollection collection = new MCRProcessableDefaultCollection("test");
registry.register(collection);
int nThreads = 3;
ExecutorService es = Executors.newFixedThreadPool(nThreads);
MCRProcessableExecutor pes = MCRProcessableFactory.newPool(es, collection);
assertEquals("No runnables should be queued right now.", 0, collection.stream().count());
assertEquals("Only the 'test' collection should be registered.", 1, registry.stream().count());
Semaphore semaphore = new Semaphore(nThreads);
// lock threads until ready
semaphore.acquire(nThreads);
MCRProcessableSupplier<?> sup1 = pes.submit(sleepyThread(semaphore));
MCRProcessableSupplier<?> sup2 = pes.submit(sleepyThread(semaphore));
MCRProcessableSupplier<?> sup3 = pes.submit(sleepyThread(semaphore));
MCRProcessableStatus s1 = sup1.getStatus();
MCRProcessableStatus s2 = sup2.getStatus();
MCRProcessableStatus s3 = sup3.getStatus();
String msgPrefix = "Job should be created or in processing: ";
assertTrue(msgPrefix + s1, MCRProcessableStatus.processing == s1 || MCRProcessableStatus.created == s1);
assertTrue(msgPrefix + s2, MCRProcessableStatus.processing == s2 || MCRProcessableStatus.created == s2);
assertTrue(msgPrefix + s3, MCRProcessableStatus.processing == s3 || MCRProcessableStatus.created == s3);
assertEquals(3, collection.stream().count());
// go
semaphore.release(nThreads);
CompletableFuture.allOf(sup1.getFuture(), sup2.getFuture(), sup3.getFuture()).get();
assertEquals(MCRProcessableStatus.successful, sup1.getStatus());
assertEquals(MCRProcessableStatus.successful, sup2.getStatus());
assertEquals(MCRProcessableStatus.successful, sup3.getStatus());
es.shutdown();
es.awaitTermination(10, TimeUnit.SECONDS);
}
Aggregations