Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package kotlinx.coroutines.experimental.reactive |
| 18 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.* |
| 20 | import org.junit.* |
| 21 | import org.reactivestreams.* |
| 22 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 23 | |
| 24 | class PublisherBackpressureTest : TestBase() { |
| 25 | @Test |
| 26 | fun testCancelWhileBPSuspended() = runBlocking<Unit> { |
| 27 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 28 | val observable = publish(coroutineContext) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 29 | expect(5) |
| 30 | send("A") // will not suspend, because an item was requested |
| 31 | expect(7) |
| 32 | send("B") // second requested item |
| 33 | expect(9) |
| 34 | try { |
| 35 | send("C") // will suspend (no more requested) |
| 36 | } finally { |
Roman Elizarov | 58246ca | 2017-07-12 12:58:44 +0300 | [diff] [blame] | 37 | expect(13) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 38 | } |
| 39 | expectUnreached() |
| 40 | } |
| 41 | expect(2) |
| 42 | var sub: Subscription? = null |
| 43 | observable.subscribe(object : Subscriber<String> { |
| 44 | override fun onSubscribe(s: Subscription) { |
| 45 | sub = s |
| 46 | expect(3) |
| 47 | s.request(2) // request two items |
| 48 | } |
| 49 | |
| 50 | override fun onNext(t: String) { |
| 51 | when (t) { |
| 52 | "A" -> expect(6) |
| 53 | "B" -> expect(8) |
| 54 | else -> error("Should not happen") |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | override fun onComplete() { |
Roman Elizarov | 58246ca | 2017-07-12 12:58:44 +0300 | [diff] [blame] | 59 | expect(11) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | override fun onError(e: Throwable) { |
| 63 | expectUnreached() |
| 64 | } |
| 65 | }) |
| 66 | expect(4) |
| 67 | yield() // yield to observable coroutine |
| 68 | expect(10) |
Roman Elizarov | 58246ca | 2017-07-12 12:58:44 +0300 | [diff] [blame] | 69 | sub!!.cancel() // now unsubscribe -- shall cancel coroutine & immediately signal onComplete |
| 70 | expect(12) |
Roman Elizarov | 7a7b009 | 2017-06-26 14:25:56 +0300 | [diff] [blame] | 71 | yield() // shall perform finally in coroutine & report onComplete |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 72 | finish(14) |
| 73 | } |
| 74 | } |