Skip to content

Commit

Permalink
Refactor with enableTestConcurrency function
Browse files Browse the repository at this point in the history
  • Loading branch information
lijamie98 committed Oct 24, 2023
1 parent 462db42 commit 7e77882
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 87 deletions.
25 changes: 4 additions & 21 deletions api-schema/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,8 @@ dependencies {
annotationProcessor(libs.lombok)
}

tasks.test {
// Enable parallel test execution
systemProperty("junit.jupiter.execution.parallel.enabled", true)
// Allocate thread count based on available processors
systemProperty("junit.jupiter.execution.parallel.config.strategy", "dynamic")
// Set default parallel mode to same thread. All tests within a class are run in sequence.
systemProperty("junit.jupiter.execution.parallel.mode.default", "same_thread")
// Set default parallel mode for classes to concurrent. All test classes are run in parallel.
systemProperty("junit.jupiter.execution.parallel.mode.classes.default", "concurrent")
apply(from = "$rootDir/scripts.gradle.kts")
@Suppress("UNCHECKED_CAST")
val enableTestConcurrency = extra["enableTestConcurrency"] as (Test) -> Unit

// Set default test class order to order annotation. All test classes are run in parallel.
// Some tests take longer to run. Enabling the order will execute long-running tests first to
// shorten the overall test time.
systemProperty(
"junit.jupiter.testclass.order.default",
"org.junit.jupiter.api.ClassOrderer\$OrderAnnotation"
)
maxParallelForks =
(Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1).also {
println("junit5 ... setting maxParallelForks to $it")
}
}
tasks.test { enableTestConcurrency(this) }
8 changes: 5 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ subprojects {

test {
useJUnitPlatform()
systemProperty(
"junit.jupiter.testclass.order.default",
"org.junit.jupiter.api.ClassOrderer\$OrderAnnotation"
)

exclude("**/AnchorPlatformCustodyEnd2EndTest**")
exclude("**/AnchorPlatformCustodyApiRpcEnd2EndTest**")
Expand Down Expand Up @@ -181,6 +185,4 @@ allprojects {
}
}

tasks.register("printVersionName") {
println(rootProject.version.toString())
}
tasks.register("printVersionName") { println(rootProject.version.toString()) }
38 changes: 7 additions & 31 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies {
implementation(
libs.scala.library
) // used to force the version of scala-library (used by kafka-json-schema-serializer) to a safer
// one.
// one.
implementation(libs.bundles.kafka)

// TODO: Consider to simplify
Expand Down Expand Up @@ -119,33 +119,9 @@ publishing {
configure<SigningExtension> { sign(publishing.publications) }
}

// TODO: when we enable parallelization for all sub-projects, we can extract the following block.
tasks.test {
// Enable parallel test execution
systemProperty("junit.jupiter.execution.parallel.enabled", true)
// Use PER_METHOD test instance life cycle. This avoids the race condition when tests are run in parallel mode
// if the test class has a non-static fields. The non-static fields are shared across all test methods. If the life
// cycle is not PER_METHOD, the test methods may overwrite the fields and cause test failures.
//
// However, the life cycle can still be over-written by @TestInstance(Lifecycle) annotation.
// See https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution
systemProperty("junit.jupiter.testinstance.lifecycle.default", "per_method")
// Allocate thread count based on available processors
systemProperty("junit.jupiter.execution.parallel.config.strategy", "dynamic")
// Set default parallel mode to same thread. All tests within a class are run in sequence.
systemProperty("junit.jupiter.execution.parallel.mode.default", "concurrent")
// Set default parallel mode for classes to concurrent. All test classes are run in parallel.
systemProperty("junit.jupiter.execution.parallel.mode.classes.default", "concurrent")

// Set default test class order to order annotation. All test classes are run in parallel.
// Some tests take longer to run. Enabling the order will execute long-running tests first to
// shorten the overall test time.
systemProperty(
"junit.jupiter.testclass.order.default",
"org.junit.jupiter.api.ClassOrderer\$OrderAnnotation"
)
maxParallelForks =
(Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1).also {
println("junit5 ... setting maxParallelForks to $it")
}
}
apply(from = "$rootDir/scripts.gradle.kts")

@Suppress("UNCHECKED_CAST")
val enableTestConcurrency = extra["enableTestConcurrency"] as (Test) -> Unit

tasks.test { enableTestConcurrency(this) }
13 changes: 3 additions & 10 deletions integration-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ dependencies {
implementation("org.springframework.boot:spring-boot-autoconfigure")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation(
libs.snakeyaml) // used to force the version of snakeyaml (used by springboot) to a safer one.
libs.snakeyaml
) // used to force the version of snakeyaml (used by springboot) to a safer one.
implementation("org.springframework.boot:spring-boot-starter-web")

implementation(libs.commons.cli)
Expand Down Expand Up @@ -47,12 +48,4 @@ dependencies {
testImplementation(libs.dotenv)
}

tasks {
bootJar { enabled = false }
test {
useJUnitPlatform()
// Setting forkEvery to 1 makes Gradle test execution to start a separeate JVM for each integration test classes.
// This is to to avoid the interaction between static states between each integration test classes.
setForkEvery(1)
}
}
tasks { bootJar { enabled = false } }
27 changes: 5 additions & 22 deletions platform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,12 @@ dependencies {
testImplementation(libs.okhttp3.tls)
}

tasks.test {
// Enable parallel test execution
systemProperty("junit.jupiter.execution.parallel.enabled", true)
// Enable parallel test execution
systemProperty("junit.jupiter.testinstance.lifecycle.default", "per_method")
// Allocate thread count based on available processors
systemProperty("junit.jupiter.execution.parallel.config.strategy", "dynamic")
// Set default parallel mode to same thread. All tests within a class are run in sequence.
systemProperty("junit.jupiter.execution.parallel.mode.default", "concurrent")
// Set default parallel mode for classes to concurrent. All test classes are run in parallel.
systemProperty("junit.jupiter.execution.parallel.mode.classes.default", "concurrent")
apply(from = "$rootDir/scripts.gradle.kts")
@Suppress("UNCHECKED_CAST")
val enableTestConcurrency = extra["enableTestConcurrency"] as (Test) -> Unit

// Set default test class order to order annotation. All test classes are run in parallel.
// Some tests take longer to run. Enabling the order will execute long-running tests first to
// shorten the overall test time.
systemProperty(
"junit.jupiter.testclass.order.default",
"org.junit.jupiter.api.ClassOrderer\$OrderAnnotation"
)
maxParallelForks =
(Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1).also {
println("junit5 ... setting maxParallelForks to $it")
}
tasks.test {
enableTestConcurrency(this)
testLogging {
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
events = setOf(org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED)
Expand Down
24 changes: 24 additions & 0 deletions scripts.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
extra["enableTestConcurrency"] =
fun(test: Test) {
test.systemProperty("junit.jupiter.execution.parallel.enabled", true)
// Use PER_METHOD test instance life cycle. This avoids the race condition when tests are run in
// parallel mode and
// if the test class has a non-static fields. The non-static fields are shared across all test
// methods. If the life cycle is not PER_METHOD, the test methods may overwrite the fields and
// cause test failures.
//
// However, the life cycle can still be over-written by @TestInstance(Lifecycle) annotation.
// See https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution
test.systemProperty("junit.jupiter.testinstance.lifecycle.default", "per_method")
// Allocate thread count based on available processors
test.systemProperty("junit.jupiter.execution.parallel.config.strategy", "dynamic")
// Set default parallel mode to same thread. All tests within a class are run in sequence.
test.systemProperty("junit.jupiter.execution.parallel.mode.default", "concurrent")
// Set default parallel mode for classes to concurrent. All test classes are run in parallel.
test.systemProperty("junit.jupiter.execution.parallel.mode.classes.default", "concurrent")

test.maxParallelForks =
(Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1).also {
println("$test setting maxParallelForks to $it")
}
}

0 comments on commit 7e77882

Please sign in to comment.