private MockBigArrays(PageCacheRecycler recycler, CircuitBreakerService breakerService, boolean checkBreaker) { super(recycler, breakerService, checkBreaker); this.recycler = recycler; this.breakerService = breakerService; long seed; try { seed = SeedUtils.parseSeed(RandomizedContext.current().getRunnerSeedAsString()); } catch (IllegalStateException e) { // rest tests don't run randomized and have no context seed = 0; } random = new Random(seed); }
/** * Registers a {@link Closeable} resource that should be closed after the test * completes. * * @return <code>resource</code> (for call chaining). */ public <T extends Closeable> T closeAfterTest(T resource) { return RandomizedContext.current().closeAtEnd(resource, LifecycleScope.TEST); }
private TestCluster buildWithPrivateContext(final Scope scope, final long seed) throws Exception { return RandomizedContext.current().runWithPrivateRandomness(seed, new Callable<TestCluster>() { @Override public TestCluster call() throws Exception { return buildTestCluster(scope, seed); } }); }
public static void initClassLogLevels() { Class currentClass = RandomizedContext.current().getTargetClass(); LogLevel annotation = (LogLevel) currentClass.getAnnotation(LogLevel.class); if (annotation == null) { return; } Map<String, Level> previousLevels = LogLevel.Configurer.setLevels(annotation.value()); savedClassLogLevels.putAll(previousLevels); }
@Test public void printContext() { System.out.println("# Test context (@Test)"); System.out.println(getContext().getRunnerSeedAsString()); System.out.println(RandomizedContext.current().getRandomness()); }
final RandomizedContext current = RandomizedContext.current(); try { Thread.currentThread().setName(testThreadName); current.push(new Randomness(c.seed, randomSupplier)); current.setTargetMethod(c.method); current.setTargetMethod(null); current.popAndDestroy();
/** * Ensures we're running with an initialized {@link RandomizedContext}. */ private static void checkContext() { // Will throw an exception if not available. RandomizedContext.current(); } }
@Test public void nightlyOnlyWithAssume() throws Exception { // Only run if Nightly test group is explicitly enabled using -Dtests.nightly=true Assume.assumeTrue(RandomizedContext.current().getGroupEvaluator().isGroupEnabled(Nightly.class)); }
/** * Augment stack trace of the given exception with seed infos. */ static <T extends Throwable> T augmentStackTrace(T e, Randomness... seeds) { if (seeds.length == 0) { seeds = RandomizedContext.current().getRandomnesses(); } final String seedChain = SeedUtils.formatSeedChain(seeds); final String existingSeed = seedFromThrowable(e); if (existingSeed != null && existingSeed.equals(seedChain)) { return e; } List<StackTraceElement> stack = new ArrayList<StackTraceElement>( Arrays.asList(e.getStackTrace())); stack.add(0, new StackTraceElement(AUGMENTED_SEED_PACKAGE + ".SeedInfo", "seed", seedChain, 0)); e.setStackTrace(stack.toArray(new StackTraceElement [stack.size()])); return e; }
@Override protected void afterAlways(final List<Throwable> errors) throws Throwable { final ObjectProcedure<CloseableResourceInfo> disposer = new ObjectProcedure<CloseableResourceInfo>() { public void apply(CloseableResourceInfo info) { try { info.getResource().close(); } catch (Throwable t) { ResourceDisposalError e = new ResourceDisposalError( "Resource in scope " + info.getScope().name() + " failed to close. Resource was" + " registered from thread " + info.getThreadName() + ", registration stack trace below.", t); e.setStackTrace(info.getAllocationStack()); errors.add(e); } } }; RandomizedContext.current().closeResources(disposer, scope); } };
@Override @SuppressForbidden("Legitimate use of syserr.") public void testFailure(Failure failure) throws Exception { // Ignore assumptions. if (failure.getException() instanceof AssumptionViolatedException) { return; } final Description d = failure.getDescription(); final StringBuilder b = new StringBuilder(); b.append("FAILURE : ").append(d.getDisplayName()).append("\n"); b.append("Message : " + failure.getMessage() + "\n"); b.append("Reproduce: "); new ReproduceErrorMessageBuilder(b).appendAllOpts(failure.getDescription()); b.append("\n"); b.append("Throwable:\n"); if (failure.getException() != null) { TraceFormatting traces = new TraceFormatting(); try { traces = RandomizedContext.current().getRunner().getTraceFormatting(); } catch (IllegalStateException e) { // Ignore if no context. } traces.formatThrowable(b, failure.getException()); } System.err.println(b.toString()); } }
@Before public void initMethodLogLevels() { Method method = RandomizedContext.current().getTargetMethod(); LogLevel annotation = method.getAnnotation(LogLevel.class); if (annotation == null) { return; } Map<String, Level> previousLevels = LogLevel.Configurer.setLevels(annotation.value()); savedMethodLogLevels.putAll(previousLevels); }
/** * Shortcut for {@link RandomizedContext#getRandom()}. Even though this method * is static, it returns per-thread {@link Random} instance, so no race conditions * can occur. * * <p>It is recommended that specific methods are used to pick random values. */ public static Random getRandom() { return getContext().getRandom(); }
/** * Registers a {@link Closeable} resource that should be closed after the suite * completes. * * @return <code>resource</code> (for call chaining). */ public static <T extends Closeable> T closeAfterSuite(T resource) { return getContext().closeAtEnd(resource, LifecycleScope.SUITE); }
/** * Pushes the given randomness to the top of the stack, runs the {@link Callable} and disposes * the randomness before the this method returns. * <p> * This utility method can be used to initialize resources in a reproducible way since all calls to utility methods * like {@link com.carrotsearch.randomizedtesting.RandomizedTest#randomInt()} et.al. are forwarded to the current * RandomContext which uses the provided randomness from the top of the stack. * </p> * * @param seed The initial seed for the new, private randomness * @param callable the callable to execute * @param <T> the return type of the callable * @return the result of the call to {@link java.util.concurrent.Callable#call()} * @throws Exception if {@link java.util.concurrent.Callable#call()} throws an exception */ public <T> T runWithPrivateRandomness(long seed, Callable<T> callable) throws Exception { Randomness randomness = getRandomness(); Randomness prv = new Randomness(seed, randomness.getRandomSupplier(), randomness.getDecorators()); return runWithPrivateRandomness(prv, callable); }
/** * Return <code>true</code> if {@link Nightly} test group is explicitly enabled. * * @see RandomizedContext#getGroupEvaluator() * @see GroupEvaluator#isGroupEnabled(Class) */ public boolean isNightly() { checkDisposed(); return getGroupEvaluator().isGroupEnabled(Nightly.class); }
/** * A shorthand for calling {@link #getRandomness()} and then {@link Randomness#getRandom()}. */ public Random getRandom() { return getRandomness().getRandom(); }
public ReproduceErrorMessageBuilder appendTestGroupOptions(RandomizedContext ctx) { if (ctx != null) { ctx.getGroupEvaluator().appendGroupFilteringOptions(this); } return this; }
@Override public void evaluate() throws Throwable { Class<?> testClass; try { testClass = RandomizedContext.current().getTargetClass(); } catch (Throwable t) { testClass = description.getTestClass(); } validate(testClass); base.evaluate(); } };