blob: 884a492e840460bcd1a5b451b4e5afc743b764da [file] [log] [blame]
Roman Elizarov331750b2017-02-15 17:59:17 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov331750b2017-02-15 17:59:17 +03003 */
4
5package kotlinx.coroutines.experimental.rx1
6
Roman Elizarov9fe5f462018-02-21 19:05:52 +03007import kotlinx.coroutines.experimental.*
8import org.hamcrest.core.*
9import org.junit.*
10import org.junit.Assert.*
11import kotlin.coroutines.experimental.*
Roman Elizarov331750b2017-02-15 17:59:17 +030012
13class CompletableTest : TestBase() {
14 @Test
15 fun testBasicSuccess() = runBlocking<Unit> {
16 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030017 val completable = rxCompletable(coroutineContext) {
Roman Elizarov331750b2017-02-15 17:59:17 +030018 expect(4)
19 }
20 expect(2)
21 completable.subscribe {
22 expect(5)
23 }
24 expect(3)
25 yield() // to completable coroutine
26 finish(6)
27 }
28
29 @Test
30 fun testBasicFailure() = runBlocking<Unit> {
31 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030032 val completable = rxCompletable(coroutineContext) {
Roman Elizarov331750b2017-02-15 17:59:17 +030033 expect(4)
34 throw RuntimeException("OK")
35 }
36 expect(2)
37 completable.subscribe({
38 expectUnreached()
39 }, { error ->
40 expect(5)
41 assertThat(error, IsInstanceOf(RuntimeException::class.java))
42 assertThat(error.message, IsEqual("OK"))
43 })
44 expect(3)
45 yield() // to completable coroutine
46 finish(6)
47 }
48
49 @Test
50 fun testBasicUnsubscribe() = runBlocking<Unit> {
51 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030052 val completable = rxCompletable(coroutineContext) {
Roman Elizarov331750b2017-02-15 17:59:17 +030053 expect(4)
54 yield() // back to main, will get cancelled
55 expectUnreached()
56 }
57 expect(2)
58 val sub = completable.subscribe({
59 expectUnreached()
60 }, { error ->
61 expect(6)
62 assertThat(error, IsInstanceOf(CancellationException::class.java))
63 })
64 expect(3)
65 yield() // to started coroutine
66 expect(5)
67 sub.unsubscribe() // will cancel coroutine
68 yield()
69 finish(7)
70 }
Konrad Kamiński804d0362017-04-07 09:22:58 +020071
72 @Test
73 fun testAwaitSuccess() = runBlocking<Unit> {
74 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030075 val completable = rxCompletable(coroutineContext) {
Konrad Kamiński804d0362017-04-07 09:22:58 +020076 expect(3)
77 }
78 expect(2)
79 completable.awaitCompleted() // shall launch coroutine
80 finish(4)
81 }
82
83 @Test
84 fun testAwaitFailure() = runBlocking<Unit> {
85 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030086 val completable = rxCompletable(coroutineContext) {
Konrad Kamiński804d0362017-04-07 09:22:58 +020087 expect(3)
88 throw RuntimeException("OK")
89 }
90 expect(2)
91 try {
92 completable.awaitCompleted() // shall launch coroutine and throw exception
93 expectUnreached()
94 } catch (e: RuntimeException) {
95 finish(4)
96 assertThat(e.message, IsEqual("OK"))
97 }
98 }
Roman Elizarov331750b2017-02-15 17:59:17 +030099}