@@ -7,38 +7,40 @@ import kotlinx.coroutines.reactive.*
7
7
import org.junit.Test
8
8
import reactor.core.publisher.*
9
9
import reactor.util.context.*
10
+ import kotlin.coroutines.*
10
11
import kotlin.test.*
11
12
12
13
class ReactorContextTest : TestBase () {
13
14
@Test
14
15
fun testMonoHookedContext () = runBlocking {
15
16
val mono = mono(Context .of(1 , " 1" , 7 , " 7" ).asCoroutineContext()) {
16
- val ctx = coroutineContext[ ReactorContext ]?.context
17
+ val ctx = reactorContext()
17
18
buildString {
18
- (1 .. 7 ).forEach { append(ctx? .getOrDefault(it, " noValue" )) }
19
+ (1 .. 7 ).forEach { append(ctx.getOrDefault(it, " noValue" )) }
19
20
}
20
21
} .subscriberContext(Context .of(2 , " 2" , 3 , " 3" , 4 , " 4" , 5 , " 5" ))
21
22
.subscriberContext { ctx -> ctx.put(6 , " 6" ) }
22
23
assertEquals(mono.awaitFirst(), " 1234567" )
23
24
}
24
25
25
26
@Test
26
- fun testFluxContext () = runBlocking< Unit > {
27
+ fun testFluxContext () {
27
28
val flux = flux(Context .of(1 , " 1" , 7 , " 7" ).asCoroutineContext()) {
28
- val ctx = coroutineContext[ ReactorContext ] !! .context
29
+ val ctx = reactorContext()
29
30
(1 .. 7 ).forEach { send(ctx.getOrDefault(it, " noValue" )) }
30
- } .subscriberContext(Context .of(2 , " 2" , 3 , " 3" , 4 , " 4" , 5 , " 5" ))
31
+ }
32
+ .subscriberContext(Context .of(2 , " 2" , 3 , " 3" , 4 , " 4" , 5 , " 5" ))
31
33
.subscriberContext { ctx -> ctx.put(6 , " 6" ) }
32
- var i = 0
33
- flux.subscribe { str -> i ++ ; assertEquals(str, i .toString()) }
34
+ val list = flux.collectList().block() !!
35
+ assertEquals(( 1 .. 7 ).map { it .toString() }, list)
34
36
}
35
37
36
38
@Test
37
39
fun testAwait () = runBlocking(Context .of(3 , " 3" ).asCoroutineContext()) {
38
40
val result = mono(Context .of(1 , " 1" ).asCoroutineContext()) {
39
- val ctx = coroutineContext[ ReactorContext ]?.context
41
+ val ctx = reactorContext()
40
42
buildString {
41
- (1 .. 3 ).forEach { append(ctx? .getOrDefault(it, " noValue" )) }
43
+ (1 .. 3 ).forEach { append(ctx.getOrDefault(it, " noValue" )) }
42
44
}
43
45
} .subscriberContext(Context .of(2 , " 2" ))
44
46
.awaitFirst()
@@ -47,36 +49,34 @@ class ReactorContextTest : TestBase() {
47
49
48
50
@Test
49
51
fun testMonoAwaitContextPropagation () = runBlocking(Context .of(7 , " 7" ).asCoroutineContext()) {
50
- assertEquals(m ().awaitFirst(), " 7" )
51
- assertEquals(m ().awaitFirstOrDefault(" noValue" ), " 7" )
52
- assertEquals(m ().awaitFirstOrNull(), " 7" )
53
- assertEquals(m ().awaitFirstOrElse { " noValue" }, " 7" )
54
- assertEquals(m ().awaitLast(), " 7" )
55
- assertEquals(m ().awaitSingle(), " 7" )
52
+ assertEquals(createMono ().awaitFirst(), " 7" )
53
+ assertEquals(createMono ().awaitFirstOrDefault(" noValue" ), " 7" )
54
+ assertEquals(createMono ().awaitFirstOrNull(), " 7" )
55
+ assertEquals(createMono ().awaitFirstOrElse { " noValue" }, " 7" )
56
+ assertEquals(createMono ().awaitLast(), " 7" )
57
+ assertEquals(createMono ().awaitSingle(), " 7" )
56
58
}
57
59
58
60
@Test
59
61
fun testFluxAwaitContextPropagation () = runBlocking<Unit >(
60
62
Context .of(1 , " 1" , 2 , " 2" , 3 , " 3" ).asCoroutineContext()
61
63
) {
62
- assertEquals(f().awaitFirst(), " 1" )
63
- assertEquals(f().awaitFirstOrDefault(" noValue" ), " 1" )
64
- assertEquals(f().awaitFirstOrNull(), " 1" )
65
- assertEquals(f().awaitFirstOrElse { " noValue" }, " 1" )
66
- assertEquals(f().awaitLast(), " 3" )
67
- var i = 0
68
- f().subscribe { str -> i++ ; assertEquals(str, i.toString()) }
64
+ assertEquals(createFlux().awaitFirst(), " 1" )
65
+ assertEquals(createFlux().awaitFirstOrDefault(" noValue" ), " 1" )
66
+ assertEquals(createFlux().awaitFirstOrNull(), " 1" )
67
+ assertEquals(createFlux().awaitFirstOrElse { " noValue" }, " 1" )
68
+ assertEquals(createFlux().awaitLast(), " 3" )
69
69
}
70
70
71
- private fun m (): Mono <String > = mono {
72
- val ctx = coroutineContext[ ReactorContext ]?.context
73
- ctx? .getOrDefault(7 , " noValue" )
71
+ private fun createMono (): Mono <String > = mono {
72
+ val ctx = reactorContext()
73
+ ctx.getOrDefault(7 , " noValue" )
74
74
}
75
75
76
76
77
- private fun f (): Flux <String ?> = flux {
78
- val ctx = coroutineContext[ ReactorContext ]?.context
79
- (1 .. 3 ).forEach { send(ctx? .getOrDefault(it, " noValue" )) }
77
+ private fun createFlux (): Flux <String ?> = flux {
78
+ val ctx = reactorContext()
79
+ (1 .. 3 ).forEach { send(ctx.getOrDefault(it, " noValue" )) }
80
80
}
81
81
82
82
@Test
@@ -95,17 +95,17 @@ class ReactorContextTest : TestBase() {
95
95
fun testFlowToFluxDirectContextPropagation () = runBlocking(
96
96
Context .of(1 , " 1" , 2 , " 2" , 3 , " 3" ).asCoroutineContext()
97
97
) {
98
- var i = 0
99
98
// convert resulting flow to channel using "produceIn"
100
99
val channel = bar().produceIn(this )
101
- channel.consumeEach { str ->
102
- i++ ; assertEquals(str, i.toString())
103
- }
104
- assertEquals(i, 3 )
100
+ val list = channel.toList()
101
+ assertEquals(listOf (" 1" , " 2" , " 3" ), list)
105
102
}
106
103
107
104
private fun bar (): Flow <String > = flux {
108
- val ctx = coroutineContext[ ReactorContext ] !! .context
105
+ val ctx = reactorContext()
109
106
(1 .. 3 ).forEach { send(ctx.getOrDefault(it, " noValue" )) }
110
107
}.asFlow()
108
+
109
+ private suspend fun reactorContext () =
110
+ coroutineContext[ReactorContext ]!! .context
111
111
}
0 commit comments