Search in sources :

Example 1 with MCRCentralProcessableRegistry

use of org.mycore.common.processing.impl.MCRCentralProcessableRegistry 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);
}
Also used : MCRProcessableRegistry(org.mycore.common.processing.MCRProcessableRegistry) MCRProcessableStatus(org.mycore.common.processing.MCRProcessableStatus) MCRProcessableCollection(org.mycore.common.processing.MCRProcessableCollection) ExecutorService(java.util.concurrent.ExecutorService) Semaphore(java.util.concurrent.Semaphore) MCRProcessableDefaultCollection(org.mycore.common.processing.MCRProcessableDefaultCollection) MCRCentralProcessableRegistry(org.mycore.common.processing.impl.MCRCentralProcessableRegistry) Test(org.junit.Test)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)1 Semaphore (java.util.concurrent.Semaphore)1 Test (org.junit.Test)1 MCRProcessableCollection (org.mycore.common.processing.MCRProcessableCollection)1 MCRProcessableDefaultCollection (org.mycore.common.processing.MCRProcessableDefaultCollection)1 MCRProcessableRegistry (org.mycore.common.processing.MCRProcessableRegistry)1 MCRProcessableStatus (org.mycore.common.processing.MCRProcessableStatus)1 MCRCentralProcessableRegistry (org.mycore.common.processing.impl.MCRCentralProcessableRegistry)1