Skip to content

Publish with multiple consumers doesn't work as expected #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sskrla opened this issue Jul 29, 2020 · 4 comments
Closed

Publish with multiple consumers doesn't work as expected #46

sskrla opened this issue Jul 29, 2020 · 4 comments
Labels
bug Something isn't working

Comments

@sskrla
Copy link

sskrla commented Jul 29, 2020

This may be a misunderstanding on my part, but coming from using Reactor (and in fact, porting code written in Reactor) I would expect the following flow to pass:

    @Test
    fun `publish multiple consumers`() = runBlocking {
        arrayOf(1, 2, 3, 4, 5)
                .asFlow()
                .publish {
                    shared ->
                        merge(shared.filter { it % 2 == 1}, shared.filter { it % 2 == 0})
                }
                .assertResult(1, 2, 3, 4, 5)
    }

But in reality, the flow completes yielding no results. Using flatMapConcat will yield results from the first sequence, and not the second.

@akarnokd
Copy link
Owner

What do you mean by yielding no results? Does the assertion fail?

@sskrla
Copy link
Author

sskrla commented Jul 29, 2020

Updated the code with a test I've added to the project. It fails with:

expected:<[1, 2, 3, 4, 5]> but was:<[]>
Expected :[1, 2, 3, 4, 5]
Actual   :[]

@akarnokd akarnokd added the bug Something isn't working label Jul 29, 2020
@akarnokd
Copy link
Owner

For some reason, merge doesn't start collecting the inner subject and the main source just runs to completion without observers. I have no idea why that is.

I know they had quite some trouble with their shareIn operator too. Kotlin/kotlinx.coroutines#2047

@akarnokd
Copy link
Owner

akarnokd commented Sep 7, 2020

This is a complicated issue and I'm not sure how to solve it without asking the programmer for more parameters.

The workaround I suggest is in the form of an overload to publish where you can tell it to wait for these 2 inner collectors.

Closing via adec011

@akarnokd akarnokd closed this as completed Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants