Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhalanskyjb committed Oct 15, 2021
1 parent 1e66c00 commit 608783e
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 15 deletions.
33 changes: 33 additions & 0 deletions kotlinx-coroutines-test/common/test/RunTestTest.kt
Expand Up @@ -120,4 +120,37 @@ class RunTestTest {
}
}

/** Tests that passing invalid contexts to [runTest] causes it to fail (on JS, without forking). */
@Test
fun testRunTestWithIllegalContext() {
for (ctx in TestCoroutineScopeTest.invalidContexts) {
assertFailsWith<IllegalArgumentException> {
runTest(ctx) { }
}
}
}

/** Tests that throwing exceptions in [runTest] fails the test with them. */
@Test
fun testThrowingInRunTestBody() = testResultMap({
assertFailsWith<RuntimeException> { it() }
}) {
runTest {
throw RuntimeException()
}
}

/** Tests that throwing exceptions in pending tasks [runTest] fails the test with them. */
@Test
fun testThrowingInRunTestPendingTask() = testResultMap({
assertFailsWith<RuntimeException> { it() }
}) {
runTest {
launch {
delay(SLOW)
throw RuntimeException()
}
}
}

}
84 changes: 79 additions & 5 deletions kotlinx-coroutines-test/common/test/TestCoroutineSchedulerTest.kt
Expand Up @@ -5,13 +5,12 @@
package kotlinx.coroutines.test

import kotlinx.coroutines.*
import kotlin.coroutines.*
import kotlin.test.*

class TestCoroutineSchedulerTest {
/** Tests that `TestCoroutineScheduler` attempts to detect if there are several instances of it. */
@Test
fun testContextElement() = runBlockingTest {
fun testContextElement() = runTest {
assertFailsWith<IllegalStateException> {
withContext(TestCoroutineDispatcher()) {
}
Expand All @@ -21,7 +20,7 @@ class TestCoroutineSchedulerTest {
/** Tests that, as opposed to [DelayController.advanceTimeBy] or [TestCoroutineScope.advanceTimeBy],
* [TestCoroutineScheduler.advanceTimeBy] doesn't run the tasks scheduled at the target moment. */
@Test
fun testAdvanceTimeByDoesNotRunCurrent() = runBlockingTest {
fun testAdvanceTimeByDoesNotRunCurrent() = runTest {
var entered = false
launch {
delay(15)
Expand All @@ -45,7 +44,7 @@ class TestCoroutineSchedulerTest {
/** Tests that if [TestCoroutineScheduler.advanceTimeBy] encounters an arithmetic overflow, all the tasks scheduled
* until the moment [Long.MAX_VALUE] get run. */
@Test
fun testAdvanceTimeByEnormousDelays() = runBlockingTest {
fun testAdvanceTimeByEnormousDelays() = runTest {
val initialDelay = 10L
delay(initialDelay)
assertEquals(initialDelay, currentTime)
Expand Down Expand Up @@ -99,7 +98,7 @@ class TestCoroutineSchedulerTest {

/** Tests the basic functionality of [TestCoroutineScheduler.runCurrent]. */
@Test
fun testRunCurrent() = runBlockingTest {
fun testRunCurrent() = runTest {
var stage = 0
launch {
delay(1)
Expand Down Expand Up @@ -182,4 +181,79 @@ class TestCoroutineSchedulerTest {
stage = 1
scope.runCurrent()
}

private fun TestCoroutineScope.checkTimeout(
timesOut: Boolean, timeoutMillis: Long = SLOW, block: suspend () -> Unit
) = assertRunsFast {
var caughtException = false
launch {
try {
withTimeout(timeoutMillis) {
block()
}
} catch (e: TimeoutCancellationException) {
caughtException = true
}
}
advanceUntilIdle()
cleanupTestCoroutines()
if (timesOut)
assertTrue(caughtException)
else
assertFalse(caughtException)
}

/** Tests that timeouts get triggered. */
@Test
fun testSmallTimeouts() {
val scope = TestCoroutineScope()
scope.checkTimeout(true) {
val half = SLOW / 2
delay(half)
delay(SLOW - half)
}
}

/** Tests that timeouts don't get triggered if the code finishes in time. */
@Test
fun testLargeTimeouts() {
val scope = TestCoroutineScope()
scope.checkTimeout(false) {
val half = SLOW / 2
delay(half)
delay(SLOW - half - 1)
}
}

/** Tests that timeouts get triggered if the code fails to finish in time asynchronously. */
@Test
fun testSmallAsynchronousTimeouts() {
val scope = TestCoroutineScope()
val deferred = CompletableDeferred<Unit>()
scope.launch {
val half = SLOW / 2
delay(half)
delay(SLOW - half)
deferred.complete(Unit)
}
scope.checkTimeout(true) {
deferred.await()
}
}

/** Tests that timeouts don't get triggered if the code finishes in time, even if it does so asynchronously. */
@Test
fun testLargeAsynchronousTimeouts() {
val scope = TestCoroutineScope()
val deferred = CompletableDeferred<Unit>()
scope.launch {
val half = SLOW / 2
delay(half)
delay(SLOW - half - 1)
deferred.complete(Unit)
}
scope.checkTimeout(false) {
deferred.await()
}
}
}
12 changes: 7 additions & 5 deletions kotlinx-coroutines-test/common/test/TestCoroutineScopeTest.kt
Expand Up @@ -120,9 +120,11 @@ class TestCoroutineScopeTest {
assertFalse(handlerCalled)
}

private val invalidContexts = listOf(
Dispatchers.Default, // not a [TestDispatcher]
TestCoroutineDispatcher() + TestCoroutineScheduler(), // the dispatcher is not linked to the scheduler
CoroutineExceptionHandler { _, _ -> }, // not an `UncaughtExceptionCaptor`
)
companion object {
internal val invalidContexts = listOf(
Dispatchers.Default, // not a [TestDispatcher]
TestCoroutineDispatcher() + TestCoroutineScheduler(), // the dispatcher is not linked to the scheduler
CoroutineExceptionHandler { _, _ -> }, // not an `UncaughtExceptionCaptor`
)
}
}
2 changes: 1 addition & 1 deletion kotlinx-coroutines-test/common/test/TestDispatchersTest.kt
Expand Up @@ -33,7 +33,7 @@ class TestDispatchersTest {
}

@Test
fun testImmediateDispatcher() = runBlockingTest {
fun testImmediateDispatcher() = runTest {
Dispatchers.setMain(ImmediateDispatcher())
expect(1)
withContext(Dispatchers.Main) {
Expand Down
Expand Up @@ -3,8 +3,6 @@
*/
package kotlinx.coroutines.test

import kotlinx.coroutines.*

actual fun testResultMap(block: (() -> Unit) -> Unit, test: () -> TestResult) {
block {
test()
Expand Down
2 changes: 0 additions & 2 deletions kotlinx-coroutines-test/native/test/Helpers.kt
Expand Up @@ -3,8 +3,6 @@
*/
package kotlinx.coroutines.test

import kotlinx.coroutines.*

actual fun testResultMap(block: (() -> Unit) -> Unit, test: () -> TestResult) {
block {
test()
Expand Down

0 comments on commit 608783e

Please sign in to comment.