Skip to content

Flow.sample() - incorrect documentation example or bug #2243

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
drinkthestars opened this issue Sep 11, 2020 · 2 comments
Closed

Flow.sample() - incorrect documentation example or bug #2243

drinkthestars opened this issue Sep 11, 2020 · 2 comments

Comments

@drinkthestars
Copy link

drinkthestars commented Sep 11, 2020

The example provided in the docs for Flow.sample(...) does not seem to behave as the documentation states:

    flow {
        repeat(10) {
            emit(it)
            delay(50)
        }
    }
    .sample(100)
    .collect { print("$it ")}

According to documentation, expected output: 1 3 5 7 9
Actual output : 2 4 6 8 (sometimes even 2 4 6 8 9)

However, debounce's example works as documented when tested side-by-side here: https://pl.kotl.in/NS_M7Sx01

This could even be a bug with the playground, but it's weird that debounce works as expected in the same environment 🤔

@elizarov
Copy link
Contributor

Thanks a lot! Timings here are indeed a bit unrealistic, so it only works under virtual time, not in reality. Will fix.

elizarov added a commit that referenced this issue Sep 22, 2020
* All Flow.debounce/sample KDoc example code snippets are automatically tested with Knit.
* Flow.sample timings are made larger, so that they produce an expected output when run under the real time, too.

Fixes #2243
elizarov added a commit that referenced this issue Sep 22, 2020
* All Flow.debounce/sample KDoc example code snippets are automatically tested with Knit.
* Flow.sample timings are made larger, so that they produce an expected output when run under the real time, too.

Fixes #2243
@drinkthestars
Copy link
Author

Got it. Yes, definitely noticed that unit tests would pass, but behaves differently otherwise. Thanks @elizarov!

recheej pushed a commit to recheej/kotlinx.coroutines that referenced this issue Dec 28, 2020
* All Flow.debounce/sample KDoc example code snippets are automatically tested with Knit.
* Flow.sample timings are made larger, so that they produce an expected output when run under the real time, too.

Fixes Kotlin#2243
recheej pushed a commit to recheej/kotlinx.coroutines that referenced this issue Dec 28, 2020
* All Flow.debounce/sample KDoc example code snippets are automatically tested with Knit.
* Flow.sample timings are made larger, so that they produce an expected output when run under the real time, too.

Fixes Kotlin#2243
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants