Skip to content

Introduce TestResult into TestBase as the first preparation step to m… #2827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion kotlinx-coroutines-core/common/test/TestBase.common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import kotlin.test.*
public expect val isStressTest: Boolean
public expect val stressTestMultiplier: Int

/**
* The result of a multiplatform asynchronous test.
* Aliases into Unit on K/JVM and K/N, and into Promise on K/JS.
*/
@Suppress("NO_ACTUAL_FOR_EXPECT")
public expect class TestResult

public expect open class TestBase constructor() {
/*
* In common tests we emulate parameterized tests
Expand All @@ -33,7 +40,7 @@ public expect open class TestBase constructor() {
expected: ((Throwable) -> Boolean)? = null,
unhandled: List<(Throwable) -> Boolean> = emptyList(),
block: suspend CoroutineScope.() -> Unit
)
): TestResult
}

public suspend inline fun hang(onCancellation: () -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ abstract class FlatMapMergeBaseTest : FlatMapBaseTest() {
}

@Test
abstract fun testFlatMapConcurrency()
abstract fun testFlatMapConcurrency(): TestResult
}
14 changes: 13 additions & 1 deletion kotlinx-coroutines-core/js/test/PromiseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@ class PromiseTest : TestBase() {
assertSame(d2, deferred)
assertEquals("OK", d2.await())
}
}

@Test
fun testLeverageTestResult(): TestResult {
// Cannot use expect(..) here
var seq = 0
val result = runTest {
++seq
}
return result.then {
if (seq != 1) error("Unexpected result: $seq")
}
}
}
5 changes: 4 additions & 1 deletion kotlinx-coroutines-core/js/test/TestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import kotlin.js.*
public actual val isStressTest: Boolean = false
public actual val stressTestMultiplier: Int = 1

@Suppress("ACTUAL_WITHOUT_EXPECT", "ACTUAL_TYPE_ALIAS_TO_CLASS_WITH_DECLARATION_SITE_VARIANCE")
public actual typealias TestResult = Promise<Unit>

public actual open class TestBase actual constructor() {
public actual val isBoundByJsTestTimeout = true
private var actionIndex = 0
Expand Down Expand Up @@ -77,7 +80,7 @@ public actual open class TestBase actual constructor() {
expected: ((Throwable) -> Boolean)? = null,
unhandled: List<(Throwable) -> Boolean> = emptyList(),
block: suspend CoroutineScope.() -> Unit
): dynamic {
): TestResult {
var exCount = 0
var ex: Throwable? = null
/*
Expand Down
5 changes: 4 additions & 1 deletion kotlinx-coroutines-core/jvm/test/TestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public actual val stressTestMultiplier = stressTestMultiplierSqrt * stressTestMu

public val stressTestMultiplierCbrt = cbrt(stressTestMultiplier.toDouble()).roundToInt()

@Suppress("ACTUAL_WITHOUT_EXPECT")
public actual typealias TestResult = Unit

/**
* Base class for tests, so that tests for predictable scheduling of actions in multiple coroutines sharing a single
* thread can be written. Use it like this:
Expand Down Expand Up @@ -188,7 +191,7 @@ public actual open class TestBase actual constructor() {
expected: ((Throwable) -> Boolean)? = null,
unhandled: List<(Throwable) -> Boolean> = emptyList(),
block: suspend CoroutineScope.() -> Unit
) {
): TestResult {
var exCount = 0
var ex: Throwable? = null
try {
Expand Down
5 changes: 4 additions & 1 deletion kotlinx-coroutines-core/native/test/TestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package kotlinx.coroutines
public actual val isStressTest: Boolean = false
public actual val stressTestMultiplier: Int = 1

@Suppress("ACTUAL_WITHOUT_EXPECT")
public actual typealias TestResult = Unit

public actual open class TestBase actual constructor() {
public actual val isBoundByJsTestTimeout = false
private var actionIndex = 0
Expand Down Expand Up @@ -71,7 +74,7 @@ public actual open class TestBase actual constructor() {
expected: ((Throwable) -> Boolean)? = null,
unhandled: List<(Throwable) -> Boolean> = emptyList(),
block: suspend CoroutineScope.() -> Unit
) {
): TestResult {
var exCount = 0
var ex: Throwable? = null
try {
Expand Down