-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathBackpressureTest.kt
53 lines (47 loc) · 1.54 KB
/
BackpressureTest.kt
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
/*
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.reactor
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.reactive.*
import org.junit.Test
import reactor.core.publisher.*
import kotlin.test.*
class BackpressureTest : TestBase() {
@Test
fun testBackpressureDropDirect() = runTest {
expect(1)
Flux.fromArray(arrayOf(1))
.onBackpressureDrop()
.collect {
assertEquals(1, it)
expect(2)
}
finish(3)
}
@Test
fun testBackpressureDropFlow() = runTest {
expect(1)
Flux.fromArray(arrayOf(1))
.onBackpressureDrop()
.asFlow()
.collect {
assertEquals(1, it)
expect(2)
}
finish(3)
}
@Test
fun testCooperativeCancellation() = runTest {
val flow = Flux.fromIterable((0L..Long.MAX_VALUE)).asFlow()
flow.onEach { if (it > 10) currentContext().cancel() }.launchIn(this + Dispatchers.Default).join()
}
@Test
fun testCooperativeCancellationForBuffered() = runTest(expected = { it is CancellationException }) {
val flow = Flux.fromIterable((0L..Long.MAX_VALUE)).asFlow()
val channel = flow.onEach { if (it > 10) currentContext().cancel() }.produceIn(this + Dispatchers.Default)
channel.consumeEach { /* Do nothing, just consume elements */ }
}
}