/** * Blocks until run has completed at least once. Will throw an exception if runnable does not * run within 10 seconds. */ public void blockTillFinished() { blockTillFinished(DEFAULT_TIMEOUT_PER_RUN, 1); }
/** * Blocks until run has completed at least once. Will throw an exception if runnable does not * run within 10 seconds. */ public void blockTillFinished() { blockTillFinished(DEFAULT_TIMEOUT_PER_RUN, 1); }
/** * Blocks until run has completed at least once. * * @param timeout time to wait for run to be called before throwing exception */ public void blockTillFinished(int timeout) { blockTillFinished(timeout, 1); }
/** * Blocks until run has completed at least once. * * @param timeout time to wait for run to be called before throwing exception */ public void blockTillFinished(int timeout) { blockTillFinished(timeout, 1); }
/** * This function blocks till the run provided, and * then gets the time between creation and a given run. * * @param runNumber the run count to get delay to * @param timeout timeout to wait for given run count to finish * @return The amount of time between construction and run being called */ public long getDelayTillRun(int runNumber, int timeout) { blockTillFinished(timeout, runNumber); Collections.sort(runTime); return runTime.get(runNumber - 1) - getCreationTime(); }
/** * This function blocks till the run provided, and * then gets the time between creation and a given run. * * @param runNumber the run count to get delay to * @param timeout timeout to wait for given run count to finish * @return The amount of time between construction and run being called */ public long getDelayTillRun(int runNumber, int timeout) { blockTillFinished(timeout, runNumber); Collections.sort(runTime); return runTime.get(runNumber - 1) - getCreationTime(); }
@Test public void signalCompleteAnotherThreadTest() { TestRunnable waitRunnable = new TestRunnable() { @Override public void handleRunFinish() { try { verifier.waitForTest(); } catch (Exception e) { throw ExceptionUtils.makeRuntime(e); } } }; new Thread(waitRunnable).start(); // should unblock thread verifier.signalComplete(); waitRunnable.blockTillFinished(); // should return quickly }
@Test (expected = ConditionTimeoutException.class) public void blockTillRunTestFail() { instance.blockTillFinished(DELAY_TIME); fail("Exception should have thrown"); }
@Test public void shutdownTest() { UnfairExecutor ue = new UnfairExecutor(1); /* adding a run time to have greater chances that runnable * will be waiting to execute after shutdown call. */ TestRunnable lastRunnable = SubmitterExecutorInterfaceTest.executeTestRunnables(ue, 5).get(TEST_QTY - 1); ue.shutdown(); // runnable should finish lastRunnable.blockTillFinished(); }
@Test public void blockTillRunTest() { TestRunnable tr = new TestRunnable() { private boolean firstRun = true; @Override public void handleRunStart() throws InterruptedException { if (firstRun) { firstRun = false; TestUtils.sleep(DELAY_TIME); run(); } } }; new Thread(tr).start(); long startTime = Clock.accurateForwardProgressingMillis(); tr.blockTillFinished(1000, 2); long endTime = Clock.accurateForwardProgressingMillis(); assertTrue(endTime - startTime >= (DELAY_TIME - ALLOWED_VARIANCE)); }
@Test public void shutdownTest() { SingleThreadScheduler sts = new SingleThreadScheduler(); /* adding a run time to have greater chances that runnable * will be waiting to execute after shutdown call. */ TestRunnable lastRunnable = // submit tasks as low priority to ensure that the shutdown lets them finish executeTestRunnables(new DefaultPriorityWrapper(sts, TaskPriority.Low), 5) .get(TEST_QTY - 1); sts.shutdown(); // runnable should finish lastRunnable.blockTillFinished(); }
@Test public void setExceptionHandlerTest() { TestExceptionHandler teh = new TestExceptionHandler(); ConfigurableThreadFactory ctf = makeThreadFactory(teh); assertEquals(teh, ctf.defaultThreadlyExceptionHandler); final AtomicReference<ExceptionHandler> ehi = new AtomicReference<>(null); TestRunnable tr = new TestRunnable() { @Override public void handleRunStart() { ehi.set(ExceptionUtils.getExceptionHandler()); } }; Thread t = ctf.newThread(tr); t.start(); tr.blockTillFinished(); assertTrue(ehi.get() == teh); } }
@Test public void isTerminatedShortTest() { final ScheduledExecutorService scheduler = makeScheduler(THREAD_COUNT); try { assertFalse(scheduler.isTerminated()); TestRunnable tr = new TestRunnable(); scheduler.execute(tr); tr.blockTillStarted(); scheduler.shutdownNow(); tr.blockTillFinished(); new TestCondition(() -> scheduler.isTerminated()).blockTillTrue(1000); } finally { scheduler.shutdownNow(); } }
@Test public void isTerminatedLongTest() { final ScheduledExecutorService scheduler = makeScheduler(THREAD_COUNT); try { final int sleepTime = 100; assertFalse(scheduler.isTerminated()); TestRunnable tr = new TestRunnable(sleepTime); scheduler.execute(tr); tr.blockTillStarted(); scheduler.shutdownNow(); tr.blockTillFinished(); new TestCondition(() -> scheduler.isTerminated()).blockTillTrue(1000); } finally { scheduler.shutdownNow(); } }
public static void getWithPriorityAverageExecutionDurationNoInputTest(StatisticPriorityScheduler scheduler) { TestRunnable tr = new TestRunnable(); scheduler.execute(tr, TaskPriority.Low); tr.blockTillFinished(); blockTillSchedulerIdle(scheduler, 1); assertEquals(-1, scheduler.getAverageExecutionDuration(TaskPriority.High), 0); }
@Test public void listenableFutureTest() { PriorityScheduler executor = new StrictPriorityScheduler(1); try { PrioritySchedulerServiceWrapper wrapper = new PrioritySchedulerServiceWrapper(executor); TestRunnable futureListener = new TestRunnable(); ListenableFuture<?> future = wrapper.submit(DoNothingRunnable.instance()); future.addListener(futureListener); futureListener.blockTillFinished(); // throws exception if never called } finally { executor.shutdownNow(); } } }
@Test public void increaseMaxConcurrencyTest() { ExecutorLimiter limiter = getLimiter(1, true); BlockingTestRunnable btr = new BlockingTestRunnable(); try { limiter.execute(btr); // block till started so that our entire limit is used up btr.blockTillStarted(); TestRunnable tr = new TestRunnable(); limiter.execute(tr); // wont be able to run limiter.setMaxConcurrency(2); tr.blockTillFinished(); // should be able to complete now that limit was increased } finally { btr.unblock(); } }
@Test public void increaseMaxConcurrencyTest() { AbstractKeyedLimiter<?> limiter = makeLimiter(1); String key = StringUtils.makeRandomString(5); BlockingTestRunnable btr = new BlockingTestRunnable(); try { limiter.execute(key, btr); // block till started so that our entire limit is used up btr.blockTillStarted(); TestRunnable tr = new TestRunnable(); limiter.execute(key, tr); // wont be able to run limiter.setMaxConcurrencyPerKey(2); tr.blockTillFinished(); // should be able to complete now that limit was increased } finally { btr.unblock(); } }
@Test public void submitScheduledRunnableNoDelayTest() throws InterruptedException, ExecutionException { SubmitterSchedulerFactory factory = getSubmitterSchedulerFactory(); try { SubmitterScheduler scheduler = factory.makeSubmitterScheduler(TEST_QTY, true); TestRunnable tr = new TestRunnable(); ListenableFuture<?> f = scheduler.submitScheduled(tr, 0); assertNotNull(f); tr.blockTillFinished(); assertEquals(1, tr.getRunCount()); assertNull(f.get()); } finally { factory.shutdown(); } }
@Test public void taskExceptionTest() { Integer key = 1; TestExceptionHandler teh = new TestExceptionHandler(); final RuntimeException testException = new SuppressedStackRuntimeException(); ExceptionUtils.setDefaultExceptionHandler(teh); TestRunnable exceptionRunnable = new TestRuntimeFailureRunnable(testException); TestRunnable followRunnable = new TestRunnable(); distributor.execute(key, exceptionRunnable); distributor.execute(key, followRunnable); exceptionRunnable.blockTillFinished(); followRunnable.blockTillStarted(); // verify that it ran despite the exception assertEquals(1, teh.getCallCount()); assertEquals(testException, teh.getLastThrowable()); }