Skip to content

Commit

Permalink
Avoid using Java9+ APIs in JUnit5 CoroutineTimeout (#4279)
Browse files Browse the repository at this point in the history
Fixes #4278
  • Loading branch information
dkhalanskyjb authored Dec 9, 2024
1 parent e670f62 commit 0e1c157
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions integration-testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The tests are the following:
* `debugDynamicAgentTest` checks that `kotlinx-coroutines-debug` agent can self-attach dynamically to JVM as a standalone dependency.
* `debugDynamicAgentJpmsTest` checks that `kotlinx-coroutines-debug` agent can self-attach dynamically to JVM as a standalone dependency (with JPMS)
* `smokeTest` builds the multiplatform test project that depends on coroutines.
* `java8Test` checks that some APIs built with Java 9+ can be used with Java 8.

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

Expand Down
2 changes: 1 addition & 1 deletion integration-testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ compileTestKotlin {
}

check {
dependsOn([jvmCoreTest, debugDynamicAgentTest, mavenTest, debugAgentTest, coreAgentTest, ":jpmsTest:check", 'smokeTest:build'])
dependsOn([jvmCoreTest, debugDynamicAgentTest, mavenTest, debugAgentTest, coreAgentTest, ":jpmsTest:check", 'smokeTest:build', "java8Test:check"])
}
compileKotlin {
kotlinOptions {
Expand Down
1 change: 1 addition & 0 deletions integration-testing/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
kotlin_version=2.0.0
coroutines_version=1.9.0-SNAPSHOT
asm_version=9.3
junit5_version=5.7.0

kotlin.code.style=official
kotlin.mpp.stability.nowarn=true
25 changes: 25 additions & 0 deletions integration-testing/java8Test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
kotlin("jvm")
}

repositories {
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev")
// Coroutines from the outer project are published by previous CI buils step
mavenLocal()
}

tasks.test {
useJUnitPlatform()
}

val coroutinesVersion = property("coroutines_version")
val junit5Version = property("junit5_version")

kotlin {
jvmToolchain(8)
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutinesVersion")
testImplementation("org.junit.jupiter:junit-jupiter-engine:$junit5Version")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import kotlinx.coroutines.debug.junit5.CoroutinesTimeout
import org.junit.jupiter.api.*

class JUnit5TimeoutCompilation {
@CoroutinesTimeout(1000)
@Test
fun testCoroutinesTimeoutNotFailing() {
}
}
1 change: 1 addition & 0 deletions integration-testing/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pluginManagement {
}

include 'smokeTest'
include 'java8Test'
include(":jpmsTest")

rootProject.name = "kotlinx-coroutines-integration-testing"
2 changes: 1 addition & 1 deletion integration-testing/smokeTest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ rootProject.extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.
// canary nodejs that supports recent Wasm GC changes
it.nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
it.nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,12 @@ internal class CoroutinesTimeoutExtension internal constructor(
}

private fun<T> Class<T>.coroutinesTimeoutAnnotation(): Optional<CoroutinesTimeout> =
AnnotationSupport.findAnnotation(this, CoroutinesTimeout::class.java).or {
enclosingClass?.coroutinesTimeoutAnnotation() ?: Optional.empty()
AnnotationSupport.findAnnotation(this, CoroutinesTimeout::class.java).let {
when {
it.isPresent -> it
enclosingClass != null -> enclosingClass.coroutinesTimeoutAnnotation()
else -> Optional.empty()
}
}

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

0 comments on commit 0e1c157

Please sign in to comment.