pekko/akka-stream-tests/src/test/scala/akka/stream/scaladsl/FlowRecoverWithSpec.scala

221 lines
6.6 KiB
Scala
Raw Normal View History

/*
* Copyright (C) 2016-2020 Lightbend Inc. <https://www.lightbend.com>
2016-01-29 22:06:36 -05:00
*/
2016-01-29 22:06:36 -05:00
package akka.stream.scaladsl
import akka.stream.stage.{ GraphStage, GraphStageLogic }
import akka.stream.testkit.StreamSpec
2016-01-29 22:06:36 -05:00
import akka.stream.testkit.scaladsl.TestSink
import akka.stream._
2016-01-29 22:06:36 -05:00
import akka.stream.testkit.Utils._
2018-06-15 18:16:58 +03:00
import akka.stream.testkit.scaladsl.StreamTestKit._
import com.github.ghik.silencer.silent
2016-01-29 22:06:36 -05:00
import scala.util.control.NoStackTrace
@silent // tests deprecated APIs
class FlowRecoverWithSpec extends StreamSpec {
2016-01-29 22:06:36 -05:00
val settings = ActorMaterializerSettings(system).withInputBuffer(initialSize = 1, maxSize = 1)
implicit val materializer: ActorMaterializer = ActorMaterializer(settings)
2016-01-29 22:06:36 -05:00
val ex = new RuntimeException("ex") with NoStackTrace
"A RecoverWith" must {
"recover when there is a handler" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
Source(1 to 4)
.map { a =>
if (a == 3) throw ex else a
}
.recoverWith { case _: Throwable => Source(List(0, -1)) }
2016-01-29 22:06:36 -05:00
.runWith(TestSink.probe[Int])
.request(2)
.expectNextN(1 to 2)
.request(1)
.expectNext(0)
.request(1)
.expectNext(-1)
.expectComplete()
}
"cancel substream if parent is terminated when there is a handler" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
Source(1 to 4)
.map { a =>
if (a == 3) throw ex else a
}
.recoverWith { case _: Throwable => Source(List(0, -1)) }
2016-01-29 22:06:36 -05:00
.runWith(TestSink.probe[Int])
.request(2)
.expectNextN(1 to 2)
.request(1)
.expectNext(0)
.cancel()
}
"failed stream if handler is not for such exception type" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
Source(1 to 3)
.map { a =>
if (a == 2) throw ex else a
}
.recoverWith { case _: IndexOutOfBoundsException => Source.single(0) }
2016-01-29 22:06:36 -05:00
.runWith(TestSink.probe[Int])
.request(1)
.expectNext(1)
.request(1)
.expectError(ex)
}
"be able to recover with the same unmaterialized source if configured" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
val src = Source(1 to 3).map { a =>
if (a == 3) throw ex else a
}
src
.recoverWith { case _: Throwable => src }
2016-01-29 22:06:36 -05:00
.runWith(TestSink.probe[Int])
.request(2)
.expectNextN(1 to 2)
.request(2)
.expectNextN(1 to 2)
.request(2)
.expectNextN(1 to 2)
.cancel()
}
"not influence stream when there is no exceptions" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
Source(1 to 3)
.map(identity)
.recoverWith { case _: Throwable => Source.single(0) }
2016-01-29 22:06:36 -05:00
.runWith(TestSink.probe[Int])
.request(3)
.expectNextN(1 to 3)
.expectComplete()
}
"finish stream if it's empty" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
Source.empty
.map(identity)
.recoverWith { case _: Throwable => Source.single(0) }
2016-01-29 22:06:36 -05:00
.runWith(TestSink.probe[Int])
.request(3)
.expectComplete()
}
"switch the second time if alternative source throws exception" in assertAllStagesStopped {
Source(1 to 3)
2019-03-11 10:38:24 +01:00
.map { a =>
if (a == 3) throw new IndexOutOfBoundsException() else a
}
2016-01-29 22:06:36 -05:00
.recoverWith {
case t: IndexOutOfBoundsException =>
Source(List(11, 22)).map(m => if (m == 22) throw new IllegalArgumentException() else m)
case t: IllegalArgumentException => Source(List(33, 44))
2019-03-11 10:38:24 +01:00
}
.runWith(TestSink.probe[Int])
2016-01-29 22:06:36 -05:00
.request(2)
.expectNextN(List(1, 2))
.request(2)
.expectNextN(List(11, 33))
.request(1)
.expectNext(44)
.expectComplete()
}
"terminate with exception if partial function fails to match after an alternative source failure" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
Source(1 to 3)
.map { a =>
if (a == 3) throw new IndexOutOfBoundsException() else a
}
2016-01-29 22:06:36 -05:00
.recoverWith {
case t: IndexOutOfBoundsException =>
Source(List(11, 22)).map(m => if (m == 22) throw ex else m)
2019-03-11 10:38:24 +01:00
}
.runWith(TestSink.probe[Int])
2016-01-29 22:06:36 -05:00
.request(2)
.expectNextN(List(1, 2))
.request(1)
.expectNextN(List(11))
.request(1)
.expectError(ex)
}
"terminate with exception after set number of retries" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
Source(1 to 3)
.map { a =>
if (a == 3) throw new IndexOutOfBoundsException() else a
}
.recoverWithRetries(3, {
case t: Throwable =>
Source(List(11, 22, 33)).map(m => if (m == 33) throw ex else m)
2019-03-11 10:38:24 +01:00
})
.runWith(TestSink.probe[Int])
.request(100)
.expectNextN(List(1, 2))
.expectNextN(List(11, 22))
.expectNextN(List(11, 22))
.expectNextN(List(11, 22))
.expectError(ex)
}
"not attempt recovering when attempts is zero" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
Source(1 to 3)
.map { a =>
if (a == 3) throw ex else a
}
.recoverWithRetries(0, { case t: Throwable => Source(List(22, 33)) })
.runWith(TestSink.probe[Int])
.request(100)
.expectNextN(List(1, 2))
.expectError(ex)
}
"recover infinitely when negative (-1) number of attempts given" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
val oneThenBoom = Source(1 to 2).map { a =>
if (a == 2) throw ex else a
}
oneThenBoom
.recoverWithRetries(-1, { case t: Throwable => oneThenBoom })
.runWith(TestSink.probe[Int])
.request(5)
.expectNextN(List(1, 2, 3, 4, 5).map(_ => 1))
.cancel()
}
"recover infinitely when negative (smaller than -1) number of attempts given" in assertAllStagesStopped {
2019-03-11 10:38:24 +01:00
val oneThenBoom = Source(1 to 2).map { a =>
if (a == 2) throw ex else a
}
oneThenBoom
.recoverWithRetries(-10, { case t: Throwable => oneThenBoom })
.runWith(TestSink.probe[Int])
.request(5)
.expectNextN(List(1, 2, 3, 4, 5).map(_ => 1))
.cancel()
}
"fail correctly when materialization of recover source fails" in assertAllStagesStopped {
val matFail = TE("fail!")
object FailingInnerMat extends GraphStage[SourceShape[String]] {
val out = Outlet[String]("out")
val shape = SourceShape(out)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) {
throw matFail
}
}
2019-03-11 10:38:24 +01:00
val result = Source
.failed(TE("trigger"))
.recoverWithRetries(1, {
case _: TE => Source.fromGraph(FailingInnerMat)
})
.runWith(Sink.ignore)
result.failed.futureValue should ===(matFail)
}
2016-01-29 22:06:36 -05:00
}
}