|
| 1 | +/* |
| 2 | + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +@file:Suppress("DeferredResultUnused") |
| 6 | + |
| 7 | +package kotlinx.coroutines |
| 8 | + |
| 9 | +import kotlinx.coroutines.channels.* |
| 10 | +import org.junit.* |
| 11 | +import org.junit.Test |
| 12 | +import org.junit.rules.* |
| 13 | +import kotlin.test.* |
| 14 | + |
| 15 | +class FailFastOnStartTest : TestBase() { |
| 16 | + |
| 17 | + @Rule |
| 18 | + @JvmField |
| 19 | + public val timeout: Timeout = Timeout.seconds(5) |
| 20 | + |
| 21 | + @Test |
| 22 | + fun testLaunch() = runTest(expected = ::mainException) { |
| 23 | + launch(Dispatchers.Main) {} |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + fun testLaunchLazy() = runTest(expected = ::mainException) { |
| 28 | + val job = launch(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() } |
| 29 | + job.join() |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun testLaunchUndispatched() = runTest(expected = ::mainException) { |
| 34 | + launch(Dispatchers.Main, start = CoroutineStart.UNDISPATCHED) { |
| 35 | + yield() |
| 36 | + fail() |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun testAsync() = runTest(expected = ::mainException) { |
| 42 | + async(Dispatchers.Main) {} |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun testAsyncLazy() = runTest(expected = ::mainException) { |
| 47 | + val job = async(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() } |
| 48 | + job.await() |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun testWithContext() = runTest(expected = ::mainException) { |
| 53 | + withContext(Dispatchers.Main) { |
| 54 | + fail() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun testProduce() = runTest(expected = ::mainException) { |
| 60 | + produce<Int>(Dispatchers.Main) { fail() } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun testActor() = runTest(expected = ::mainException) { |
| 65 | + actor<Int>(Dispatchers.Main) { fail() } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun testActorLazy() = runTest(expected = ::mainException) { |
| 70 | + val actor = actor<Int>(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() } |
| 71 | + actor.send(1) |
| 72 | + } |
| 73 | + |
| 74 | + private fun mainException(e: Throwable): Boolean { |
| 75 | + return e is IllegalStateException && e.message?.contains("Module with the Main dispatcher is missing") ?: false |
| 76 | + } |
| 77 | +} |
0 commit comments