Skip to content

Commit 43cecf5

Browse files
committed
Improve platform-specific tests in the test module
1 parent caacdc5 commit 43cecf5

File tree

7 files changed

+96
-20
lines changed

7 files changed

+96
-20
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ open class OrderedExecutionTestBase {
6666
}
6767

6868
internal fun <T> T.void() { }
69+
70+
@OptionalExpectation
71+
expect annotation class NoJs()
72+
73+
@OptionalExpectation
74+
expect annotation class NoNative()

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

+18-18
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class RunTestTest {
8484

8585
/** Tests that too low of a dispatch timeout causes crashes. */
8686
@Test
87-
@Ignore // TODO: timeout leads to `Cannot execute task because event loop was shut down` on Native
87+
@NoNative // TODO: timeout leads to `Cannot execute task because event loop was shut down` on Native
8888
fun testRunTestWithSmallTimeout() = testResultMap({ fn ->
8989
assertFailsWith<UncompletedCoroutinesError> { fn() }
9090
}) {
@@ -107,7 +107,7 @@ class RunTestTest {
107107

108108
/** Tests uncaught exceptions taking priority over dispatch timeout in error reports. */
109109
@Test
110-
@Ignore // TODO: timeout leads to `Cannot execute task because event loop was shut down` on Native
110+
@NoNative // TODO: timeout leads to `Cannot execute task because event loop was shut down` on Native
111111
fun testRunTestTimingOutAndThrowing() = testResultMap({ fn ->
112112
assertFailsWith<IllegalArgumentException> { fn() }
113113
}) {
@@ -174,12 +174,12 @@ class RunTestTest {
174174

175175
/** Tests that, once the test body has thrown, the child coroutines are cancelled. */
176176
@Test
177-
fun testChildrenCancellationOnTestBodyFailure() {
177+
fun testChildrenCancellationOnTestBodyFailure(): TestResult {
178178
var job: Job? = null
179-
testResultMap({
179+
return testResultMap({
180180
assertFailsWith<AssertionError> { it() }
181181
assertTrue(job!!.isCancelled)
182-
}, {
182+
}) {
183183
runTest {
184184
job = launch {
185185
while (true) {
@@ -188,34 +188,34 @@ class RunTestTest {
188188
}
189189
throw AssertionError()
190190
}
191-
})
191+
}
192192
}
193193

194194
/** Tests that [runTest] reports [TimeoutCancellationException]. */
195195
@Test
196196
fun testTimeout() = testResultMap({
197197
assertFailsWith<TimeoutCancellationException> { it() }
198-
}, {
198+
}) {
199199
runTest {
200200
withTimeout(50) {
201201
launch {
202202
delay(1000)
203203
}
204204
}
205205
}
206-
})
206+
}
207207

208208
/** Checks that [runTest] throws the root cause and not [JobCancellationException] when a child coroutine throws. */
209209
@Test
210210
fun testRunTestThrowsRootCause() = testResultMap({
211211
assertFailsWith<TestException> { it() }
212-
}, {
212+
}) {
213213
runTest {
214214
launch {
215215
throw TestException()
216216
}
217217
}
218-
})
218+
}
219219

220220
/** Tests that [runTest] completes its job. */
221221
@Test
@@ -224,13 +224,13 @@ class RunTestTest {
224224
return testResultMap({
225225
it()
226226
assertTrue(handlerCalled)
227-
}, {
227+
}) {
228228
runTest {
229229
coroutineContext.job.invokeOnCompletion {
230230
handlerCalled = true
231231
}
232232
}
233-
})
233+
}
234234
}
235235

236236
/** Tests that [runTest] doesn't complete the job that was passed to it as an argument. */
@@ -245,11 +245,11 @@ class RunTestTest {
245245
it()
246246
assertFalse(handlerCalled)
247247
assertEquals(0, job.children.filter { it.isActive }.count())
248-
}, {
248+
}) {
249249
runTest(job) {
250250
assertTrue(coroutineContext.job in job.children)
251251
}
252-
})
252+
}
253253
}
254254

