Skip to content

Commit e65e47c

Browse files
committed
Tests for defer
1 parent fbb36d3 commit e65e47c

File tree

1 file changed

+49
-0
lines changed
  • kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental

1 file changed

+49
-0
lines changed

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/CoroutinesTest.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,53 @@ class CoroutinesTest {
187187
}
188188
finish(5)
189189
}
190+
191+
@Test
192+
fun testDeferSimple(): Unit = runBlocking {
193+
expect(1)
194+
val d = defer(context) {
195+
expect(2)
196+
42
197+
}
198+
expect(3)
199+
check(d.await() == 42)
200+
finish(4)
201+
}
202+
203+
@Test
204+
fun testDeferAndYield(): Unit = runBlocking {
205+
expect(1)
206+
val d = defer(context) {
207+
expect(2)
208+
yield()
209+
expect(4)
210+
42
211+
}
212+
expect(3)
213+
check(d.await() == 42)
214+
finish(5)
215+
}
216+
217+
@Test(expected = IOException::class)
218+
fun testDeferSimpleException(): Unit = runBlocking {
219+
expect(1)
220+
val d = defer(context) {
221+
expect(2)
222+
throw IOException()
223+
}
224+
finish(3)
225+
d.await() // will throw IOException
226+
}
227+
228+
@Test(expected = IOException::class)
229+
fun testDeferAndYieldException(): Unit = runBlocking {
230+
expect(1)
231+
val d = defer(context) {
232+
expect(2)
233+
yield()
234+
throw IOException()
235+
}
236+
finish(3)
237+
d.await() // will throw IOException
238+
}
190239
}

0 commit comments

Comments
 (0)