Skip to content

Commit 96610ad

Browse files
committed
~comment, better naming
1 parent 83464e7 commit 96610ad

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

kotlinx-coroutines-core/common/test/TestBase.common.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public expect val isStressTest: Boolean
1414
public expect val stressTestMultiplier: Int
1515

1616
public expect open class TestBase constructor() {
17-
public val isJs: Boolean
17+
public val isBoundByJsTestTimeout: Boolean
1818
public fun error(message: Any, cause: Throwable? = null): Nothing
1919
public fun expect(index: Int)
2020
public fun expectUnreached()

kotlinx-coroutines-core/common/test/flow/sharing/SharedFlowTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class SharedFlowTest : TestBase() {
440440

441441
@Test
442442
fun testDifferentBufferedFlowCapacities() = runTest {
443-
if (isJs) return@runTest // Too slow for JS, bounded by 2 sec. default JS timeout
443+
if (isBoundByJsTestTimeout) return@runTest // Too slow for JS, bounded by 2 sec. default JS timeout
444444
for (replay in 0..10) {
445445
for (extraBufferCapacity in 0..5) {
446446
if (replay == 0 && extraBufferCapacity == 0) continue // test only buffered shared flows
@@ -679,7 +679,7 @@ class SharedFlowTest : TestBase() {
679679

680680
@Test
681681
fun testStateFlowModel() = runTest {
682-
if (isJs) return@runTest // Too slow for JS, bounded by 2 sec. default JS timeout
682+
if (isBoundByJsTestTimeout) return@runTest // Too slow for JS, bounded by 2 sec. default JS timeout
683683
val stateFlow = MutableStateFlow<Data?>(null)
684684
val expect = modelLog(stateFlow)
685685
val sharedFlow = MutableSharedFlow<Data?>(

kotlinx-coroutines-core/js/test/TestBase.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public actual val isStressTest: Boolean = false
1010
public actual val stressTestMultiplier: Int = 1
1111

1212
public actual open class TestBase actual constructor() {
13-
public actual val isJs = true
13+
public actual val isBoundByJsTestTimeout = true
1414
private var actionIndex = 0
1515
private var finished = false
1616
private var error: Throwable? = null
@@ -72,7 +72,6 @@ public actual open class TestBase actual constructor() {
7272
finished = false
7373
}
7474

75-
// todo: The dynamic (promise) result is a work-around for missing suspend tests, see KT-22228
7675
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
7776
public actual fun runTest(
7877
expected: ((Throwable) -> Boolean)? = null,
@@ -81,6 +80,25 @@ public actual open class TestBase actual constructor() {
8180
): dynamic {
8281
var exCount = 0
8382
var ex: Throwable? = null
83+
/*
84+
* This is an additional sanity check against `runTest` mis-usage on JS.
85+
* The only way to write an async test on JS is to return Promise from the test function.
86+
* _Just_ launching promise and returning `Unit` won't suffice as the underlying test framework
87+
* won't be able to detect an asynchronous failure in a timely manner.
88+
* We cannot detect such situations, but we can detect the most common erroneous pattern
89+
* in our code base, an attempt to use multiple `runTest` in the same `@Test` method,
90+
* which typically is a premise to the same error:
91+
* ```
92+
* @Test
93+
* fun incorrectTestForJs() { // <- promise is not returned
94+
* for (parameter in parameters) {
95+
* runTest {
96+
* runTestForParameter(parameter)
97+
* }
98+
* }
99+
* }
100+
* ```
101+
*/
84102
if (lastTestPromise != null) {
85103
error("Attempt to run multiple asynchronous test within one @Test method")
86104
}

kotlinx-coroutines-core/jvm/test/TestBase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public val stressTestMultiplierCbrt = cbrt(stressTestMultiplier.toDouble()).roun
5050
* ```
5151
*/
5252
public actual open class TestBase actual constructor() {
53-
public actual val isJs = false
53+
public actual val isBoundByJsTestTimeout = false
5454
private var actionIndex = AtomicInteger()
5555
private var finished = AtomicBoolean()
5656
private var error = AtomicReference<Throwable>()

kotlinx-coroutines-core/native/test/TestBase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public actual val isStressTest: Boolean = false
88
public actual val stressTestMultiplier: Int = 1
99

1010
public actual open class TestBase actual constructor() {
11-
public actual val isJs = false
11+
public actual val isBoundByJsTestTimeout = false
1212
private var actionIndex = 0
1313
private var finished = false
1414
private var error: Throwable? = null

0 commit comments

Comments
 (0)