|
| 1 | +/* |
| 2 | + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.coroutines.rx2 |
| 6 | + |
| 7 | +import kotlinx.coroutines.* |
| 8 | +import kotlinx.coroutines.flow.* |
| 9 | +import org.hamcrest.core.* |
| 10 | +import org.junit.* |
| 11 | +import org.junit.Assert.* |
| 12 | + |
| 13 | +class FlowAsObservableTest : TestBase() { |
| 14 | + @Test |
| 15 | + fun testBasicSuccess() = runTest { |
| 16 | + expect(1) |
| 17 | + val observable = flow { |
| 18 | + expect(3) |
| 19 | + emit("OK") |
| 20 | + }.asObservable() |
| 21 | + |
| 22 | + expect(2) |
| 23 | + observable.subscribe { value -> |
| 24 | + expect(4) |
| 25 | + assertEquals("OK", value) |
| 26 | + } |
| 27 | + |
| 28 | + finish(5) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun testBasicFailure() = runTest { |
| 33 | + expect(1) |
| 34 | + val observable = flow<Int> { |
| 35 | + expect(3) |
| 36 | + throw RuntimeException("OK") |
| 37 | + }.asObservable() |
| 38 | + |
| 39 | + expect(2) |
| 40 | + observable.subscribe({ expectUnreached() }, { error -> |
| 41 | + expect(4) |
| 42 | + assertThat(error, IsInstanceOf(RuntimeException::class.java)) |
| 43 | + assertEquals("OK", error.message) |
| 44 | + }) |
| 45 | + finish(5) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testBasicUnsubscribe() = runTest { |
| 50 | + expect(1) |
| 51 | + val observable = flow<Int> { |
| 52 | + expect(3) |
| 53 | + hang { |
| 54 | + expect(4) |
| 55 | + } |
| 56 | + }.asObservable() |
| 57 | + |
| 58 | + expect(2) |
| 59 | + val sub = observable.subscribe({ expectUnreached() }, { expectUnreached() }) |
| 60 | + sub.dispose() // will cancel coroutine |
| 61 | + finish(5) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun testNotifyOnceOnCancellation() = runTest { |
| 66 | + val observable = |
| 67 | + flow { |
| 68 | + expect(3) |
| 69 | + emit("OK") |
| 70 | + hang { |
| 71 | + expect(7) |
| 72 | + } |
| 73 | + }.asObservable() |
| 74 | + .doOnNext { |
| 75 | + expect(4) |
| 76 | + assertEquals("OK", it) |
| 77 | + } |
| 78 | + .doOnDispose { |
| 79 | + expect(6) // notified once! |
| 80 | + } |
| 81 | + |
| 82 | + expect(1) |
| 83 | + val job = launch(start = CoroutineStart.UNDISPATCHED) { |
| 84 | + expect(2) |
| 85 | + observable.consumeEach { |
| 86 | + expect(5) |
| 87 | + assertEquals("OK", it) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + yield() |
| 92 | + job.cancelAndJoin() |
| 93 | + finish(8) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun testFailingConsumer() = runTest { |
| 98 | + expect(1) |
| 99 | + val observable = flow { |
| 100 | + expect(2) |
| 101 | + emit("OK") |
| 102 | + hang { |
| 103 | + expect(4) |
| 104 | + } |
| 105 | + |
| 106 | + }.asObservable() |
| 107 | + |
| 108 | + try { |
| 109 | + observable.consumeEach { |
| 110 | + expect(3) |
| 111 | + throw TestException() |
| 112 | + } |
| 113 | + } catch (e: TestException) { |
| 114 | + finish(5) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + fun testNonAtomicStart() = runTest { |
| 120 | + withContext(Dispatchers.Unconfined) { |
| 121 | + val observable = flow<Int> { |
| 122 | + expect(1) |
| 123 | + }.asObservable() |
| 124 | + |
| 125 | + val disposable = observable.subscribe({ expectUnreached() }, { expectUnreached() }, { expectUnreached() }) |
| 126 | + disposable.dispose() |
| 127 | + } |
| 128 | + finish(2) |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun testFlowCancelledFromWithin() = runTest { |
| 133 | + val observable = flow { |
| 134 | + expect(1) |
| 135 | + emit(1) |
| 136 | + kotlin.coroutines.coroutineContext.cancel() |
| 137 | + kotlin.coroutines.coroutineContext.ensureActive() |
| 138 | + expectUnreached() |
| 139 | + }.asObservable() |
| 140 | + |
| 141 | + observable.subscribe({ expect(2) }, { expectUnreached() }, { finish(3) }) |
| 142 | + } |
| 143 | +} |
0 commit comments