Roman Elizarov | 331750b | 2017-02-15 17:59:17 +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 | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.rx1 |
| 6 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.* |
| 8 | import org.hamcrest.core.* |
| 9 | import org.junit.* |
| 10 | import org.junit.Assert.* |
| 11 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 12 | |
| 13 | class CompletableTest : TestBase() { |
| 14 | @Test |
| 15 | fun testBasicSuccess() = runBlocking<Unit> { |
| 16 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 17 | val completable = rxCompletable(coroutineContext) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 18 | 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 Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 32 | val completable = rxCompletable(coroutineContext) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 33 | 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 Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 52 | val completable = rxCompletable(coroutineContext) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 53 | 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ński | 804d036 | 2017-04-07 09:22:58 +0200 | [diff] [blame] | 71 | |
| 72 | @Test |
| 73 | fun testAwaitSuccess() = runBlocking<Unit> { |
| 74 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 75 | val completable = rxCompletable(coroutineContext) { |
Konrad Kamiński | 804d036 | 2017-04-07 09:22:58 +0200 | [diff] [blame] | 76 | 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 Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 86 | val completable = rxCompletable(coroutineContext) { |
Konrad Kamiński | 804d036 | 2017-04-07 09:22:58 +0200 | [diff] [blame] | 87 | 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 Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 99 | } |