In this part you will learn and exercise parallel execution feature for Spock.
First, familiarize yourself with tests in this module.
- Run tests
./gradlew --rerun-tasks :part1.0-introduction:test :part1.0-introduction:createTestsExecutionReport
- Check reports
- Enable parallel execution
in
SpockConfig.groovy
runner {
parallel {
enabled true
}
}
- Run tests
./gradlew --rerun-tasks :part1.0-introduction:test :part1.0-introduction:createTestsExecutionReport
- Check reports
Check other reports created by Gradle:
- two execution modes
SAME_THREAD
CONCURRENT
- defined
- for specifications (test classes) by
runner.parallel.defaultSpecificationExecutionMode
- for features (tests methods) by
runner.parallel.defaultExecutionMode
- for specifications (test classes) by
Sample configuration:
import org.spockframework.runtime.model.parallel.ExecutionMode
runner {
parallel {
enabled true
defaultSpecificationExecutionMode ExecutionMode.SAME_THREAD
defaultExecutionMode ExecutionMode.SAME_THREAD
}
}
- Set
runner {
parallel {
enabled false
}
}
- or
import org.spockframework.runtime.model.parallel.ExecutionMode
runner {
parallel {
enabled true
defaultSpecificationExecutionMode ExecutionMode.SAME_THREAD
defaultExecutionMode ExecutionMode.SAME_THREAD
}
}
- Run tests
./gradlew --rerun-tasks :part1.0-introduction:test :part1.0-introduction:createTestsExecutionReport
- Check reports
- Set
import org.spockframework.runtime.model.parallel.ExecutionMode
runner {
parallel {
enabled true
}
}
- or
import org.spockframework.runtime.model.parallel.ExecutionMode
runner {
parallel {
enabled true
// ExecutionMode.CONCURRENT is a default value
// of defaultSpecificationExecutionMode and defaultExecutionMode
defaultSpecificationExecutionMode ExecutionMode.CONCURRENT
defaultExecutionMode ExecutionMode.CONCURRENT
}
}
- Run tests
./gradlew --rerun-tasks :part1.0-introduction:test :part1.0-introduction:createTestsExecutionReport
- Check reports
- Set
import org.spockframework.runtime.model.parallel.ExecutionMode
runner {
parallel {
enabled true
defaultSpecificationExecutionMode ExecutionMode.CONCURRENT
defaultExecutionMode ExecutionMode.SAME_THREAD
}
}
- Run tests
./gradlew --rerun-tasks :part1.0-introduction:test :part1.0-introduction:createTestsExecutionReport
- Check reports
- Set
import org.spockframework.runtime.model.parallel.ExecutionMode
runner {
parallel {
enabled true
defaultSpecificationExecutionMode ExecutionMode.SAME_THREAD
defaultExecutionMode ExecutionMode.CONCURRENT
}
}
- Run tests
./gradlew --rerun-tasks :part1.0-introduction:test :part1.0-introduction:createTestsExecutionReport
- Check reports
- Enable parallel execution (concurrent features, concurrent specifications)
- Create a copy of class
A
and name itC
- Run tests
./gradlew --rerun-tasks :part1.0-introduction:test :part1.0-introduction:createTestsExecutionReport
- Check reports
- Add
@Isolated
(spock.lang.Isolated
) annotation toC
class - Run test again
./gradlew --rerun-tasks :part1.0-introduction:test :part1.0-introduction:createTestsExecutionReport
and check reports
Supported options:
dynamic(BigDecimal factor)
dynamicWithReservedProcessors(BigDecimal factor, int reservedProcessors)
fixed(int parallelism)
custom(int parallelism, int minimumRunnable, int maxPoolSize, int corePoolSize, int keepAliveSeconds)
(docs)
Default: dynamicWithReservedProcessors(1.0, 2)
(Runtime.getRuntime().availableProcessors() * 1.0 - 2
)
Example thread pool configuration:
runner {
parallel {
enabled true
fixed(4)
}
}
Check number of available processors:
jshell print-available-processors.jsh
Sample output:
Runtime.getRuntime().availableProcessors() = 10
- Add test case to class
A
def "test 3"() {
sleep SLEEP_DURATION
expect:
true
where:
data << (1..20)
}
- Remove class
C
- Run
tests
./gradlew --rerun-tasks :part1.0-introduction:test :part1.0-introduction:createTestsExecutionReport -PtotalTimeOfAllTests=false
- Check reports
- Configure a thread pool of your choice, run tests and check reports
- Randomize duration of tests. In test
test 3
replaceSLEEP_DURATION
withnew Random().nextLong(50, 250)
. Run tests and check reports.