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 Dec 11, 2024
1 parent 473de1a commit 9a6715e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
19 changes: 10 additions & 9 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,14 @@ fun Project.configureJvm() {
}
}

/**
* On local machine use for tests the JDK used for compilation.
* On CI use the default JDK.
*/
private fun Test.configureJavaToolchain(compileJdk: Int) {
val testJdk = if (CI) currentJdk else compileJdk
val javaToolchains = project.the<JavaToolchainService>()
/** 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
19 changes: 18 additions & 1 deletion buildSrc/src/main/kotlin/KtorBuildProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,24 @@ val HOST_NAME = when {
else -> error("Unknown os name `$OS_NAME`")
}

val currentJdk = JavaVersion.current().majorVersion.toInt()
private var _testJdk = 0

/**
* 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`.
*/
val Project.testJdk: Int
get() {
if (_testJdk == 0) {
_testJdk = rootProject.properties["test.jdk"]?.toString()?.toInt()
?: JavaVersion.current().majorVersion.toInt()
logger.info("Running tests against JDK $_testJdk")
}
return _testJdk
}

val Project.requiredJdkVersion: Int
get() = when {
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
}
2 changes: 1 addition & 1 deletion ktor-client/ktor-client-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ kotlin.sourceSets {
api(project(":ktor-client:ktor-client-apache5"))
runtimeOnly(project(":ktor-client:ktor-client-android"))
runtimeOnly(project(":ktor-client:ktor-client-okhttp"))
if (currentJdk >= 11) {
if (testJdk >= 11) {
runtimeOnly(project(":ktor-client:ktor-client-java"))
}
implementation(project(":ktor-client:ktor-client-plugins:ktor-client-logging"))
Expand Down
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 9a6715e

Please sign in to comment.