Skip to content
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

fix: chunk get together when random rechunk #3205

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2399,7 +2399,7 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
Stream.suspend {
assert(maxFactor >= minFactor, "maxFactor should be greater or equal to minFactor")
val random = new scala.util.Random(seed)
def factor: Double = Math.abs(random.nextInt()) % (maxFactor - minFactor) + minFactor
def factor: Double = Math.abs(random.nextInt()) % (maxFactor - minFactor) + minFactor + 1e-4

def nextSize(sourceSize: Int): Int = (factor * sourceSize).toInt

Expand All @@ -2410,10 +2410,10 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
if (acc.size < size)
go(acc ++ hd, size, tl)
else if (acc.size == size)
Pull.output(acc) >> go(hd, size, tl)
Pull.output(acc) >> go(hd, -1, tl)
else {
val (out, rem) = acc.splitAt(size - 1)
Pull.output(out) >> go(rem ++ hd, -1, tl)
Pull.output(out) >> go(rem, -1, s)
}
case None =>
Pull.output(acc)
Expand Down
7 changes: 7 additions & 0 deletions core/shared/src/test/scala/fs2/StreamSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,13 @@ class StreamSuite extends Fs2Suite {
assertEquals(countChunks(source), 2)
}

test("rechunkRandomly sometimes emits everything in a single chunk") {
val fiveChunks = Stream(1) ++ Stream(2) ++ Stream(3) ++ Stream(4) ++ Stream(5)

val source = fiveChunks.rechunkRandomlyWithSeed(0.1, 2.0)(5).chunks.toList
Copy link
Contributor

Choose a reason for hiding this comment

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

Also the way factor and nextSize are implemented now, I don't think factor can ever reach maxFactor. So for this example nextSize can only be 0 or 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that's true. Now the range is [minFactor, maxFactor).

assert(source.forall(_.size <= 2), "Some Chunks have larger size than maxFactor * chunkSize")
}

group("Stream[F, Either[Throwable, O]]") {
test(".evalMap(_.pure.rethrow).mask <-> .rethrow.mask") {
forAllF { (stream: Stream[Pure, Int]) =>
Expand Down