Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental |
| 2 | |
| 3 | import kotlin.test.* |
| 4 | |
| 5 | class CancellableContinuationHandlersTest : TestBase() { |
| 6 | |
| 7 | @Test |
| 8 | fun testDoubleSubscription() = runTest({ it is IllegalStateException }) { |
| 9 | suspendCancellableCoroutine<Unit> { c -> |
| 10 | c.invokeOnCancellation { finish(1) } |
| 11 | c.invokeOnCancellation { expectUnreached() } |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | @Test |
| 16 | fun testDoubleSubscriptionAfterCompletion() = runTest { |
| 17 | suspendCancellableCoroutine<Unit> { c -> |
| 18 | c.resume(Unit) |
| 19 | // Nothing happened |
| 20 | c.invokeOnCancellation { expectUnreached() } |
| 21 | c.invokeOnCancellation { expectUnreached() } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | @Test |
| 26 | fun testDoubleSubscriptionAfterCancellation() = runTest { |
| 27 | try { |
| 28 | suspendCancellableCoroutine<Unit> { c -> |
| 29 | c.cancel() |
| 30 | c.invokeOnCancellation { |
Roman Elizarov | 6d9f40f | 2018-04-28 14:44:02 +0300 | [diff] [blame] | 31 | assertTrue(it is CancellationException) |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 32 | expect(1) |
| 33 | } |
| 34 | c.invokeOnCancellation { |
Roman Elizarov | 6d9f40f | 2018-04-28 14:44:02 +0300 | [diff] [blame] | 35 | assertTrue(it is CancellationException) |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 36 | expect(2) |
| 37 | } |
| 38 | } |
| 39 | } catch (e: CancellationException) { |
| 40 | finish(3) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | @Test |
| 45 | fun testDoubleSubscriptionAfterCancellationWithCause() = runTest { |
| 46 | try { |
| 47 | suspendCancellableCoroutine<Unit> { c -> |
| 48 | c.cancel(AssertionError()) |
| 49 | c.invokeOnCancellation { |
| 50 | require(it is AssertionError) |
| 51 | expect(1) |
| 52 | } |
| 53 | c.invokeOnCancellation { |
| 54 | require(it is AssertionError) |
| 55 | expect(2) |
| 56 | } |
| 57 | } |
| 58 | } catch (e: AssertionError) { |
| 59 | finish(3) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | fun testDoubleSubscriptionMixed() = runTest { |
| 65 | try { |
| 66 | suspendCancellableCoroutine<Unit> { c -> |
| 67 | c.invokeOnCancellation { |
| 68 | require(it is IndexOutOfBoundsException) |
| 69 | expect(1) |
| 70 | } |
| 71 | |
| 72 | c.cancel(IndexOutOfBoundsException()) |
| 73 | c.invokeOnCancellation { |
| 74 | require(it is IndexOutOfBoundsException) |
| 75 | expect(2) |
| 76 | } |
| 77 | } |
| 78 | } catch (e: IndexOutOfBoundsException) { |
| 79 | finish(3) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | fun testExceptionInHandler() = runTest({it is CancellationException}, listOf({e -> e is CompletionHandlerException})) { |
| 85 | suspendCancellableCoroutine<Unit> { c -> |
| 86 | c.invokeOnCancellation { throw AssertionError() } |
| 87 | c.cancel() |
| 88 | } |
| 89 | } |
| 90 | } |