Skip to content

Commit b5af3ef

Browse files
committed
Do proper checkcast in takeEventLoop, throw ISE instead of TCE
Fixes #920
1 parent 4b60d01 commit b5af3ef

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

native/kotlinx-coroutines-core-native/src/CoroutineContext.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlin.coroutines.*
88
import kotlinx.coroutines.internal.*
99

1010
private fun takeEventLoop(): EventLoopImpl =
11-
ThreadLocalEventLoop.currentOrNull() as EventLoopImpl ?:
11+
ThreadLocalEventLoop.currentOrNull() as? EventLoopImpl ?:
1212
error("There is no event loop. Use runBlocking { ... } to start one.")
1313

1414
internal object DefaultExecutor : CoroutineDispatcher(), Delay {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines
6+
7+
import kotlin.coroutines.*
8+
import kotlin.test.Test
9+
import kotlin.test.assertTrue
10+
11+
class DelayExceptionTest {
12+
private object Dispatcher : CoroutineDispatcher() {
13+
override fun isDispatchNeeded(context: CoroutineContext): Boolean = true
14+
override fun dispatch(context: CoroutineContext, block: Runnable) { block.run() }
15+
}
16+
17+
private lateinit var exception: Throwable
18+
19+
20+
@Test
21+
fun testThrowsTce() {
22+
CoroutineScope(Dispatcher + CoroutineExceptionHandler { _, e -> exception = e }).launch {
23+
delay(10)
24+
}
25+
26+
assertTrue(exception is IllegalStateException)
27+
}
28+
}

0 commit comments

Comments
 (0)