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

Add withTimeout combinator #1777

Merged
merged 4 commits into from
Feb 5, 2020
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
12 changes: 12 additions & 0 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import fs2.internal.{Resource => _, _}
import java.io.PrintStream

import scala.annotation.tailrec
import scala.concurrent.TimeoutException
import scala.concurrent.duration._

/**
Expand Down Expand Up @@ -2713,6 +2714,17 @@ final class Stream[+F[_], +O] private[fs2] (private val free: FreeC[F, O, Unit])
)(f: (Stream[F, O], Stream[F2, O2]) => Stream[F2, O3]): Stream[F2, O3] =
f(this, s2)

/** Fails this stream with a [[TimeoutException]] if it does not complete within given `timeout`. */
def timeout[F2[x] >: F[x]: Concurrent: Timer](
timeout: FiniteDuration
): Stream[F2, O] =
this.interruptWhen(
Timer[F2]
.sleep(timeout)
.as(Left(new TimeoutException(s"Timed out after $timeout")))
.widen[Either[Throwable, Unit]]
)

/**
* Translates effect type from `F` to `G` using the supplied `FunctionK`.
*
Expand Down
31 changes: 31 additions & 0 deletions core/shared/src/test/scala/fs2/StreamSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cats.effect._
import cats.effect.concurrent.{Deferred, Ref, Semaphore}
import cats.implicits._
import scala.concurrent.duration._
import scala.concurrent.TimeoutException
import org.scalactic.anyvals._
import org.scalatest.{Assertion, Succeeded}
import fs2.concurrent.{Queue, SignallingRef}
Expand Down Expand Up @@ -3786,4 +3787,34 @@ class StreamSpec extends Fs2Spec {
}
}
}

"withTimeout" - {
"timeout never-ending stream" in {
Copy link
Member

Choose a reason for hiding this comment

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

Would you mind also testing composability of timeouts? i.e. if you do (s1.timeout(d1) ++ s2).timeout(d2), what happens depending on the times of s1, s2, d1 > d2 (if d2 > d1, s1 might make it in time but s2 might not, so we still want to timeout, etc.)

Stream.never[IO].timeout(100.millis).compile.drain.assertThrows[TimeoutException]
}

"not trigger timeout on successfully completed stream" in {
Stream.sleep(10.millis).timeout(1.second).compile.drain.assertNoException
}

"compose timeouts d1 and d2 when d1 < d2" in {
val d1 = 20.millis
val d2 = 30.millis
(Stream.sleep(10.millis).timeout(d1) ++ Stream.sleep(30.millis))
.timeout(d2)
.compile
.drain
.assertThrows[TimeoutException]
}

"compose timeouts d1 and d2 when d1 > d2" in {
val d1 = 40.millis
val d2 = 30.millis
(Stream.sleep(10.millis).timeout(d1) ++ Stream.sleep(25.millis))
.timeout(d2)
.compile
.drain
.assertThrows[TimeoutException]
}
}
}