Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@quelgar
Copy link
Contributor

@quelgar quelgar commented Oct 21, 2020

Currently if Sink.take(n) collects the nth element at the end of a chunk, it will not emit its output until the stream ends or another element is received. In this situation it should just emit immediately, with an empty left-over.

It's a one character fix, but I lack the knowledge and/or imagination to construct a good test for it. I've been manually testing like this:

val s1 = Stream(1, 2) ++ Stream(3, 4)
runtime.unsafeRun(s1.run(Sink.take(2))) // emits Chunk(1, 2) immediately

val s2 = Stream(1, 2) ++ Stream(3, 4).mapChunksM(c => ZIO.sleep(15.seconds).as(c))
runtime.unsafeRun(s2.run(Sink.take(2)))  // master takes 15 seconds to emit Chunk(1, 2)

Any suggestions on how to write a good test for this? Never mind, I had a brainwave that Stream.never is what I needed.

If there's exactly enough data for Sink.take, it should emit
immediately with empty leftover.
@quelgar quelgar requested a review from iravid as a code owner October 21, 2020 01:30
@narma
Copy link
Contributor

narma commented Oct 26, 2020

Good catch!

Couple of thoughts:
idx is not correct naming there because actually, this value means how much left we want elements left to grab in. Because how we get it: n - take.length, where take is an accumulator. And this, I think, was leads to the issue, because idx always should be less than length.

My proposal here is rename idx to remain or something like that.
Also, I think Stream.die is a more convenient way to test behavior like this instead of Stream.never, because of in bad case it will be faster.

Test using Sink.die.
@quelgar
Copy link
Contributor Author

quelgar commented Oct 27, 2020

@narma yes, Stream.die is better, done 👍

@narma
Copy link
Contributor

narma commented Oct 29, 2020

@adamgfraser @iravid Guys, any suggestions?

Copy link
Contributor

@adamgfraser adamgfraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@adamgfraser adamgfraser merged commit bfc2a78 into zio:master Oct 29, 2020
@quelgar quelgar deleted the sink-take-emit-end-of-chunk branch November 12, 2020 12:16
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

Successfully merging this pull request may close these issues.

3 participants