@Override public void run() { TestUtils.sleep(DELAY_TIME); f.cancel(true); } }).start();
@Override public void run() { while (! aboutToSleep.get()) { // spin } TestUtils.sleep(DELAY_TIME); testThread.interrupt(); } });
@Override public final void run() { int startRunningCount = currentRunningCount.incrementAndGet(); runTime.addLast(Clock.accurateForwardProgressingMillis()); try { handleRunStart(); } catch (InterruptedException e) { // ignored, just reset status Thread.currentThread().interrupt(); } finally { if (runDelayInMillis > 0) { TestUtils.sleep(runDelayInMillis); } runCount.incrementAndGet(); try { handleRunFinish(); } finally { ranConcurrent = currentRunningCount.decrementAndGet() != 0 || // must be first to ensure decrement is called ranConcurrent || startRunningCount != 1; } } }
@Override public final void run() { int startRunningCount = currentRunningCount.incrementAndGet(); runTime.addLast(Clock.accurateForwardProgressingMillis()); try { handleRunStart(); } catch (InterruptedException e) { // ignored, just reset status Thread.currentThread().interrupt(); } finally { if (runDelayInMillis > 0) { TestUtils.sleep(runDelayInMillis); } runCount.incrementAndGet(); try { handleRunFinish(); } finally { ranConcurrent = currentRunningCount.decrementAndGet() != 0 || // must be first to ensure decrement is called ranConcurrent || startRunningCount != 1; } } }
@Override public void handleRunStart() throws InterruptedException { if (firstRun) { firstRun = false; TestUtils.sleep(DELAY_TIME); run(); } } };
@Override public Object call() { callTime = Clock.accurateForwardProgressingMillis(); handleCallStart(); TestUtils.sleep(runDurration); done = true; return result; }
@Test public void dumpOutputStreamEmptyTest() { profiler.start(); TestUtils.sleep(WAIT_TIME_FOR_COLLECTION); ByteArrayOutputStream out = new ByteArrayOutputStream(); profiler.dump(out); String resultStr = out.toString(); // no dump since no threads set to track assertEquals(0, resultStr.length()); }
@Test public void dumpStoppedOutputStreamEmptyTest() { profiler.start(); TestUtils.sleep(WAIT_TIME_FOR_COLLECTION); profiler.stop(); ByteArrayOutputStream out = new ByteArrayOutputStream(); profiler.dump(out); String resultStr = out.toString(); // no dump since no threads set to track assertEquals(0, resultStr.length()); }
@Test public void dumpStringEmptyTest() { profiler.start(); TestUtils.sleep(WAIT_TIME_FOR_COLLECTION); String resultStr = profiler.dump(); // no dump since no threads set to track assertEquals(0, resultStr.length()); }
@Test public void dumpStoppedStringEmptyTest() { profiler.start(); TestUtils.sleep(WAIT_TIME_FOR_COLLECTION); profiler.stop(); String resultStr = profiler.dump(); // no dump since no threads set to track assertEquals(0, resultStr.length()); }
@Test public void sleepTest() { long start = Clock.accurateForwardProgressingMillis(); TestUtils.sleep(DELAY_TIME); long end = Clock.accurateForwardProgressingMillis(); assertTrue(end - start >= (DELAY_TIME - ALLOWED_VARIANCE)); }
TestUtils.sleep(1000 * 20);
@Test public void scheduleAtFixedRateConcurrentTest() { ScheduledExecutorService scheduler = makeScheduler(2); try { final int periodInMillis = DELAY_TIME; final int runnableSleepTime = DELAY_TIME * 2; TestRunnable tr = new TestRunnable(runnableSleepTime); scheduler.scheduleAtFixedRate(tr, 0, periodInMillis, TimeUnit.MILLISECONDS); // block till we have run enough times to throw exception tr.blockTillFinished(1000 * 10, CYCLE_COUNT); // let all runnables finish readily tr.setRunDelayInMillis(0); // wait a little extra to give time for additional runs TestUtils.sleep(periodInMillis * 2); assertFalse(tr.ranConcurrently()); } finally { scheduler.shutdownNow(); } }
@Test public void verifyCleanupTaskTest() { double permits = .1; Object key = new Object(); limiter.execute(permits, key, new TestRunnable()); assertEquals(2, scheduler.advance(KeyedRateLimiterExecutor.LIMITER_IDLE_TIMEOUT)); assertEquals(1, limiter.getTrackedKeyCount()); assertFalse(limiter.currentLimiters.isEmpty()); if (TEST_PROFILE == TestLoad.Stress) { // too slow for normal tests right now TestUtils.sleep((long)(KeyedRateLimiterExecutor.LIMITER_IDLE_TIMEOUT + (1000 * permits))); TestUtils.blockTillClockAdvances(); assertEquals(1, scheduler.advance(KeyedRateLimiterExecutor.LIMITER_IDLE_TIMEOUT)); assertTrue(limiter.currentLimiters.isEmpty()); assertEquals(0, limiter.getTrackedKeyCount()); } }
public static void getLongRunningTasksTest(StatisticPriorityScheduler scheduler) { final int checkTime = DELAY_TIME; BlockingTestRunnable br = new BlockingTestRunnable(); scheduler.execute(br); br.blockTillStarted(); TestUtils.sleep(checkTime + 1); assertEquals(1, scheduler.getLongRunningTasksQty(checkTime)); List<Pair<Runnable, StackTraceElement[]>> longRunning = scheduler.getLongRunningTasks(checkTime); br.unblock(); assertEquals(1, longRunning.size()); assertTrue(longRunning.get(0).getLeft() == br); // wait for task to finish now blockTillSchedulerIdle(scheduler, 1); assertEquals(0, scheduler.getLongRunningTasksQty(0)); longRunning = scheduler.getLongRunningTasks(0); assertTrue(longRunning.isEmpty()); }
@Test public void timeoutTest() { poller = new Poller(new SingleThreadScheduler(), POLL_INTERVAL, 10); ListenableFuture<?> f = poller.watch(() -> false); TestUtils.sleep(10); new TestCondition(() -> f.isDone() && f.isCancelled()) .blockTillTrue();// will throw if does not become true }
@Test public void executeLimitQueueTest() { SchedulerServiceLimiter limitedExecutor = getLimiter(PARALLEL_COUNT, true); List<BlockingTestRunnable> blockingRunnables = new ArrayList<>(PARALLEL_COUNT); try { for (int i = 0; i < PARALLEL_COUNT; i++) { BlockingTestRunnable btr = new BlockingTestRunnable(); limitedExecutor.execute(btr); blockingRunnables.add(btr); } for (BlockingTestRunnable btr : blockingRunnables) { btr.blockTillStarted(); } assertEquals(0, limitedExecutor.getQueuedTaskCount()); limitedExecutor.execute(DoNothingRunnable.instance()); TestUtils.sleep(DELAY_TIME); assertEquals(1, limitedExecutor.getQueuedTaskCount()); } finally { for (BlockingTestRunnable btr : blockingRunnables) { btr.unblock(); } } }
private static void verifyGuaranteedThreadProtection(List<SchedulerService> executors, int expectedLimit) { List<BlockingTestRunnable> blockingRunnables = new ArrayList<>(expectedLimit); try { for (SchedulerService executor : executors) { for (int i = 0; i < expectedLimit; i++) { BlockingTestRunnable btr = new BlockingTestRunnable(); blockingRunnables.add(btr); executor.execute(btr); } for (BlockingTestRunnable btr : blockingRunnables) { btr.blockTillStarted(); } // verify additional tasks would queue int startingQueueCount = executor.getQueuedTaskCount(); executor.execute(DoNothingRunnable.instance()); TestUtils.sleep(DELAY_TIME); assertEquals(startingQueueCount + 1, executor.getQueuedTaskCount()); // verify we can still execute on pool with existing threads TestRunnable tr = new TestRunnable(); CentralThreadlyPool.lowPriorityPool().execute(tr); tr.blockTillStarted(); } } finally { for (BlockingTestRunnable btr : blockingRunnables) { btr.unblock(); } } }
@Test public void rescheduledFutureCheckTest() throws InterruptedException { long delayTime = 100; // longer than constants DELAY_TIME to ensure we can tick BEFORE the second future times out watchdog = new Watchdog(scheduler, delayTime * 2, true); SettableListenableFuture<?> slf1 = new SettableListenableFuture<>(); watchdog.watch(slf1); TestUtils.sleep(delayTime); SettableListenableFuture<?> slf2 = new SettableListenableFuture<>(); watchdog.watch(slf2); assertEquals(1, scheduler.blockingTick(null)); assertTrue(slf1.isCancelled()); assertFalse(slf2.isCancelled()); assertEquals(1, scheduler.blockingTick(null)); assertTrue(slf1.isCancelled()); assertTrue(slf2.isCancelled()); } }
private void removeRecurringRunnableTest(TaskPriority priority) { int runFrequency = 1; AbstractPrioritySchedulerFactory factory = getAbstractPrioritySchedulerFactory(); try { AbstractPriorityScheduler scheduler = factory.makeAbstractPriorityScheduler(1); TestRunnable removedTask = new TestRunnable(); TestRunnable keptTask = new TestRunnable(); scheduler.scheduleWithFixedDelay(removedTask, 0, runFrequency, priority); scheduler.scheduleWithFixedDelay(keptTask, 0, runFrequency, priority); removedTask.blockTillStarted(); assertFalse(scheduler.remove(new TestRunnable())); assertTrue(scheduler.remove(removedTask)); // verify removed is no longer running, and the kept task continues to run int keptRunCount = keptTask.getRunCount(); int runCount = removedTask.getRunCount(); TestUtils.sleep(runFrequency * 10); // may be +1 if the task was running while the remove was called assertTrue(removedTask.getRunCount() == runCount || removedTask.getRunCount() == runCount + 1); assertTrue(keptTask.getRunCount() >= keptRunCount); } finally { factory.shutdown(); } }