@Override public String call() throws Exception { completionService.continueTaskInBackground(); return "bla"; } }
public void testExceptions() { ExecutorAllCompletionService service = createService(1); service.submit(new WaitRunnable(1), null); service.submit(new ExceptionRunnable("second"), null); service.submit(new WaitRunnable(1), null); service.submit(new ExceptionRunnable("third"), null); service.waitUntilAllCompleted(); assertTrue(service.isAllCompleted()); assertTrue(service.isExceptionThrown()); assertEquals("second", findCause(service.getFirstException()).getMessage()); }
public void testExecuteAsyncSupplierReturnsNull() throws Exception { eventuallyEquals(0, executor::getActiveCount); LimitedExecutor limitedExecutor = new LimitedExecutor(NAME, executor, 1); limitedExecutor.executeAsync(() -> null); CompletableFuture<String> cf1 = new CompletableFuture<>(); limitedExecutor.execute(() -> cf1.complete("a")); cf1.get(10, TimeUnit.SECONDS); }
public void testWaitForAll() { ExecutorAllCompletionService service = createService(1); long before = System.currentTimeMillis(); service.submit(new WaitRunnable(500), null); service.submit(new WaitRunnable(500), null); service.waitUntilAllCompleted(); long after = System.currentTimeMillis(); assertTrue(after - before >= 1000); assertTrue(service.isAllCompleted()); assertFalse(service.isExceptionThrown()); }
public void testBackgroundTasks() throws Exception { SemaphoreCompletionService<String> completionService = new SemaphoreCompletionService<>(executor2Threads, 1); CountDownLatch latch = new CountDownLatch(1); Future<String> backgroundInitFuture = completionService.submit(new BackgroundInitTask(completionService)); assertEquals("bla", backgroundInitFuture.get(1, SECONDS)); Future<String> dummyFuture = completionService.submit(new DummyTask()); assertSame(backgroundInitFuture, completionService.poll(1, SECONDS)); assertFalse(dummyFuture.isDone()); Future<String> backgroundEndFuture = completionService.backgroundTaskFinished(new BlockingTask(latch)); assertNull(completionService.poll(1, SECONDS)); assertFalse(dummyFuture.isDone()); latch.countDown(); assertEquals("bla", backgroundEndFuture.get(10, SECONDS)); assertEquals("bla", dummyFuture.get(10, SECONDS)); }
public void testSimpleExecution() throws Exception { BlockingTaskAwareExecutorService executorService = createExecutorService(); try { final DoSomething doSomething = new DoSomething(); executorService.execute(doSomething); Thread.sleep(100); assert !doSomething.isReady(); assert !doSomething.isExecuted(); doSomething.markReady(); executorService.checkForReadyTasks(); assert doSomething.isReady(); eventually(doSomething::isExecuted); } finally { executorService.shutdownNow(); } }
public void testConcurrencyLimit() throws Exception { SemaphoreCompletionService<String> completionService = new SemaphoreCompletionService<>(executor2Threads, 1); CountDownLatch latch = new CountDownLatch(1); Future<String> blockingFuture = completionService.submit(new BlockingTask(latch)); Future<String> dummyFuture = completionService.submit(new DummyTask()); assertNull(completionService.poll(1, SECONDS)); assertFalse(dummyFuture.isDone()); latch.countDown(); assertEquals("bla", blockingFuture.get(10, SECONDS)); assertEquals("bla", dummyFuture.get(10, SECONDS)); }
private ExecutorAllCompletionService createService(int maxThreads) { if (lastExecutorService != null) { lastExecutorService.shutdownNow(); } lastExecutorService = Executors.newFixedThreadPool(maxThreads, getTestThreadFactory("Worker")); return new ExecutorAllCompletionService(lastExecutorService); }
/** * Test that an async task ({@code executeAsync()}) will block another task from running * until its {@code CompletableFuture} is completed. */ public void testConcurrencyLimitExecuteAsync() throws Exception { eventuallyEquals(0, executor::getActiveCount); LimitedExecutor limitedExecutor = new LimitedExecutor(NAME, executor, 1); CompletableFuture<String> blocker1 = new CompletableFuture<>(); CompletableFuture<String> cf1 = new CompletableFuture<>(); limitedExecutor.executeAsync(() -> blocker1.thenAccept(cf1::complete)); verifyTaskIsBlocked(limitedExecutor, blocker1, cf1); }
/** * Test that no more than 1 task runs at a time. */ public void testConcurrencyLimit() throws Exception { eventuallyEquals(0, executor::getActiveCount); LimitedExecutor limitedExecutor = new LimitedExecutor(NAME, executor, 1); CompletableFuture<String> blocker1 = new CompletableFuture<>(); CompletableFuture<String> cf1 = new CompletableFuture<>(); limitedExecutor.execute(() -> { try { cf1.complete(blocker1.get(10, SECONDS)); } catch (Exception e) { cf1.completeExceptionally(e); } }); verifyTaskIsBlocked(limitedExecutor, blocker1, cf1); }
/** * Test that no more than 1 task runs at a time when using a {@link WithinThreadExecutor}. */ public void testConcurrencyLimitWithinThread() throws Exception { LimitedExecutor limitedExecutor = new LimitedExecutor(NAME, new WithinThreadExecutor(), 1); CompletableFuture<String> blocker1 = new CompletableFuture<>(); CompletableFuture<String> blocker2 = new CompletableFuture<>(); CompletableFuture<String> cf1 = new CompletableFuture<>(); // execute() will block Future<?> fork1 = fork(() -> { limitedExecutor.execute(() -> { blocker2.complete("blocking"); try { cf1.complete(blocker1.get(10, SECONDS)); } catch (Exception e) { cf1.completeExceptionally(e); } }); }); assertEquals("blocking", blocker2.get(10, SECONDS)); verifyTaskIsBlocked(limitedExecutor, blocker1, cf1); fork1.get(10, SECONDS); }
/** * Test that an async task ({@code executeAsync()}) will block another task from running * until its {@code CompletableFuture} is completed, when using a {@link WithinThreadExecutor}. */ public void testConcurrencyLimitExecuteAsyncWithinThread() throws Exception { LimitedExecutor limitedExecutor = new LimitedExecutor(NAME, new WithinThreadExecutor(), 1); CompletableFuture<String> blocker1 = new CompletableFuture<>(); CompletableFuture<String> cf1 = new CompletableFuture<>(); // executeAsync() will not block limitedExecutor.executeAsync(() -> blocker1.thenAccept(cf1::complete)); verifyTaskIsBlocked(limitedExecutor, blocker1, cf1); }
private void verifyTaskIsBlocked(LimitedExecutor limitedExecutor, CompletableFuture<String> blocker1, CompletableFuture<String> cf1) throws Exception { CompletableFuture<String> blocker2 = new CompletableFuture<>(); CompletableFuture<String> cf2 = new CompletableFuture<>(); // execute() may block Future<?> fork2 = fork(() -> { limitedExecutor.execute(() -> { try { cf2.complete(cf1.getNow("task 2 ran too early") + " " + blocker2.get(10, SECONDS)); } catch (Exception e) { cf2.completeExceptionally(e); } }); }); assertFalse(cf1.isDone()); assertFalse(cf2.isDone()); blocker1.complete("value1"); assertEquals("value1", cf1.get(10, SECONDS)); assertFalse(cf2.isDone()); blocker2.complete("value2"); assertEquals("value1 value2", cf2.get(10, SECONDS)); fork2.get(10, SECONDS); eventuallyEquals(0, executor::getActiveCount); } }
private void startStaleNodesLinkCleaner(long staleNodesLinksCleanerDelay) { Properties props = new Properties(); props.put("threadNamePrefix", "StaleNodesLinksCleaner"); ScheduledExecutorService executorService = new DefaultScheduledExecutorFactory().getScheduledExecutor(props); executorService.scheduleWithFixedDelay(new StaleNodesLinksCleaner(), staleNodesLinksCleanerDelay, staleNodesLinksCleanerDelay, TimeUnit.MILLISECONDS); log.info("StaleNodesCleaner started successfully with delay " + staleNodesLinksCleanerDelay); }
public void testBasicWithinThread() throws Exception { LimitedExecutor limitedExecutor = new LimitedExecutor(NAME, new WithinThreadExecutor(), 1); CompletableFuture<String> cf = new CompletableFuture<>(); limitedExecutor.execute(() -> cf.complete("value")); assertEquals("value", cf.getNow("task did not run synchronously")); }
public void testExecutorMBeans() throws Exception { LazyInitializingScheduledExecutorService timeoutExecutor = extractGlobalComponent(cacheManager, LazyInitializingScheduledExecutorService.class, TIMEOUT_SCHEDULE_EXECUTOR); timeoutExecutor.submit(() -> {}); ObjectName objectName = getCacheManagerObjectName(JMX_DOMAIN, "DefaultCacheManager", TIMEOUT_SCHEDULE_EXECUTOR); assertTrue(existsObject(objectName)); assertEquals(1, server.getAttribute(objectName, "PoolSize")); assertEquals(Integer.MAX_VALUE, server.getAttribute(objectName, "MaximumPoolSize")); }
public void testParallelException() throws InterruptedException { final ExecutorAllCompletionService service = createService(2); for (int i = 0; i < 150; ++i) { service.submit(new WaitRunnable(10), null); } service.submit(new ExceptionRunnable("foobar"), null); for (int i = 0; i < 150; ++i) { service.submit(new WaitRunnable(10), null); } List<Thread> threads = new ArrayList<>(10); for (int i = 0; i < 10; ++i) { Thread t = new Thread(() -> { service.waitUntilAllCompleted(); assertTrue(service.isAllCompleted()); assertTrue(service.isExceptionThrown()); }); threads.add(t); t.start(); } for (Thread t : threads) { t.join(); } assertTrue(service.isAllCompleted()); assertTrue(service.isExceptionThrown()); }
public void testMultipleExecutions() throws Exception { BlockingTaskAwareExecutorServiceImpl executorService = createExecutorService(); try { List<DoSomething> tasks = new LinkedList<>(); for (int i = 0; i < 30; ++i) { tasks.add(new DoSomething()); } tasks.forEach(executorService::execute); for (DoSomething doSomething : tasks) { assert !doSomething.isReady(); assert !doSomething.isExecuted(); } tasks.forEach(BlockingTaskAwareExecutorServiceTest.DoSomething::markReady); executorService.checkForReadyTasks(); for (final DoSomething doSomething : tasks) { eventually(doSomething::isExecuted); } } finally { executorService.shutdownNow(); } }
public void testConcurrency1WithinThread() throws Exception { SemaphoreCompletionService<String> completionService = new SemaphoreCompletionService<>(new WithinThreadExecutor(), 1); Future<String> future1 = completionService.submit(new DummyTask()); Future<String> future2 = completionService.poll(); assertSame(future1, future2); assertNotNull(future2); assertEquals("bla", future2.get()); }
public void testParallelWait() throws InterruptedException { final ExecutorAllCompletionService service = createService(2); for (int i = 0; i < 300; ++i) { service.submit(new WaitRunnable(10), null); } List<Thread> threads = new ArrayList<>(10); for (int i = 0; i < 10; ++i) { Thread t = new Thread(() -> { service.waitUntilAllCompleted(); assertTrue(service.isAllCompleted()); assertFalse(service.isExceptionThrown()); }); threads.add(t); t.start(); } for (Thread t : threads) { t.join(); } assertTrue(service.isAllCompleted()); assertFalse(service.isExceptionThrown()); }