private static Runner parallelize(Runner runner) { int nThreads = Integer.getInteger(Constants.NTHREADS, Runtime.getRuntime().availableProcessors()); LOGGER.info("Using " + nThreads + " threads."); if (runner instanceof ParentRunner) { ((ParentRunner<?>) runner).setScheduler(new RunnerScheduler() { private final ExecutorService fService = Executors.newFixedThreadPool(nThreads); @Override public void schedule(Runnable childStatement) { fService.submit(childStatement); } @Override public void finished() { try { fService.shutdown(); fService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { e.printStackTrace(System.err); } } }); } return runner; }
private void runChildren(@SuppressWarnings("hiding") final RunNotifier notifier) { RunnerScheduler currentScheduler = scheduler; try { List<FrameworkMethod> roots = graph.getRoots().stream().map(r -> nameToMethod.get(r)).collect(Collectors.toList()); for (FrameworkMethod each : roots) { currentScheduler.schedule(new Runnable() { @Override public void run() { ConcurrentDependsOnRunner.this.runChild(each, notifier); } }); } } finally { currentScheduler.finished(); } }
private void runChildren(@SuppressWarnings("hiding") final RunNotifier notifier) { RunnerScheduler currentScheduler = scheduler; try { List<Runner> roots = graph.getRoots().stream().map(r -> nameToRunner.get(r)).collect(Collectors.toList()); for (Runner each : roots) { currentScheduler.schedule(new Runnable() { @Override public void run() { ConcurrentDependsOnClasspathSuite.this.runChild(each, notifier); } }); } } finally { currentScheduler.finished(); } }
public Parallelized(Class<?> klass) throws Throwable { super(klass); setScheduler(new RunnerScheduler() { private final ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); public void schedule(Runnable childStatement) { service.submit(childStatement); } public void finished() { try { service.shutdown(); service.awaitTermination(5, TimeUnit.MINUTES); } catch (InterruptedException e) { throw new RuntimeException(e); } } }); }
public ParallelSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError { super(klass, builder); setScheduler(new RunnerScheduler() { private final ExecutorService service = Executors.newCachedThreadPool(); public void schedule(Runnable childStatement) { service.submit(childStatement); } public void finished() { try { service.shutdown(); service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { e.printStackTrace(System.err); } } }); }
private static Runner parallelize(Runner runner) { if (runner instanceof ParentRunner) { ((ParentRunner<?>) runner).setScheduler(new RunnerScheduler() { private final ExecutorService fService = Executors.newCachedThreadPool(); public void schedule(Runnable childStatement) { fService.submit(childStatement); } public void finished() { try { fService.shutdown(); fService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { e.printStackTrace(System.err); } } }); } return runner; }
@Test public void useChildHarvester() throws InitializationError { log = ""; ParentRunner<?> runner = new BlockJUnit4ClassRunner(FruitTest.class); runner.setScheduler(new RunnerScheduler() { public void schedule(Runnable childStatement) { log += "before "; childStatement.run(); log += "after "; } public void finished() { log += "afterAll "; } }); runner.run(new RunNotifier()); assertEquals("before apple after before banana after afterAll ", log); }
public ParallelParameterized(Class<?> arg0) throws Throwable { super(arg0); setScheduler(new RunnerScheduler() { private final ExecutorService service = Executors.newFixedThreadPool(8); public void schedule(Runnable childStatement) { service.submit(childStatement); } public void finished() { try { service.shutdown(); service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { e.printStackTrace(System.err); } } }); }
/** Coppied from super constructor: Only called reflectively. Do not use problematically. */ public ParallelParameterized(final Class<?> test_class) throws Throwable { super(test_class); final String index_as_string = System.getProperty(TEST_PARAM_INDEX); next_host_index = new AtomicInteger(); parameter_index = index_as_string != null ? Integer.valueOf(index_as_string) : null; configuration = getParallelizationAnnotation(); process_builder = new AgentBasedJavaProcessBuilder(); configure(configuration); setScheduler(new RunnerScheduler() { @Override public void schedule(final Runnable childStatement) { executor_service.execute(childStatement); } @Override public void finished() { executor_service.shutdown(); try { executor_service.awaitTermination(10, TimeUnit.MINUTES); } catch (InterruptedException exc) { throw new RuntimeException(exc); } } }); }
@Override public void setScheduler(RunnerScheduler scheduler) { for (Runner child : getChildren()) { if (child instanceof ParentRunner<?>) { ((ParentRunner<?>) child).setScheduler(scheduler); } } }
public ConcurrentJunitRunner(final Class<?> klass) throws InitializationError { super(klass); setScheduler(new RunnerScheduler() { ExecutorService executorService = Executors.newFixedThreadPool( klass.isAnnotationPresent(Concurrent.class) ? klass.getAnnotation(Concurrent.class).threads() : (int) (Runtime.getRuntime().availableProcessors() * 1.5), new NamedThreadFactory(klass.getSimpleName())); CompletionService<Void> completionService = new ExecutorCompletionService<>(executorService); Queue<Future<Void>> tasks = new LinkedList<>(); @Override public void schedule(Runnable childStatement) { tasks.offer(completionService.submit(childStatement, null)); } @Override public void finished() { try { while (!tasks.isEmpty()) tasks.remove(completionService.take()); } catch (@SuppressWarnings("unused") InterruptedException e) { Thread.currentThread().interrupt(); } finally { while (!tasks.isEmpty()) tasks.poll().cancel(true); executorService.shutdownNow(); } } }); }
private void runChildren(final RunNotifier notifier) { final RunnerScheduler scheduler = fScheduler; try { for (final T each : getFilteredChildren()) { scheduler.schedule(new Runnable() { public void run() { ParentRunner.this.runChild(each, notifier); } }); } } finally { scheduler.finished(); } }
private void runChildren(final RunNotifier notifier) { final RunnerScheduler currentScheduler = scheduler; try { for (final FrameworkMethod each : getFilteredChildren()) { currentScheduler.schedule(new Runnable() { public void run() { runChild(each, notifier); } }); } } finally { currentScheduler.finished(); } }
private void runChildren(final RunNotifier notifier) { final RunnerScheduler currentScheduler = scheduler; try { for (final Runner each : getFilteredChildren()) { currentScheduler.schedule(new Runnable() { public void run() { runChild(each, notifier); } }); } } finally { currentScheduler.finished(); } }
@Override public void setScheduler(RunnerScheduler scheduler) { super.setScheduler(scheduler); this.scheduler = scheduler; }
public ConcurrentSuite(final Class<?> klass) throws InitializationError { super(klass, new AllDefaultPossibilitiesBuilder(true) { @Override public Runner runnerForClass(Class<?> testClass) throws Throwable { List<RunnerBuilder> builders = Arrays.asList( new RunnerBuilder() { @Override public Runner runnerForClass(Class<?> testClass2) throws Throwable { Concurrent annotation = testClass2.getAnnotation(Concurrent.class); if (annotation != null) return new ConcurrentJunitRunner(testClass2); return null; } }, ignoredBuilder(), annotatedBuilder(), suiteMethodBuilder(), junit3Builder(), junit4Builder()); for (RunnerBuilder each : builders) { Runner runner = each.safeRunnerForClass(testClass); if (runner != null) return runner; } return null; } }); setScheduler(new RunnerScheduler() { ExecutorService executorService = Executors.newFixedThreadPool( klass.isAnnotationPresent(Concurrent.class) ? klass.getAnnotation(Concurrent.class).threads() : (int) (Runtime.getRuntime().availableProcessors() * 1.5), new NamedThreadFactory(klass.getSimpleName())); CompletionService<Void> completionService = new ExecutorCompletionService<>(executorService); Queue<Future<Void>> tasks = new LinkedList<>(); @Override public void schedule(Runnable childStatement) { tasks.offer(completionService.submit(childStatement, null)); } @Override public void finished() { try { while (!tasks.isEmpty()) tasks.remove(completionService.take()); } catch (@SuppressWarnings("unused") InterruptedException e) { Thread.currentThread().interrupt(); } finally { while (!tasks.isEmpty()) tasks.poll().cancel(true); executorService.shutdownNow(); } } }); }
/** * Sets a scheduler that determines the order and parallelization * of children. Highly experimental feature that may change. */ public void setScheduler(RunnerScheduler scheduler) { this.fScheduler = scheduler; }