Skip to content

Commit

Permalink
Use test.jdk property to determine JDK to be used for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Nov 25, 2024
1 parent dbaa0d1 commit 8203a49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
33 changes: 26 additions & 7 deletions buildSrc/src/main/kotlin/JvmConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ fun Project.configureJvm() {
}
}

val testJdk = project.testJdk
val jvmTest = tasks.named<KotlinJvmTest>("jvmTest") {
maxHeapSize = "2g"
exclude("**/*StressTest*")
useJUnitPlatform()
configureJavaToolchain(compileJdk)
configureJavaToolchain(compileJdk, testJdk)
}

tasks.register<Test>("stressTest") {
Expand All @@ -67,7 +68,7 @@ fun Project.configureJvm() {
systemProperty("enable.stress.tests", "true")
include("**/*StressTest*")
useJUnitPlatform()
configureJavaToolchain(compileJdk)
configureJavaToolchain(compileJdk, testJdk)
}

val configuredVersion: String by rootProject.extra
Expand All @@ -83,14 +84,32 @@ fun Project.configureJvm() {
}
}

private var _testJdk = 0

/**
* On local machine use for tests the JDK used for compilation.
* On CI use the default JDK.
* Retrieves the JDK version for running tests.
*
* Takes the version from property "test.jdk" or uses Gradle JDK by default.
* For example, to run tests against JDK 8, run test task with flag "-Ptest.jdk=8"
* or put this property to `gradle.properties`.
*/
private fun Test.configureJavaToolchain(compileJdk: Int) {
val testJdk = if (CI) currentJdk else compileJdk
val javaToolchains = project.the<JavaToolchainService>()
private val Project.testJdk: Int
get() {
if (_testJdk == 0) {
_testJdk = rootProject.properties["test.jdk"]?.toString()?.toInt() ?: currentJdk
logger.info("Running tests against JDK $_testJdk")
}
return _testJdk
}

/** Configure tests against different JDK versions. */
private fun Test.configureJavaToolchain(compileJdk: Int, testJdk: Int) {
if (testJdk < compileJdk) {
enabled = false
return
}

val javaToolchains = project.the<JavaToolchainService>()
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(testJdk)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ dependencyResolutionManagement {
}

fun VersionCatalogBuilder.downgradeTestDependencies() {
val testJdk = JavaVersion.current().majorVersion.toInt()
val testJdk = providers.gradleProperty("test.jdk").orNull?.toInt() ?: return

if (testJdk < 11) version("logback", "1.3.14") // Java 8 support dropped in Logback 1.4.x
}
14 changes: 6 additions & 8 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ include(":ktor-server:ktor-server-test-base")
include(":ktor-server:ktor-server-test-suites")
include(":ktor-server:ktor-server-jetty")
include(":ktor-server:ktor-server-jetty:ktor-server-jetty-test-http2")
include(":ktor-server:ktor-server-jetty-jakarta")
include(":ktor-server:ktor-server-jetty-jakarta:ktor-server-jetty-test-http2-jakarta")
include(":ktor-server:ktor-server-servlet")
include(":ktor-server:ktor-server-servlet-jakarta")
include(":ktor-server:ktor-server-tomcat")
include(":ktor-server:ktor-server-tomcat-jakarta")
include(":ktor-server:ktor-server-netty")
include(":ktor-server:ktor-server-cio")
include(":ktor-client")
Expand All @@ -40,15 +44,9 @@ include(":ktor-client:ktor-client-ios")
include(":ktor-client:ktor-client-darwin")
include(":ktor-client:ktor-client-darwin-legacy")
include(":ktor-client:ktor-client-winhttp")
if (JavaVersion.current() >= JavaVersion.VERSION_11) {
include(":ktor-client:ktor-client-java")
include(":ktor-client:ktor-client-jetty-jakarta")
include(":ktor-server:ktor-server-servlet-jakarta")
include(":ktor-server:ktor-server-jetty-jakarta")
include(":ktor-server:ktor-server-jetty-jakarta:ktor-server-jetty-test-http2-jakarta")
include(":ktor-server:ktor-server-tomcat-jakarta")
}
include(":ktor-client:ktor-client-java")
include(":ktor-client:ktor-client-jetty")
include(":ktor-client:ktor-client-jetty-jakarta")
include(":ktor-client:ktor-client-js")
include(":ktor-client:ktor-client-mock")
include(":ktor-client:ktor-client-okhttp")
Expand Down

0 comments on commit 8203a49

Please sign in to comment.