• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
<lambda>null22     fun testLaunch() = runTest(expected = ::mainException) {
23         launch(Dispatchers.Main) {}
24     }
25 
26     @Test
<lambda>null27     fun testLaunchLazy() = runTest(expected = ::mainException) {
28         val job = launch(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() }
29         job.join()
30     }
31 
32     @Test
<lambda>null33     fun testLaunchUndispatched() = runTest(expected = ::mainException) {
34         launch(Dispatchers.Main, start = CoroutineStart.UNDISPATCHED) {
35             yield()
36             fail()
37         }
38     }
39 
40     @Test
<lambda>null41     fun testAsync() = runTest(expected = ::mainException) {
42         async(Dispatchers.Main) {}
43     }
44 
45     @Test
testAsyncLazynull46     fun testAsyncLazy() = runTest(expected = ::mainException) {
47         val job = async(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() }
48         job.await()
49     }
50 
51     @Test
<lambda>null52     fun testWithContext() = runTest(expected = ::mainException) {
53         withContext(Dispatchers.Main) {
54             fail()
55         }
56     }
57 
58     @Test
<lambda>null59     fun testProduce() = runTest(expected = ::mainException) {
60         produce<Int>(Dispatchers.Main) { fail() }
61     }
62 
63     @Test
<lambda>null64     fun testActor() = runTest(expected = ::mainException) {
65         actor<Int>(Dispatchers.Main) { fail() }
66     }
67 
68     @Test
<lambda>null69     fun testActorLazy() = runTest(expected = ::mainException) {
70         val actor = actor<Int>(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() }
71         actor.send(1)
72     }
73 
mainExceptionnull74     private fun mainException(e: Throwable): Boolean {
75         return e is IllegalStateException && e.message?.contains("Module with the Main dispatcher is missing") ?: false
76     }
77 
78     @Test
<lambda>null79     fun testProduceNonChild() = runTest(expected = ::mainException) {
80         produce<Int>(Job() + Dispatchers.Main) { fail() }
81     }
82 
83     @Test
<lambda>null84     fun testAsyncNonChild() = runTest(expected = ::mainException) {
85         async<Int>(Job() + Dispatchers.Main) { fail() }
86     }
87 }
88