255255
/** Tests that, when the test body fails, the reported exceptions are suppressed. */
@@ -267,14 +267,14 @@ class RunTestTest {
267267
assertEquals("y", suppressed[1].message)
268268
assertEquals("z", suppressed[2].message)
269269
}
270-
}, {
270+
}) {
271271
runTest {
272272
launch(SupervisorJob()) { throw TestException("x") }
273273
launch(SupervisorJob()) { throw TestException("y") }
274274
launch(SupervisorJob()) { throw TestException("z") }
275275
throw TestException("w")
276276
}
277-
})
277+
}
278278

279279
/** Tests that [TestCoroutineScope.runTest] does not inherit the exception handler and works. */
280280
@Test
@@ -287,10 +287,10 @@ class RunTestTest {
287287
} catch (e: TestException) {
288288
scope.cleanupTestCoroutines() // should not fail
289289
}
290-
}, {
290+
}) {
291291
scope.runTest {
292292
launch(SupervisorJob()) { throw TestException("x") }
293293
}
294-
})
294+
}
295295
}
296296
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class TestDispatchersTest: OrderedExecutionTestBase() {
2020
Dispatchers.resetMain()
2121
}
2222

23-
/** Tests that asynchronous execution of tests does not happen concurrently with [AfterTest] (in fact, it does). */
24-
@Ignore // fails on JS.
23+
/** Tests that asynchronous execution of tests does not happen concurrently with [AfterTest]. */
24+
@NoJs
2525
@Test
2626
fun testMainMocking() = runTest {
2727
val mainAtStart = mainTestDispatcher
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package kotlinx.coroutines.test
2+
3+
import kotlinx.coroutines.*
4+
import kotlinx.coroutines.test.internal.*
5+
import kotlin.test.*
6+
7+
/** The tests that we want to fail. They are here so that, when the issue is fixed, their failure indicates that
8+
* everything is better now. */
9+
class FailingTests {
10+
11+
private var tearDownEntered = false
12+
13+
@BeforeTest
14+
fun setUp() {
15+
Dispatchers.setMain(StandardTestDispatcher())
16+
}
17+
18+
@AfterTest
19+
fun tearDown() {
20+
Dispatchers.resetMain()
21+
tearDownEntered = true
22+
}
23+
24+
/** [TestDispatchersTest.testMainMocking]. */
25+
@Test
26+
fun testAfterTestIsConcurrent() = runTest {
27+
try {
28+
val mainAtStart = (Dispatchers.Main as? TestMainDispatcher)?.delegate as? TestDispatcher ?: return@runTest
29+
withContext(Dispatchers.Default) {
30+
// context switch
31+
}
32+
assertNotSame(mainAtStart, (Dispatchers.Main as TestMainDispatcher).delegate)
33+
} finally {
34+
assertTrue(tearDownEntered)
35+
}
36+
}
37+
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package kotlinx.coroutines.test
66

7+
import kotlin.test.*
8+
79
actual fun testResultMap(block: (() -> Unit) -> Unit, test: () -> TestResult): TestResult =
810
test().then(
911
{
@@ -14,3 +16,5 @@ actual fun testResultMap(block: (() -> Unit) -> Unit, test: () -> TestResult): T
1416
throw it
1517
}
1618
})
19+
20+
actual typealias NoJs = Ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.test
6+
7+
import kotlinx.coroutines.*
8+
import kotlin.test.*
9+
10+
/** The tests that we want to fail. They are here so that, when the issue is fixed, their failure indicates that
11+
* everything is better now. */
12+
class FailingTests {
13+
@Test
14+
fun testRunTestLoopShutdownOnTimeout() = testResultMap({ fn ->
15+
assertFailsWith<IllegalStateException> { fn() }
16+
}) {
17+
runTest(dispatchTimeoutMs = 1) {
18+
withContext(Dispatchers.Default) {
19+
delay(10000)
20+
}
21+
fail("shouldn't be reached")
22+
}
23+
}
24+
25+
}

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

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

6+
import kotlin.test.*
7+
68
actual fun testResultMap(block: (() -> Unit) -> Unit, test: () -> TestResult) {
79
block {
810
test()
911
}
1012
}
13+
14+
actual typealias NoNative = Ignore

0 commit comments

Comments
 (0)