Skip to content
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
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
* The `chunkLimit` parameter controls backpressure on the source stream.
*/
def conflateChunks[F2[x] >: F[x]: Concurrent](chunkLimit: Int): Stream[F2, Chunk[O]] =
Stream.eval(Channel.bounded[F2, Chunk[O]](chunkLimit)).flatMap { chan =>
Stream.eval(Channel.bounded[F2, Chunk[O]](chunkLimit - 1)).flatMap { chan =>
val producer = chunks.through(chan.sendAll)
val consumer = chan.stream.chunks.map(_.combineAll)
consumer.concurrently(producer)
Expand Down
20 changes: 20 additions & 0 deletions core/shared/src/test/scala/fs2/StreamConflateSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package fs2

import cats.effect.IO
import cats.effect.testkit.TestControl
import cats.syntax.all._

import scala.concurrent.duration._

Expand All @@ -44,4 +45,23 @@ class StreamConflateSuite extends Fs2Suite {
)
)
}
test("conflateChunks respects chunk limit") {

(1 to 1000).toList.traverse_ { _ =>
Stream(1, 2, 3, 4, 5, 6, 7)
.covary[IO]
.chunkLimit(1)
.unchunks
.conflateChunks(3)
.compile
.toList
.map { chunks =>
assert(
chunks.forall(_.size <= 3),
s"Expected all chunks <= 3, but got: $chunks"
)
}
}
}

}
Loading