Skip to content

Commit 0e1c157

Browse files
authored
Avoid using Java9+ APIs in JUnit5 CoroutineTimeout (#4279)
Fixes #4278
1 parent e670f62 commit 0e1c157

File tree

8 files changed

+46
-5
lines changed

8 files changed

+46
-5
lines changed

integration-testing/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The tests are the following:
1010
* `debugDynamicAgentTest` checks that `kotlinx-coroutines-debug` agent can self-attach dynamically to JVM as a standalone dependency.
1111
* `debugDynamicAgentJpmsTest` checks that `kotlinx-coroutines-debug` agent can self-attach dynamically to JVM as a standalone dependency (with JPMS)
1212
* `smokeTest` builds the multiplatform test project that depends on coroutines.
13+
* `java8Test` checks that some APIs built with Java 9+ can be used with Java 8.
1314

1415
The `integration-testing` project is expected to be in a subdirectory of the main `kotlinx.coroutines` project.
1516

integration-testing/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ compileTestKotlin {
167167
}
168168

169169
check {
170-
dependsOn([jvmCoreTest, debugDynamicAgentTest, mavenTest, debugAgentTest, coreAgentTest, ":jpmsTest:check", 'smokeTest:build'])
170+
dependsOn([jvmCoreTest, debugDynamicAgentTest, mavenTest, debugAgentTest, coreAgentTest, ":jpmsTest:check", 'smokeTest:build', "java8Test:check"])
171171
}
172172
compileKotlin {
173173
kotlinOptions {

integration-testing/gradle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
kotlin_version=2.0.0
22
coroutines_version=1.9.0-SNAPSHOT
33
asm_version=9.3
4+
junit5_version=5.7.0
45

56
kotlin.code.style=official
67
kotlin.mpp.stability.nowarn=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
plugins {
2+
kotlin("jvm")
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev")
8+
// Coroutines from the outer project are published by previous CI buils step
9+
mavenLocal()
10+
}
11+
12+
tasks.test {
13+
useJUnitPlatform()
14+
}
15+
16+
val coroutinesVersion = property("coroutines_version")
17+
val junit5Version = property("junit5_version")
18+
19+
kotlin {
20+
jvmToolchain(8)
21+
dependencies {
22+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutinesVersion")
23+
testImplementation("org.junit.jupiter:junit-jupiter-engine:$junit5Version")
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import kotlinx.coroutines.debug.junit5.CoroutinesTimeout
2+
import org.junit.jupiter.api.*
3+
4+
class JUnit5TimeoutCompilation {
5+
@CoroutinesTimeout(1000)
6+
@Test
7+
fun testCoroutinesTimeoutNotFailing() {
8+
}
9+
}

integration-testing/settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pluginManagement {
88
}
99

1010
include 'smokeTest'
11+
include 'java8Test'
1112
include(":jpmsTest")
1213

1314
rootProject.name = "kotlinx-coroutines-integration-testing"

integration-testing/smokeTest/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ rootProject.extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.
7676
// canary nodejs that supports recent Wasm GC changes
7777
it.nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
7878
it.nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
79-
}
79+
}

kotlinx-coroutines-debug/src/junit/junit5/CoroutinesTimeoutExtension.kt

+7-3
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,12 @@ internal class CoroutinesTimeoutExtension internal constructor(
206206
}
207207

208208
private fun<T> Class<T>.coroutinesTimeoutAnnotation(): Optional<CoroutinesTimeout> =
209-
AnnotationSupport.findAnnotation(this, CoroutinesTimeout::class.java).or {
210-
enclosingClass?.coroutinesTimeoutAnnotation() ?: Optional.empty()
209+
AnnotationSupport.findAnnotation(this, CoroutinesTimeout::class.java).let {
210+
when {
211+
it.isPresent -> it
212+
enclosingClass != null -> enclosingClass.coroutinesTimeoutAnnotation()
213+
else -> Optional.empty()
214+
}
211215
}
212216

213217
private fun <T: Any?> interceptMethod(
@@ -232,7 +236,7 @@ internal class CoroutinesTimeoutExtension internal constructor(
232236
}
233237
/* The extension was registered via an annotation; check that we succeeded in finding the annotation that led to
234238
the extension being registered and taking its parameters. */
235-
if (testAnnotationOptional.isEmpty && classAnnotationOptional.isEmpty) {
239+
if (!testAnnotationOptional.isPresent && !classAnnotationOptional.isPresent) {
236240
throw UnsupportedOperationException("Timeout was registered with a CoroutinesTimeout annotation, but we were unable to find it. Please report this.")
237241
}
238242
return when {

0 commit comments

Comments
 (0)