1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.reactive
import kotlinx.coroutines.*
import org.junit.*
import org.reactivestreams.*
class PublisherBackpressureTest : TestBase() {
@Test
fun testCancelWhileBPSuspended() = runBlocking {
expect(1)
val observable = publish {
expect(5)
send("A") // will not suspend, because an item was requested
expect(7)
send("B") // second requested item
expect(9)
try {
send("C") // will suspend (no more requested)
} finally {
expect(12)
}
expectUnreached()
}
expect(2)
var sub: Subscription? = null
observable.subscribe(object : Subscriber<String> {
override fun onSubscribe(s: Subscription) {
sub = s
expect(3)
s.request(2) // request two items
}
override fun onNext(t: String) {
when (t) {
"A" -> expect(6)
"B" -> expect(8)
else -> error("Should not happen")
}
}
override fun onComplete() {
expectUnreached()
}
override fun onError(e: Throwable) {
expectUnreached()
}
})
expect(4)
yield() // yield to observable coroutine
expect(10)
sub!!.cancel() // now unsubscribe -- shall cancel coroutine (& do not signal)
expect(11)
yield() // shall perform finally in coroutine
finish(13)
}
}
|