SokolovaMaria | 693142c | 2019-07-17 17:20:29 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.reactor |
| 2 | |
| 3 | import kotlinx.coroutines.* |
| 4 | import kotlinx.coroutines.reactive.* |
| 5 | import org.junit.Test |
| 6 | import reactor.util.context.Context |
| 7 | import kotlin.test.assertEquals |
| 8 | |
| 9 | class 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 | } |