Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental |
| 2 | |
| 3 | import org.junit.Test |
| 4 | import java.io.IOException |
Roman Elizarov | b6b0125 | 2017-02-06 13:17:40 +0300 | [diff] [blame^] | 5 | import kotlin.test.assertTrue |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 6 | |
| 7 | class DeferTest : TestBase() { |
| 8 | @Test |
| 9 | fun testSimple(): Unit = runBlocking { |
| 10 | expect(1) |
| 11 | val d = defer(context) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 12 | expect(3) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 13 | 42 |
| 14 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 15 | expect(2) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 16 | check(d.isActive) |
| 17 | check(d.await() == 42) |
| 18 | check(!d.isActive) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 19 | expect(4) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 20 | check(d.await() == 42) // second await -- same result |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 21 | finish(5) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | @Test(expected = IOException::class) |
| 25 | fun testSimpleException(): Unit = runBlocking { |
| 26 | expect(1) |
| 27 | val d = defer(context) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 28 | finish(3) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 29 | throw IOException() |
| 30 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 31 | expect(2) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 32 | d.await() // will throw IOException |
| 33 | } |
| 34 | |
| 35 | @Test(expected = IOException::class) |
| 36 | fun testDeferAndYieldException(): Unit = runBlocking { |
| 37 | expect(1) |
| 38 | val d = defer(context) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 39 | expect(3) |
| 40 | yield() // no effect, parent waiting |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 41 | finish(4) |
| 42 | throw IOException() |
| 43 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 44 | expect(2) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 45 | d.await() // will throw IOException |
| 46 | } |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 47 | |
| 48 | @Test |
| 49 | fun testDeferWithTwoWaiters() = runBlocking { |
| 50 | expect(1) |
| 51 | val d = defer(context) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 52 | expect(5) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 53 | yield() |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 54 | expect(9) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 55 | 42 |
| 56 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 57 | expect(2) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 58 | launch(context) { |
| 59 | expect(6) |
| 60 | check(d.await() == 42) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 61 | expect(11) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 62 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 63 | expect(3) |
| 64 | launch(context) { |
| 65 | expect(7) |
| 66 | check(d.await() == 42) |
| 67 | expect(12) |
| 68 | } |
| 69 | expect(4) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 70 | yield() // this actually yields control to defer, which produces results and resumes both waiters (in order) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 71 | expect(8) |
| 72 | yield() // yield again to "d", which completes |
| 73 | expect(10) |
| 74 | yield() // yield to both waiters |
| 75 | finish(13) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 76 | } |
Roman Elizarov | b6b0125 | 2017-02-06 13:17:40 +0300 | [diff] [blame^] | 77 | |
| 78 | class BadClass { |
| 79 | override fun equals(other: Any?): Boolean = error("equals") |
| 80 | override fun hashCode(): Int = error("hashCode") |
| 81 | override fun toString(): String = error("toString") |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | fun testDeferBadClass() = runBlocking { |
| 86 | val bad = BadClass() |
| 87 | val d = defer(context) { |
| 88 | expect(1) |
| 89 | bad |
| 90 | } |
| 91 | assertTrue(d.await() === bad) |
| 92 | finish(2) |
| 93 | } |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 94 | } |