/** * Blocks until run has been called at least once. * * @param timeout time to wait for run to be called before throwing exception */ public void blockTillStarted(int timeout) { new TestCondition(() -> ! runTime.isEmpty()) .blockTillTrue(timeout, RUN_CONDITION_POLL_INTERVAL); }
@Test (expected = RuntimeException.class) public void defaultConstructorFail() { TestCondition tc = new TestCondition(); tc.get(); }
/** * Construct a new {@link TestCondition} with a provided supplier for when the state changes. * Alternatively this can be constructed with {@link #TestCondition()} and then the condition can * be reported by overriding {@link #get()}. * * @since 5.0 * @param condition Condition to check for */ public TestCondition(Supplier<Boolean> condition) { blockCondition = (timeoutInMillis, pollIntervalInMillis) -> { long startTime = Clock.accurateForwardProgressingMillis(); long now = startTime; boolean lastResult; while (! (lastResult = condition == null ? get() : condition.get()) && ! Thread.currentThread().isInterrupted() && (now = Clock.accurateForwardProgressingMillis()) - startTime < timeoutInMillis) { delay(pollIntervalInMillis); } if (lastResult) { return 0; // ignored result } else { throw new ConditionTimeoutException("Still 'false' after " + (now - startTime) + "ms, interrupted: " + Thread.currentThread().isInterrupted()); } }; }
/** * Blocks till condition is true, useful for asynchronism operations, waiting for them to * complete in other threads during unit tests. * <p> * This uses a default timeout of 10 seconds, and a poll interval of 10ms */ public void blockTillTrue() { blockTillTrue(DEFAULT_TIMEOUT, DEFAULT_POLL_INTERVAL); }
! Thread.currentThread().isInterrupted() && (now = Clock.accurateForwardProgressingMillis()) - startTime < timeoutInMillis) { delay(pollIntervalInMillis);
/** * Blocks till condition is true, useful for asynchronism operations, waiting for them to * complete in other threads during unit tests. * <p> * This uses the default poll interval of 10ms * * @param timeoutInMillis time to wait for value to become true */ public void blockTillTrue(int timeoutInMillis) { blockTillTrue(timeoutInMillis, DEFAULT_POLL_INTERVAL); }
! Thread.currentThread().isInterrupted() && (now = Clock.accurateForwardProgressingMillis()) - startTime < timeoutInMillis) { delay(pollIntervalInMillis);
/** * Blocks until run has been called at least once. * * @param timeout time to wait for run to be called before throwing exception */ public void blockTillStarted(int timeout) { new TestCondition(() -> ! runTime.isEmpty()) .blockTillTrue(timeout, RUN_CONDITION_POLL_INTERVAL); }
/** * Construct a new {@link TestCondition} with a provided supplier for when the state changes. * Alternatively this can be constructed with {@link #TestCondition()} and then the condition can * be reported by overriding {@link #get()}. * * @since 5.0 * @param condition Condition to check for */ public TestCondition(Supplier<Boolean> condition) { blockCondition = (timeoutInMillis, pollIntervalInMillis) -> { long startTime = Clock.accurateForwardProgressingMillis(); long now = startTime; boolean lastResult; while (! (lastResult = condition == null ? get() : condition.get()) && ! Thread.currentThread().isInterrupted() && (now = Clock.accurateForwardProgressingMillis()) - startTime < timeoutInMillis) { delay(pollIntervalInMillis); } if (lastResult) { return 0; // ignored result } else { throw new ConditionTimeoutException("Still 'false' after " + (now - startTime) + "ms, interrupted: " + Thread.currentThread().isInterrupted()); } }; }
/** * Blocks till condition is true, useful for asynchronism operations, waiting for them to * complete in other threads during unit tests. * <p> * This uses the default poll interval of 10ms * * @param timeoutInMillis time to wait for value to become true */ public void blockTillTrue(int timeoutInMillis) { blockTillTrue(timeoutInMillis, DEFAULT_POLL_INTERVAL); }
/** * Blocks until run completed been called the provided quantity of times. * * @param timeout time to wait for run to be called before throwing exception * @param expectedRunCount run count to wait for */ public void blockTillFinished(int timeout, int expectedRunCount) { final int blockRunCount = expectedRunCount; new TestCondition(() -> { int finishCount = runCount.get(); if (finishCount < blockRunCount) { return false; } else if (finishCount > blockRunCount) { return true; } else { // they are equal return currentRunningCount.get() == 0; } }).blockTillTrue(timeout, RUN_CONDITION_POLL_INTERVAL); }
/** * Blocks till condition is true, useful for asynchronism operations, waiting for them to * complete in other threads during unit tests. * <p> * This uses a default timeout of 10 seconds, and a poll interval of 10ms */ public void blockTillTrue() { blockTillTrue(DEFAULT_TIMEOUT, DEFAULT_POLL_INTERVAL); }
/** * Blocks until run completed been called the provided quantity of times. * * @param timeout time to wait for run to be called before throwing exception * @param expectedRunCount run count to wait for */ public void blockTillFinished(int timeout, int expectedRunCount) { final int blockRunCount = expectedRunCount; new TestCondition(() -> { int finishCount = runCount.get(); if (finishCount < blockRunCount) { return false; } else if (finishCount > blockRunCount) { return true; } else { // they are equal return currentRunningCount.get() == 0; } }).blockTillTrue(timeout, RUN_CONDITION_POLL_INTERVAL); }
/** * Blocks until the System clock advances at least 1 millisecond. This will also ensure that * the {@link Clock} class's representation of time has advanced. */ public static void blockTillClockAdvances() { long startTime = Clock.accurateTimeMillis(); long alwaysProgressingStartTime = Clock.accurateForwardProgressingMillis(); new TestCondition(() -> Clock.accurateTimeMillis() > startTime && Clock.accurateForwardProgressingMillis() > alwaysProgressingStartTime) .blockTillTrue(TestCondition.DEFAULT_TIMEOUT, 1); } }
/** * Blocks until the System clock advances at least 1 millisecond. This will also ensure that * the {@link Clock} class's representation of time has advanced. */ public static void blockTillClockAdvances() { long startTime = Clock.accurateTimeMillis(); long alwaysProgressingStartTime = Clock.accurateForwardProgressingMillis(); new TestCondition(() -> Clock.accurateTimeMillis() > startTime && Clock.accurateForwardProgressingMillis() > alwaysProgressingStartTime) .blockTillTrue(TestCondition.DEFAULT_TIMEOUT, 1); } }
private void verifyDumpContains(String str) { new TestCondition(() -> profiler.dump(false), (s) -> s.contains(str)).blockTillTrue(); }
@Override protected void profilingExecutor(Executor executor) { AtomicReference<Thread> tAR = new AtomicReference<>(); executor.execute(() -> tAR.set(Thread.currentThread())); new TestCondition(() -> tAR.get() != null).blockTillTrue(); ctProfiler.addProfiledThread(tAR.get()); }
@Test public void supplierPredicateTest() { Queue<String> values = new ArrayDeque<>(8); values.add("foo"); values.add("false"); values.add("true"); new TestCondition(() -> values.remove(), Boolean::parseBoolean).blockTillTrue(); }
public static void blockTillSchedulerIdle(final StatisticPriorityScheduler scheduler, final int expectedSampleSize) { new TestCondition(() -> scheduler.getActiveTaskCount() == 0 && scheduler.getExecutionDurationSamples().size() >= expectedSampleSize) // block till all are finished .blockTillTrue(); }
protected void blockForProfilerSample() { int startCount = profiler.getCollectedSampleQty(); new TestCondition(() -> profiler.getCollectedSampleQty() > startCount).blockTillTrue(1000 * 20); }