1
1
package kotlinx.coroutines.reactor
2
2
3
3
import kotlinx.coroutines.*
4
+ import kotlinx.coroutines.channels.*
5
+ import kotlinx.coroutines.flow.*
4
6
import kotlinx.coroutines.reactive.*
5
7
import org.junit.Test
6
8
import reactor.core.publisher.*
7
- import reactor.util.context.Context
8
- import kotlin.test.assertEquals
9
- import kotlinx.coroutines.flow.*
9
+ import reactor.util.context.*
10
+ import kotlin.test.*
10
11
11
12
class ReactorContextTest {
12
13
@Test
@@ -55,7 +56,9 @@ class ReactorContextTest {
55
56
}
56
57
57
58
@Test
58
- fun testFluxAwaitContextPropagation () = runBlocking<Unit >(Context .of(1 , " 1" , 2 , " 2" , 3 , " 3" ).asCoroutineContext()) {
59
+ fun testFluxAwaitContextPropagation () = runBlocking<Unit >(
60
+ Context .of(1 , " 1" , 2 , " 2" , 3 , " 3" ).asCoroutineContext()
61
+ ) {
59
62
assertEquals(f().awaitFirst(), " 1" )
60
63
assertEquals(f().awaitFirstOrDefault(" noValue" ), " 1" )
61
64
assertEquals(f().awaitFirstOrNull(), " 1" )
@@ -77,18 +80,32 @@ class ReactorContextTest {
77
80
}
78
81
79
82
@Test
80
- fun testFlowToFluxContextPropagation () = runBlocking(Context .of(1 , " 1" , 2 , " 2" , 3 , " 3" ).asCoroutineContext()) {
83
+ fun testFlowToFluxContextPropagation () = runBlocking(
84
+ Context .of(1 , " 1" , 2 , " 2" , 3 , " 3" ).asCoroutineContext()
85
+ ) {
81
86
var i = 0
87
+ // call "collect" on the converted Flow
82
88
bar().collect { str ->
83
89
i++ ; assertEquals(str, i.toString())
84
90
}
85
91
assertEquals(i, 3 )
86
92
}
87
93
88
- suspend fun bar (): Flow <String > {
89
- return flux {
90
- val ctx = coroutineContext[ReactorContext ]!! .context
91
- (1 .. 3 ).forEach { send(ctx.getOrDefault(it, " noValue" )) }
92
- }.asFlow()
94
+ @Test
95
+ fun testFlowToFluxDirectContextPropagation () = runBlocking(
96
+ Context .of(1 , " 1" , 2 , " 2" , 3 , " 3" ).asCoroutineContext()
97
+ ) {
98
+ var i = 0
99
+ // convert resulting flow to channel using "produceIn"
100
+ val channel = bar().produceIn(this )
101
+ channel.consumeEach { str ->
102
+ i++ ; assertEquals(str, i.toString())
103
+ }
104
+ assertEquals(i, 3 )
93
105
}
106
+
107
+ private fun bar (): Flow <String > = flux {
108
+ val ctx = coroutineContext[ReactorContext ]!! .context
109
+ (1 .. 3 ).forEach { send(ctx.getOrDefault(it, " noValue" )) }
110
+ }.asFlow()
94
111
}
0 commit comments