Class Scheduler
- java.lang.Object
-
- org.apache.maven.surefire.junitcore.pc.Scheduler
-
- All Implemented Interfaces:
org.junit.runners.model.RunnerScheduler
public class Scheduler extends java.lang.Object implements org.junit.runners.model.RunnerScheduler
Schedules tests, controls thread resources, awaiting tests and other schedulers finished, and a master scheduler can shutdown slaves.
The scheduler objects should be first created (and wired) and set in runnersParentRunner.setScheduler(org.junit.runners.model.RunnerScheduler)
.
A new instance of scheduling strategy should be passed to the constructor of this scheduler.- Since:
- 2.16
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
Scheduler.Controller
If this is a master scheduler, the slaves can stop scheduling by the master through the controller.class
Scheduler.ShutdownHandler
There is a way to shutdown the hierarchy of schedulers.
-
Field Summary
Fields Modifier and Type Field Description private Balancer
balancer
private org.junit.runner.Description
description
private boolean
finished
private ConsoleStream
logger
private Scheduler.Controller
masterController
private boolean
shutdown
private java.util.Set<Scheduler.Controller>
slaves
private boolean
started
private SchedulingStrategy
strategy
-
Constructor Summary
Constructors Constructor Description Scheduler(ConsoleStream logger, org.junit.runner.Description description, Scheduler masterScheduler, SchedulingStrategy strategy)
Should be used with individual pools on suites, classes and methods, seeParallelComputerBuilder.useSeparatePools()
.Scheduler(ConsoleStream logger, org.junit.runner.Description description, Scheduler masterScheduler, SchedulingStrategy strategy, int concurrency)
Scheduler(ConsoleStream logger, org.junit.runner.Description description, Scheduler masterScheduler, SchedulingStrategy strategy, Balancer balancer)
Can be used by e.g.Scheduler(ConsoleStream logger, org.junit.runner.Description description, SchedulingStrategy strategy)
Use e.g.Scheduler(ConsoleStream logger, org.junit.runner.Description description, SchedulingStrategy strategy, int concurrency)
Should be used if schedulers in parallel children and parent use one instance of bounded thread pool.Scheduler(ConsoleStream logger, org.junit.runner.Description description, SchedulingStrategy strategy, Balancer balancer)
New instances should be used by schedulers with limited concurrency by balancer against other groups of schedulers.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
afterExecute()
protected void
beforeExecute()
private boolean
canSchedule()
protected ShutdownResult
describeStopped(boolean stopNow)
Attempts to stop all actively executing tasks and immediately returns a collection of descriptions of those tasks which have started prior to this call.void
finished()
protected void
logQuietly(java.lang.String msg)
protected void
logQuietly(java.lang.Throwable t)
protected Scheduler.ShutdownHandler
newShutdownHandler()
private boolean
register(Scheduler slave)
void
schedule(java.lang.Runnable childStatement)
private void
setController(Scheduler.Controller masterController)
protected boolean
shutdownThreadPoolsAwaitingKilled()
private void
stop(java.util.Collection<org.junit.runner.Description> executedTests, java.util.Collection<org.junit.runner.Description> incompleteTests, boolean tryCancelFutures, boolean stopNow)
Stop/Shutdown/Interrupt scheduler and its children (if any).private java.lang.Runnable
wrapTask(java.lang.Runnable task)
-
-
-
Field Detail
-
balancer
private final Balancer balancer
-
strategy
private final SchedulingStrategy strategy
-
slaves
private final java.util.Set<Scheduler.Controller> slaves
-
description
private final org.junit.runner.Description description
-
logger
private final ConsoleStream logger
-
shutdown
private volatile boolean shutdown
-
started
private volatile boolean started
-
finished
private volatile boolean finished
-
masterController
private volatile Scheduler.Controller masterController
-
-
Constructor Detail
-
Scheduler
public Scheduler(ConsoleStream logger, org.junit.runner.Description description, SchedulingStrategy strategy)
Use e.g. parallel classes have own non-shared thread pool, and methods another pool.
You can use it with one infinite thread pool shared in strategies across all suites, class runners, etc.- Parameters:
logger
- console loggerdescription
- JUnit description of classstrategy
- scheduling strategy
-
Scheduler
public Scheduler(ConsoleStream logger, org.junit.runner.Description description, SchedulingStrategy strategy, int concurrency)
Should be used if schedulers in parallel children and parent use one instance of bounded thread pool.
Set this scheduler in a e.g. one suite of classes, then every individual class runner should referenceScheduler(ConsoleStream, org.junit.runner.Description, Scheduler, SchedulingStrategy)
orScheduler(ConsoleStream, org.junit.runner.Description, Scheduler, SchedulingStrategy, int)
.- Parameters:
logger
- current logger implementationdescription
- description of current runnerstrategy
- scheduling strategy with a shared thread poolconcurrency
- determines maximum concurrent children scheduled a time viaschedule(Runnable)
- Throws:
java.lang.NullPointerException
- if null strategy
-
Scheduler
public Scheduler(ConsoleStream logger, org.junit.runner.Description description, SchedulingStrategy strategy, Balancer balancer)
New instances should be used by schedulers with limited concurrency by balancer against other groups of schedulers. The schedulers share one pool.
Unlike inScheduler(ConsoleStream, org.junit.runner.Description, SchedulingStrategy, int)
which was limiting the concurrency of children of a runner where this scheduler was set,this
balancer is limiting the concurrency of all children in runners having schedulers created by this constructor.- Parameters:
logger
- current logger implementationdescription
- description of current runnerstrategy
- scheduling strategy which may share threads with other strategybalancer
- determines maximum concurrent children scheduled a time viaschedule(Runnable)
- Throws:
java.lang.NullPointerException
- if null strategy or balancer
-
Scheduler
public Scheduler(ConsoleStream logger, org.junit.runner.Description description, Scheduler masterScheduler, SchedulingStrategy strategy, Balancer balancer)
Can be used by e.g. a runner having parallel classes in use case with parallel suites, classes and methods sharing the same thread pool.- Parameters:
logger
- current logger implementationdescription
- description of current runnermasterScheduler
- scheduler sharing own threads with this slavestrategy
- scheduling strategy for this schedulerbalancer
- determines maximum concurrent children scheduled a time viaschedule(Runnable)
- Throws:
java.lang.NullPointerException
- if null masterScheduler, strategy or balancer
-
Scheduler
public Scheduler(ConsoleStream logger, org.junit.runner.Description description, Scheduler masterScheduler, SchedulingStrategy strategy, int concurrency)
- Parameters:
logger
- console loggerdescription
- JUnit description of classmasterScheduler
- a reference toScheduler(ConsoleStream, org.junit.runner.Description, SchedulingStrategy, int)
orScheduler(ConsoleStream, org.junit.runner.Description, SchedulingStrategy)
strategy
- scheduling strategyconcurrency
- determines maximum concurrent children scheduled a time viaschedule(Runnable)
- See Also:
Scheduler(ConsoleStream, org.junit.runner.Description, SchedulingStrategy)
,Scheduler(ConsoleStream, org.junit.runner.Description, SchedulingStrategy, int)
-
Scheduler
public Scheduler(ConsoleStream logger, org.junit.runner.Description description, Scheduler masterScheduler, SchedulingStrategy strategy)
Should be used with individual pools on suites, classes and methods, seeParallelComputerBuilder.useSeparatePools()
.
Cached thread pool is infinite and can be always shared.- Parameters:
logger
- console loggerdescription
- JUnit description of classmasterScheduler
- parent schedulerstrategy
- scheduling strategy
-
-
Method Detail
-
setController
private void setController(Scheduler.Controller masterController)
-
register
private boolean register(Scheduler slave)
- Parameters:
slave
- a slave scheduler to register- Returns:
true
if successfully registered the slave.
-
canSchedule
private boolean canSchedule()
- Returns:
true
if new tasks can be scheduled.
-
logQuietly
protected void logQuietly(java.lang.Throwable t)
-
logQuietly
protected void logQuietly(java.lang.String msg)
-
describeStopped
protected ShutdownResult describeStopped(boolean stopNow)
Attempts to stop all actively executing tasks and immediately returns a collection of descriptions of those tasks which have started prior to this call.
This scheduler and other registered schedulers will stop, seeregister(Scheduler)
. If shutdownNow is set, waiting methods will be interrupted viaThread.interrupt()
.- Parameters:
stopNow
- iftrue
interrupts waiting test methods- Returns:
- collection of descriptions started before shutting down
-
stop
private void stop(java.util.Collection<org.junit.runner.Description> executedTests, java.util.Collection<org.junit.runner.Description> incompleteTests, boolean tryCancelFutures, boolean stopNow)
Stop/Shutdown/Interrupt scheduler and its children (if any).- Parameters:
executedTests
- Started tests which have finished normally or abruptly till called this method.incompleteTests
- Started tests which have finished incomplete due to shutdown.tryCancelFutures
- Useful to set tofalse
if a timeout is specified in plugin config. When the runner ofComputer.getSuite(org.junit.runners.model.RunnerBuilder, Class[])
is finished inParentRunner.run(org.junit.runner.notification.RunNotifier)
all the thread-pools created byParallelComputerBuilder.PC
are already dead. See the unit testParallelComputerBuilder#timeoutAndForcedShutdown()
.stopNow
- Interrupting tests byExecutorService.shutdownNow()
orFuture#cancel(true)
orThread.interrupt()
.
-
shutdownThreadPoolsAwaitingKilled
protected boolean shutdownThreadPoolsAwaitingKilled()
-
beforeExecute
protected void beforeExecute()
-
afterExecute
protected void afterExecute()
-
schedule
public void schedule(java.lang.Runnable childStatement)
- Specified by:
schedule
in interfaceorg.junit.runners.model.RunnerScheduler
-
finished
public void finished()
- Specified by:
finished
in interfaceorg.junit.runners.model.RunnerScheduler
-
wrapTask
private java.lang.Runnable wrapTask(java.lang.Runnable task)
-
newShutdownHandler
protected Scheduler.ShutdownHandler newShutdownHandler()
-
-