Skip to content

Commit d694912

Browse files
committed
Fix build
1 parent 3ba7c52 commit d694912

File tree

6 files changed

+120
-15
lines changed

6 files changed

+120
-15
lines changed

kotlinx-coroutines-test/common/test/RunTestTest.kt

+33
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,37 @@ class RunTestTest {
120120
}
121121
}
122122

123+
/** Tests that passing invalid contexts to [runTest] causes it to fail (on JS, without forking). */
124+
@Test
125+
fun testRunTestWithIllegalContext() {
126+
for (ctx in TestCoroutineScopeTest.invalidContexts) {
127+
assertFailsWith<IllegalArgumentException> {
128+
runTest(ctx) { }
129+
}
130+
}
131+
}
132+
133+
/** Tests that throwing exceptions in [runTest] fails the test with them. */
134+
@Test
135+
fun testThrowingInRunTestBody() = testResultMap({
136+
assertFailsWith<RuntimeException> { it() }
137+
}) {
138+
runTest {
139+
throw RuntimeException()
140+
}
141+
}
142+
143+
/** Tests that throwing exceptions in pending tasks [runTest] fails the test with them. */
144+
@Test
145+
fun testThrowingInRunTestPendingTask() = testResultMap({
146+
assertFailsWith<RuntimeException> { it() }
147+
}) {
148+
runTest {
149+
launch {
150+
delay(SLOW)
151+
throw RuntimeException()
152+
}
153+
}
154+
}
155+
123156
}

kotlinx-coroutines-test/common/test/TestCoroutineSchedulerTest.kt

+79-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
package kotlinx.coroutines.test
66

77
import kotlinx.coroutines.*
8-
import kotlin.coroutines.*
98
import kotlin.test.*
109

