Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix context support in Publisher.asFlow.flowOn
* When using asFlow().flowOn(...) context is now properly tracked and taken into account for both execution context of the reactive subscription and for injection into Reactor context. * Publisher.asFlow slow-path implementation is simplified. It does not sure specialized openSubscription anymore, but always uses the same flow request logic. Fixes #1765
- Loading branch information
Showing
5 changed files
with
163 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
reactive/kotlinx-coroutines-reactor/test/FluxContextTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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.flow.* | ||
import kotlinx.coroutines.reactive.* | ||
import org.junit.* | ||
import org.junit.Test | ||
import reactor.core.publisher.* | ||
import kotlin.test.* | ||
|
||
class FluxContextTest : TestBase() { | ||
private val dispatcher = newSingleThreadContext("FluxContextTest") | ||
|
||
@After | ||
fun tearDown() { | ||
dispatcher.close() | ||
} | ||
|
||
@Test | ||
fun testFluxCreateAsFlowThread() = runTest { | ||
expect(1) | ||
val mainThread = Thread.currentThread() | ||
val dispatcherThread = withContext(dispatcher) { Thread.currentThread() } | ||
assertTrue(dispatcherThread != mainThread) | ||
Flux.create<String> { | ||
assertEquals(dispatcherThread, Thread.currentThread()) | ||
it.next("OK") | ||
it.complete() | ||
} | ||
.asFlow() | ||
.flowOn(dispatcher) | ||
.collect { | ||
expect(2) | ||
assertEquals("OK", it) | ||
assertEquals(mainThread, Thread.currentThread()) | ||
} | ||
finish(3) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
reactive/kotlinx-coroutines-rx2/test/FlowableContextTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.rx2 | ||
|
||
import io.reactivex.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.reactive.* | ||
import org.junit.* | ||
import org.junit.Test | ||
import kotlin.test.* | ||
|
||
class FlowableContextTest : TestBase() { | ||
private val dispatcher = newSingleThreadContext("FlowableContextTest") | ||
|
||
@After | ||
fun tearDown() { | ||
dispatcher.close() | ||
} | ||
|
||
@Test | ||
fun testFlowableCreateAsFlowThread() = runTest { | ||
expect(1) | ||
val mainThread = Thread.currentThread() | ||
val dispatcherThread = withContext(dispatcher) { Thread.currentThread() } | ||
assertTrue(dispatcherThread != mainThread) | ||
Flowable.create<String>({ | ||
assertEquals(dispatcherThread, Thread.currentThread()) | ||
it.onNext("OK") | ||
it.onComplete() | ||
}, BackpressureStrategy.BUFFER) | ||
.asFlow() | ||
.flowOn(dispatcher) | ||
.collect { | ||
expect(2) | ||
assertEquals("OK", it) | ||
assertEquals(mainThread, Thread.currentThread()) | ||
} | ||
finish(3) | ||
} | ||
} |