aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/jvm/test/FailFastOnStartTest.kt
diff options
context:
space:
mode:
Diffstat (limited to 'kotlinx-coroutines-core/jvm/test/FailFastOnStartTest.kt')
-rw-r--r--kotlinx-coroutines-core/jvm/test/FailFastOnStartTest.kt77
1 files changed, 77 insertions, 0 deletions
diff --git a/kotlinx-coroutines-core/jvm/test/FailFastOnStartTest.kt b/kotlinx-coroutines-core/jvm/test/FailFastOnStartTest.kt
new file mode 100644
index 00000000..15cb83ce
--- /dev/null
+++ b/kotlinx-coroutines-core/jvm/test/FailFastOnStartTest.kt
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ */
+
+@file:Suppress("DeferredResultUnused")
+
+package kotlinx.coroutines
+
+import kotlinx.coroutines.channels.*
+import org.junit.*
+import org.junit.Test
+import org.junit.rules.*
+import kotlin.test.*
+
+class FailFastOnStartTest : TestBase() {
+
+ @Rule
+ @JvmField
+ public val timeout: Timeout = Timeout.seconds(5)
+
+ @Test
+ fun testLaunch() = runTest(expected = ::mainException) {
+ launch(Dispatchers.Main) {}
+ }
+
+ @Test
+ fun testLaunchLazy() = runTest(expected = ::mainException) {
+ val job = launch(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() }
+ job.join()
+ }
+
+ @Test
+ fun testLaunchUndispatched() = runTest(expected = ::mainException) {
+ launch(Dispatchers.Main, start = CoroutineStart.UNDISPATCHED) {
+ yield()
+ fail()
+ }
+ }
+
+ @Test
+ fun testAsync() = runTest(expected = ::mainException) {
+ async(Dispatchers.Main) {}
+ }
+
+ @Test
+ fun testAsyncLazy() = runTest(expected = ::mainException) {
+ val job = async(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() }
+ job.await()
+ }
+
+ @Test
+ fun testWithContext() = runTest(expected = ::mainException) {
+ withContext(Dispatchers.Main) {
+ fail()
+ }
+ }
+
+ @Test
+ fun testProduce() = runTest(expected = ::mainException) {
+ produce<Int>(Dispatchers.Main) { fail() }
+ }
+
+ @Test
+ fun testActor() = runTest(expected = ::mainException) {
+ actor<Int>(Dispatchers.Main) { fail() }
+ }
+
+ @Test
+ fun testActorLazy() = runTest(expected = ::mainException) {
+ val actor = actor<Int>(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() }
+ actor.send(1)
+ }
+
+ private fun mainException(e: Throwable): Boolean {
+ return e is IllegalStateException && e.message?.contains("Module with the Main dispatcher is missing") ?: false
+ }
+}