@Override public void run() { TestUtils.sleep(DELAY_TIME); f.cancel(true); } }).start();
@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()); } }
@Test public void cacheCleanTest() { SettableListenableFuture<Object> slf = new SettableListenableFuture<>(); watchdog.watch(slf, TIMEOUT); assertFalse(watchdog.cachedDogs.isEmpty()); TestUtils.blockTillClockAdvances(); assertEquals(2, scheduler.advance(WatchdogCache.INSPECTION_INTERVAL_MILLIS)); assertTrue(watchdog.cachedDogs.isEmpty()); }
@Test public void blockTillClockAdvancesTest() { long before = Clock.accurateTimeMillis(); TestUtils.blockTillClockAdvances(); assertTrue(Clock.lastKnownTimeMillis() != before); } }
@Override public void run() { while (! aboutToSleep.get()) { // spin } TestUtils.sleep(DELAY_TIME); testThread.interrupt(); } });
@Test public void getExecutionDelayPercentilesTest() { NoThreadSchedulerStatisticTracker scheduler = new NoThreadSchedulerStatisticTracker(); assertEquals(0, scheduler.getExecutionDelayPercentiles(90).get(90.), 0); scheduler.execute(new ClockUpdateRunnable()); TestUtils.blockTillClockAdvances(); scheduler.tick(null); assertEquals(1, scheduler.getExecutionDelayPercentiles(90).get(90.), SLOW_MACHINE ? 50 : 20); }
@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; } } }
protected void getPriorityExecutionDelayPercentilesTest(TaskPriority priority) { NoThreadSchedulerStatisticTracker scheduler = new NoThreadSchedulerStatisticTracker(); assertEquals(0, scheduler.getExecutionDelayPercentiles(90).get(90.), 0); scheduler.execute(new ClockUpdateRunnable(), priority); TestUtils.blockTillClockAdvances(); scheduler.tick(null); assertEquals(1, scheduler.getExecutionDelayPercentiles(90).get(90.), SLOW_MACHINE ? 50 : 20); }
@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; } } }
scheduler.execute(DoNothingRunnable.instance(), TaskPriority.Low); TestUtils.blockTillClockAdvances(); scheduler.tick(null); for (int i = 0; i < highPriorityCount; i++) { scheduler.execute(DoNothingRunnable.instance(), TaskPriority.High); TestUtils.blockTillClockAdvances(); scheduler.tick(null);
@Override public void handleRunStart() throws InterruptedException { if (firstRun) { firstRun = false; TestUtils.sleep(DELAY_TIME); run(); } } };
for (int i = 0; i < lowPriorityCount; i++) { if (lastRunnable != null) { TestUtils.blockTillClockAdvances(); lastRunnable.unblock(); TestUtils.blockTillClockAdvances(); lastRunnable.unblock(); lastRunnable = new BlockingTestRunnable(); scheduler.execute(lastRunnable, TaskPriority.High); TestUtils.blockTillClockAdvances(); lastRunnable.unblock();
@Override public Object call() { callTime = Clock.accurateForwardProgressingMillis(); handleCallStart(); TestUtils.sleep(runDurration); done = true; return result; }
@Test public void expiredFutureTest() { SettableListenableFuture<?> slf = new SettableListenableFuture<>(); watchdog.watch(slf); TestUtils.blockTillClockAdvances(); assertEquals(1, scheduler.tick(null)); assertTrue(slf.isCancelled()); assertTrue(watchdog.futures.isEmpty()); }
@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()); }
public static void getExecutionDelayPercentilesTest(StatisticPriorityScheduler scheduler) { BlockingTestRunnable btr = new BlockingTestRunnable(); try { assertEquals(0, scheduler.getExecutionDelayPercentiles(90).get(90.), 0); scheduler.execute(btr); scheduler.execute(new ClockUpdateRunnable()); TestUtils.blockTillClockAdvances(); btr.unblock(); assertEquals(2, scheduler.getExecutionDelayPercentiles(90).get(90.), SLOW_MACHINE ? 50 : 20); } finally { btr.unblock(); } }
@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 forwardProgressingDurationTest() { long startTime = Clock.lastKnownForwardProgressingMillis(); assertEquals(0, Clock.forwardProgressingDuration(startTime)); TestUtils.blockTillClockAdvances(); assertTrue(Clock.forwardProgressingDuration(startTime) > 0); }
@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 lastKnownForwardProgressingMillisAccurateTimeUpdateTest() { // verify clock is not updating long before = Clock.lastKnownForwardProgressingMillis(); TestUtils.blockTillClockAdvances(); // update clock long newTime = -1; assertTrue((newTime = Clock.accurateForwardProgressingMillis()) > before); // verify we get the new time again assertTrue(newTime == Clock.lastKnownForwardProgressingMillis()); }