Denis Zharkov | b8fc343 | 2016-07-07 10:57:33 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 2 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 3 | import kotlinx.coroutines.experimental.CoroutineDispatcher |
| 4 | import kotlinx.coroutines.experimental.future.await |
| 5 | import kotlinx.coroutines.experimental.future.future |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 6 | import org.junit.Test |
| 7 | import java.util.concurrent.CompletableFuture |
| 8 | import java.util.concurrent.ExecutionException |
| 9 | import java.util.concurrent.atomic.AtomicInteger |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 10 | import kotlin.coroutines.CoroutineContext |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 11 | import kotlin.test.assertEquals |
| 12 | import kotlin.test.assertFalse |
| 13 | import kotlin.test.assertTrue |
| 14 | import kotlin.test.fail |
| 15 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 16 | class FutureTest { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 17 | @Test |
| 18 | fun testSimple() { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 19 | val future = future { |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 20 | CompletableFuture.supplyAsync { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 21 | "O" |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 22 | }.await() + "K" |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | assertEquals("OK", future.get()) |
| 26 | } |
| 27 | |
| 28 | @Test |
| 29 | fun testWaitForCompletion() { |
| 30 | val toAwait = CompletableFuture<String>() |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 31 | val future = future { |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 32 | toAwait.await() + "K" |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | assertFalse(future.isDone) |
| 36 | toAwait.complete("O") |
| 37 | |
| 38 | assertEquals("OK", future.get()) |
| 39 | } |
| 40 | |
| 41 | @Test |
Roman Elizarov | ee89344 | 2017-01-19 14:56:21 +0300 | [diff] [blame] | 42 | fun testDoneFutureCompletedExceptionally() { |
| 43 | val toAwait = CompletableFuture<String>() |
| 44 | toAwait.completeExceptionally(RuntimeException("O")) |
| 45 | val future = future<String> { |
| 46 | try { |
| 47 | toAwait.await() |
| 48 | } catch (e: RuntimeException) { |
| 49 | e.message!! |
| 50 | } + "K" |
| 51 | } |
| 52 | |
| 53 | assertFalse(future.isDone) |
| 54 | assertEquals("OK", future.get()) |
| 55 | } |
| 56 | |
| 57 | @Test |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 58 | fun testAwaitedFutureCompletedExceptionally() { |
| 59 | val toAwait = CompletableFuture<String>() |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 60 | val future = future<String> { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 61 | try { |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 62 | toAwait.await() |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 63 | } catch (e: RuntimeException) { |
| 64 | e.message!! |
| 65 | } + "K" |
| 66 | } |
| 67 | |
| 68 | assertFalse(future.isDone) |
| 69 | toAwait.completeExceptionally(RuntimeException("O")) |
| 70 | |
| 71 | assertEquals("OK", future.get()) |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | fun testExceptionInsideCoroutine() { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 76 | val future = future { |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 77 | if (CompletableFuture.supplyAsync { true }.await()) { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 78 | throw IllegalStateException("OK") |
| 79 | } |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 80 | CompletableFuture.supplyAsync { "fail" }.await() |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | try { |
| 84 | future.get() |
| 85 | fail("'get' should've throw an exception") |
| 86 | } catch (e: ExecutionException) { |
| 87 | assertTrue(e.cause is IllegalStateException) |
| 88 | assertEquals("OK", e.cause!!.message) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | fun testContinuationWrapped() { |
| 94 | val depth = AtomicInteger() |
| 95 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 96 | val future = future(wrapContinuation { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 97 | depth.andIncrement |
| 98 | it() |
| 99 | depth.andDecrement |
| 100 | }) { |
Denis Zharkov | 6304ef7 | 2016-11-29 18:06:42 +0300 | [diff] [blame] | 101 | assertEquals(1, depth.get(), "Part before first suspension must be wrapped") |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 102 | |
| 103 | val result = |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 104 | CompletableFuture.supplyAsync { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 105 | while (depth.get() > 0); |
| 106 | |
| 107 | assertEquals(0, depth.get(), "Part inside suspension point should not be wrapped") |
| 108 | "OK" |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 109 | }.await() |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 110 | |
| 111 | assertEquals(1, depth.get(), "Part after first suspension should be wrapped") |
| 112 | |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 113 | CompletableFuture.supplyAsync { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 114 | while (depth.get() > 0); |
| 115 | |
| 116 | assertEquals(0, depth.get(), "Part inside suspension point should not be wrapped") |
| 117 | "ignored" |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 118 | }.await() |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 119 | |
| 120 | result |
| 121 | } |
| 122 | |
| 123 | assertEquals("OK", future.get()) |
| 124 | } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 125 | |
| 126 | private fun wrapContinuation(wrapper: (() -> Unit) -> Unit): CoroutineDispatcher = object : CoroutineDispatcher() { |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 127 | override fun isDispatchNeeded(context: CoroutineContext): Boolean = true |
| 128 | override fun dispatch(context: CoroutineContext, block: Runnable) { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 129 | wrapper { |
| 130 | block.run() |
| 131 | } |
| 132 | } |
| 133 | } |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 134 | } |