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

Fixes eager observe that was causing early cleanup of resources. #1106

Merged
merged 1 commit into from
Mar 12, 2018
Merged
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
22 changes: 22 additions & 0 deletions core/jvm/src/test/scala/fs2/PipeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,28 @@ class PipeSpec extends Fs2Spec {
out2.map(_.toLong).sum shouldBe sum.get
}
}

"observe is not eager (1)" in {
//Do not pull another element before we emit the currently processed one
(Stream.eval(IO(1)) ++ Stream.eval(IO.raiseError(new Throwable("Boom"))))
.observe(_.evalMap(_ => IO(Thread.sleep(100)))) //Have to do some work here, so that we give time for the underlying stream to try pull more
.take(1)
.compile
.toVector
.unsafeRunSync shouldBe Vector(1)
}

"observe is not eager (2)" in {
//Do not pull another element before the downstream asks for another
(Stream.eval(IO(1)) ++ Stream.eval(IO.raiseError(new Throwable("Boom"))))
.observe(_.drain)
.flatMap(_ => Stream.eval(IO(Thread.sleep(100))) >> Stream(1, 2)) //Have to do some work here, so that we give time for the underlying stream to try pull more
.take(2)
.compile
.toVector
.unsafeRunSync shouldBe Vector(1, 2)
}

}
"handle errors from observing sink" in {
forAll { (s: PureStream[Int]) =>
Expand Down
10 changes: 5 additions & 5 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2185,14 +2185,14 @@ object Stream {
/** Send chunks through `sink`, allowing up to `maxQueued` pending _segments_ before blocking `s`. */
def observeAsync(maxQueued: Int)(sink: Sink[F, O])(implicit F: Effect[F],
ec: ExecutionContext): Stream[F, O] =
Stream.eval(async.semaphore[F](maxQueued)).flatMap { guard =>
Stream.eval(async.semaphore[F](maxQueued - 1)).flatMap { guard =>
Stream.eval(async.unboundedQueue[F, Option[Segment[O, Unit]]]).flatMap { outQ =>
Stream.eval(async.unboundedQueue[F, Option[Segment[O, Unit]]]).flatMap { sinkQ =>
val inputStream =
self.segments.noneTerminate.evalMap {
case Some(segment) =>
guard.decrement >>
sinkQ.enqueue1(Some(segment))
sinkQ.enqueue1(Some(segment)) >>
guard.decrement

case None =>
sinkQ.enqueue1(None)
Expand All @@ -2214,8 +2214,8 @@ object Stream {
val outputStream =
outQ.dequeue.unNoneTerminate
.flatMap { segment =>
Stream.eval(guard.increment) >>
Stream.segment(segment)
Stream.segment(segment) ++
Stream.eval_(guard.increment)
}

outputStream.concurrently(runner)
Expand Down