Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental |
| 6 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 7 | import kotlin.coroutines.experimental.* |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 8 | import kotlin.test.* |
| 9 | |
| 10 | class AbstractCoroutineTest : TestBase() { |
| 11 | @Test |
| 12 | fun testNotifications() = runTest { |
| 13 | expect(1) |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 14 | val coroutineContext = coroutineContext // workaround for KT-22984 |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 15 | val coroutine = object : AbstractCoroutine<String>(coroutineContext, false) { |
| 16 | override fun onStart() { |
| 17 | expect(3) |
| 18 | } |
| 19 | |
| 20 | override fun onCancellation(cause: Throwable?) { |
| 21 | assertTrue(cause == null) |
| 22 | expect(5) |
| 23 | } |
| 24 | |
| 25 | override fun onCompleted(value: String) { |
| 26 | assertEquals("OK", value) |
| 27 | expect(6) |
| 28 | } |
| 29 | |
| 30 | override fun onCompletedExceptionally(exception: Throwable) { |
| 31 | expectUnreached() |
| 32 | } |
| 33 | } |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 34 | coroutine.invokeOnCompletion(onCancelling = true) { |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 35 | assertTrue(it == null) |
| 36 | expect(7) |
| 37 | } |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 38 | coroutine.invokeOnCompletion { |
| 39 | assertTrue(it == null) |
| 40 | expect(8) |
| 41 | } |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 42 | expect(2) |
| 43 | coroutine.start() |
| 44 | expect(4) |
| 45 | coroutine.resume("OK") |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 46 | finish(9) |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Test |
| 50 | fun testNotificationsWithException() = runTest { |
| 51 | expect(1) |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 52 | val coroutineContext = coroutineContext // workaround for KT-22984 |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 53 | val coroutine = object : AbstractCoroutine<String>(coroutineContext, false) { |
| 54 | override fun onStart() { |
| 55 | expect(3) |
| 56 | } |
| 57 | |
| 58 | override fun onCancellation(cause: Throwable?) { |
| 59 | assertTrue(cause is TestException0) |
| 60 | expect(5) |
| 61 | } |
| 62 | |
| 63 | override fun onCompleted(value: String) { |
| 64 | expectUnreached() |
| 65 | } |
| 66 | |
| 67 | override fun onCompletedExceptionally(exception: Throwable) { |
| 68 | assertTrue(exception is TestException1) |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 69 | expect(8) |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 70 | } |
| 71 | } |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 72 | coroutine.invokeOnCompletion(onCancelling = true) { |
| 73 | assertTrue(it is TestException0) |
| 74 | expect(6) |
| 75 | } |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 76 | coroutine.invokeOnCompletion { |
| 77 | assertTrue(it is TestException1) |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 78 | expect(9) |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 79 | } |
| 80 | expect(2) |
| 81 | coroutine.start() |
| 82 | expect(4) |
| 83 | coroutine.cancel(TestException0()) |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 84 | expect(7) |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 85 | coroutine.resumeWithException(TestException1()) |
Roman Elizarov | 9da9e35 | 2018-01-25 10:06:39 +0300 | [diff] [blame] | 86 | finish(10) |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private class TestException0 : Throwable() |
| 90 | private class TestException1 : Throwable() |
| 91 | } |