blob: 124f3802afc856b5d0858aaa8f352fa106aa1dd3 [file] [log] [blame]
Vsevolod Tolstopyatovf3a50132018-04-16 19:41:20 +03001package kotlinx.coroutines.experimental
2
3import kotlin.test.*
4
5class 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 Elizarov6d9f40f2018-04-28 14:44:02 +030031 assertTrue(it is CancellationException)
Vsevolod Tolstopyatovf3a50132018-04-16 19:41:20 +030032 expect(1)
33 }
34 c.invokeOnCancellation {
Roman Elizarov6d9f40f2018-04-28 14:44:02 +030035 assertTrue(it is CancellationException)
Vsevolod Tolstopyatovf3a50132018-04-16 19:41:20 +030036 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}