blob: 1fb4f0bb6439656c9d5636951f15758407d0e1bb [file] [log] [blame]
SokolovaMaria693142c2019-07-17 17:20:29 +03001package kotlinx.coroutines.reactor
2
3import kotlinx.coroutines.*
4import kotlinx.coroutines.reactive.*
5import org.junit.Test
6import reactor.util.context.Context
7import kotlin.test.assertEquals
8
9class ReactorContextTest {
10 @Test
11 fun testMonoHookedContext() = runBlocking {
12 val mono = mono(Context.of(1, "1", 7, "7").asCoroutineContext()) {
13 val ctx = coroutineContext[ReactorContext]?.context
14 buildString {
15 (1..7).forEach { append(ctx?.getOrDefault(it, "noValue")) }
16 }
17 } .subscriberContext(Context.of(2, "2", 3, "3", 4, "4", 5, "5"))
18 .subscriberContext { ctx -> ctx.put(6, "6") }
19 assertEquals(mono.awaitFirst(), "1234567")
20 }
21
22 @Test
23 fun testFluxContext() = runBlocking<Unit> {
24 val flux = flux(Context.of(1, "1", 7, "7").asCoroutineContext()) {
25 val ctx = coroutineContext[ReactorContext]!!.context
26 (1..7).forEach { send(ctx.getOrDefault(it, "noValue")) }
27 } .subscriberContext(Context.of(2, "2", 3, "3", 4, "4", 5, "5"))
28 .subscriberContext { ctx -> ctx.put(6, "6") }
29 var i = 0
30 flux.subscribe { str -> i++; assertEquals(str, i.toString()) }
31 }
32}