1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913
package kotlinx.coroutines
import kotlin.test.*
class AsyncLazyTest : TestBase() {
@Test
fun testSimple() = runTest {
expect(1)
val d = async(start = CoroutineStart.LAZY) {
expect(3)
42
}
expect(2)
assertTrue(!d.isActive && !d.isCompleted)
assertEquals(d.await(), 42)
assertTrue(!d.isActive && d.isCompleted && !d.isCancelled)
expect(4)
assertEquals(d.await(), 42) // second await -- same result
finish(5)
}
@Test
fun testLazyDeferAndYield() = runTest {
expect(1)
val d = async(start = CoroutineStart.LAZY) {
expect(3)
yield() // this has not effect, because parent coroutine is waiting
expect(4)
42
}
expect(2)
assertTrue(!d.isActive && !d.isCompleted)
assertEquals(d.await(), 42)
assertTrue(!d.isActive && d.isCompleted && !d.isCancelled)
expect(5)
assertEquals(d.await(), 42) // second await -- same result
finish(6)
}
@Test
fun testLazyDeferAndYield2() = runTest {
expect(1)
val d = async(start = CoroutineStart.LAZY) {
expect(7)
42
}
expect(2)
assertTrue(!d.isActive && !d.isCompleted)
launch { // see how it looks from another coroutine
expect(4)
assertTrue(!d.isActive && !d.isCompleted)
yield() // yield back to main
expect(6)
assertTrue(d.isActive && !d.isCompleted) // implicitly started by main's await
yield() // yield to d
}
expect(3)
assertTrue(!d.isActive && !d.isCompleted)
yield() // yield to second child (lazy async is not computing yet)
expect(5)
assertTrue(!d.isActive && !d.isCompleted)
assertEquals(d.await(), 42) // starts computing
assertTrue(!d.isActive && d.isCompleted && !d.isCancelled)
finish(8)
}
@Test
fun testSimpleException() = runTest(
expected = { it is TestException }
) {
expect(1)
val d = async(start = CoroutineStart.LAZY) {
finish(3)
throw TestException()
}
expect(2)
assertTrue(!d.isActive && !d.isCompleted)
d.await() // will throw IOException
}
@Test
fun testLazyDeferAndYieldException() = runTest(
expected = { it is TestException }
) {
expect(1)
val d = async(start = CoroutineStart.LAZY) {
expect(3)
yield() // this has not effect, because parent coroutine is waiting
finish(4)
throw TestException()
}
expect(2)
assertTrue(!d.isActive && !d.isCompleted)
d.await() // will throw IOException
}
@Test
fun testCatchException() = runTest {
expect(1)
val d = async(NonCancellable, start = CoroutineStart.LAZY) {
expect(3)
throw TestException()
}
expect(2)
assertTrue(!d.isActive && !d.isCompleted)
try {
d.await() // will throw IOException
} catch (e: TestException) {
assertTrue(!d.isActive && d.isCompleted && d.isCancelled)
expect(4)
}
finish(5)
}
@Test
fun testStart() = runTest {
expect(1)
val d = async(start = CoroutineStart.LAZY) {
expect(4)
42
}
expect(2)
assertTrue(!d.isActive && !d.isCompleted)
assertTrue(d.start())
assertTrue(d.isActive && !d.isCompleted)
expect(3)
assertTrue(!d.start())
yield() // yield to started coroutine
assertTrue(!d.isActive && d.isCompleted && !d.isCancelled) // and it finishes
expect(5)
assertEquals(d.await(), 42) // await sees result
finish(6)
}
@Test
fun testCancelBeforeStart() = runTest(
expected = { it is CancellationException }
) {
expect(1)
val d = async(start = CoroutineStart.LAZY) {
expectUnreached()
42
}
expect(2)
assertTrue(!d.isActive && !d.isCompleted)
d.cancel()
assertTrue(!d.isActive && d.isCompleted && d.isCancelled)
assertTrue(!d.start())
finish(3)
assertEquals(d.await(), 42) // await shall throw CancellationException
expectUnreached()
}
@Test
fun testCancelWhileComputing() = runTest(
expected = { it is CancellationException }
) {
expect(1)
val d = async(start = CoroutineStart.LAZY) {
expect(4)
yield() // yield to main, that is going to cancel us
expectUnreached()
42
}
expect(2)
assertTrue(!d.isActive && !d.isCompleted && !d.isCancelled)
assertTrue(d.start())
assertTrue(d.isActive && !d.isCompleted && !d.isCancelled)
expect(3)
yield() // yield to d
expect(5)
assertTrue(d.isActive && !d.isCompleted && !d.isCancelled)
d.cancel()
assertTrue(!d.isActive && d.isCancelled) // cancelling !
assertTrue(!d.isActive && d.isCancelled) // still cancelling
finish(6)
assertEquals(d.await(), 42) // await shall throw CancellationException
expectUnreached()
}
}
|