1110
class TestCoroutineSchedulerTest {
1211
/** Tests that `TestCoroutineScheduler` attempts to detect if there are several instances of it. */
1312
@Test
14-
fun testContextElement() = runBlockingTest {
13+
fun testContextElement() = runTest {
1514
assertFailsWith<IllegalStateException> {
1615
withContext(TestCoroutineDispatcher()) {
1716
}
@@ -21,7 +20,7 @@ class TestCoroutineSchedulerTest {
2120
/** Tests that, as opposed to [DelayController.advanceTimeBy] or [TestCoroutineScope.advanceTimeBy],
2221
* [TestCoroutineScheduler.advanceTimeBy] doesn't run the tasks scheduled at the target moment. */
2322
@Test
24-
fun testAdvanceTimeByDoesNotRunCurrent() = runBlockingTest {
23+
fun testAdvanceTimeByDoesNotRunCurrent() = runTest {
2524
var entered = false
2625
launch {
2726
delay(15)
@@ -45,7 +44,7 @@ class TestCoroutineSchedulerTest {
4544
/** Tests that if [TestCoroutineScheduler.advanceTimeBy] encounters an arithmetic overflow, all the tasks scheduled
4645
* until the moment [Long.MAX_VALUE] get run. */
4746
@Test
48-
fun testAdvanceTimeByEnormousDelays() = runBlockingTest {
47+
fun testAdvanceTimeByEnormousDelays() = runTest {
4948
val initialDelay = 10L
5049
delay(initialDelay)
5150
assertEquals(initialDelay, currentTime)
@@ -99,7 +98,7 @@ class TestCoroutineSchedulerTest {
9998

10099
/** Tests the basic functionality of [TestCoroutineScheduler.runCurrent]. */
101100
@Test
102-
fun testRunCurrent() = runBlockingTest {
101+
fun testRunCurrent() = runTest {
103102
var stage = 0
104103
launch {
105104
delay(1)
@@ -182,4 +181,79 @@ class TestCoroutineSchedulerTest {
182181
stage = 1
183182
scope.runCurrent()
184183
}
184+
185+
private fun TestCoroutineScope.checkTimeout(
186+
timesOut: Boolean, timeoutMillis: Long = SLOW, block: suspend () -> Unit
187+
) = assertRunsFast {
188+
var caughtException = false
189+
launch {
190+
try {
191+
withTimeout(timeoutMillis) {
192+
block()
193+
}
194+
} catch (e: TimeoutCancellationException) {
195+
caughtException = true
196+
}
197+
}
198+
advanceUntilIdle()
199+
cleanupTestCoroutines()
200+
if (timesOut)
201+
assertTrue(caughtException)
202+
else
203+
assertFalse(caughtException)
204+
}
205+
206+
/** Tests that timeouts get triggered. */
207+
@Test
208+
fun testSmallTimeouts() {
209+
val scope = TestCoroutineScope()
210+
scope.checkTimeout(true) {
211+
val half = SLOW / 2
212+
delay(half)
213+
delay(SLOW - half)
214+
}
215+
}
216+
217+
/** Tests that timeouts don't get triggered if the code finishes in time. */
218+
@Test
219+
fun testLargeTimeouts() {
220+
val scope = TestCoroutineScope()
221+
scope.checkTimeout(false) {
222+
val half = SLOW / 2
223+
delay(half)
224+
delay(SLOW - half - 1)
225+
}
226+
}
227+
228+
/** Tests that timeouts get triggered if the code fails to finish in time asynchronously. */
229+
@Test
230+
fun testSmallAsynchronousTimeouts() {
231+
val scope = TestCoroutineScope()
232+
val deferred = CompletableDeferred<Unit>()
233+
scope.launch {
234+
val half = SLOW / 2
235+
delay(half)
236+
delay(SLOW - half)
237+
deferred.complete(Unit)
238+
}
239+
scope.checkTimeout(true) {
240+
deferred.await()
241+
}
242+
}
243+
244+
/** Tests that timeouts don't get triggered if the code finishes in time, even if it does so asynchronously. */
245+
@Test
246+
fun testLargeAsynchronousTimeouts() {
247+
val scope = TestCoroutineScope()
248+
val deferred = CompletableDeferred<Unit>()
249+
scope.launch {
250+
val half = SLOW / 2
251+
delay(half)
252+
delay(SLOW - half - 1)
253+
deferred.complete(Unit)
254+
}
255+
scope.checkTimeout(false) {
256+
deferred.await()
257+
}
258+
}
185259
}

kotlinx-coroutines-test/common/test/TestCoroutineScopeTest.kt

+7-5
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ class TestCoroutineScopeTest {
120120
assertFalse(handlerCalled)
121121
}
122122

123-
private val invalidContexts = listOf(
124-
Dispatchers.Default, // not a [TestDispatcher]
125-
TestCoroutineDispatcher() + TestCoroutineScheduler(), // the dispatcher is not linked to the scheduler
126-
CoroutineExceptionHandler { _, _ -> }, // not an `UncaughtExceptionCaptor`
127-
)
123+
companion object {
124+
internal val invalidContexts = listOf(
125+
Dispatchers.Default, // not a [TestDispatcher]
126+
TestCoroutineDispatcher() + TestCoroutineScheduler(), // the dispatcher is not linked to the scheduler
127+
CoroutineExceptionHandler { _, _ -> }, // not an `UncaughtExceptionCaptor`
128+
)
129+
}
128130
}

kotlinx-coroutines-test/common/test/TestDispatchersTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class TestDispatchersTest {
3333
}
3434

3535
@Test
36-
fun testImmediateDispatcher() = runBlockingTest {
36+
fun testImmediateDispatcher() = runTest {
3737
Dispatchers.setMain(ImmediateDispatcher())
3838
expect(1)
3939
withContext(Dispatchers.Main) {

kotlinx-coroutines-test/jvm/test/Helpers.kt renamed to kotlinx-coroutines-test/jvm/test/HelpersJvm.kt

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
*/
44
package kotlinx.coroutines.test
55

6-
import kotlinx.coroutines.*
7-
86
actual fun testResultMap(block: (() -> Unit) -> Unit, test: () -> TestResult) {
97
block {
108
test()

kotlinx-coroutines-test/native/test/Helpers.kt

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
*/
44
package kotlinx.coroutines.test
55

6-
import kotlinx.coroutines.*
7-
86
actual fun testResultMap(block: (() -> Unit) -> Unit, test: () -> TestResult) {
97
block {
108
test()

0 commit comments

Comments
 (0)