pekko/akka-stream-tests/src/test/scala/akka/stream/scaladsl/FlowForeachSpec.scala
Patrik Nordwall f930bcdda8 =str #15191 Verify that stage actors are stopped
* found one bug of too early actor stop and thereby missing cancel of upstream,
  in fan-in tests ""work with one immediately failed and one nonempty publisher"
2015-04-23 20:00:12 +02:00

56 lines
1.6 KiB
Scala

/**
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.stream.scaladsl
import scala.util.control.NoStackTrace
import akka.stream.ActorFlowMaterializer
import akka.stream.testkit.{ AkkaSpec, StreamTestKit }
import scala.concurrent.Await
import akka.stream.testkit.StreamTestKit.assertAllStagesStopped
class FlowForeachSpec extends AkkaSpec {
implicit val mat = ActorFlowMaterializer()
import system.dispatcher
"A Foreach" must {
"call the procedure for each element" in assertAllStagesStopped {
Source(1 to 3).runForeach(testActor ! _) onSuccess {
case _ testActor ! "done"
}
expectMsg(1)
expectMsg(2)
expectMsg(3)
expectMsg("done")
}
"complete the future for an empty stream" in assertAllStagesStopped {
Source.empty[String].runForeach(testActor ! _) onSuccess {
case _ testActor ! "done"
}
expectMsg("done")
}
"yield the first error" in assertAllStagesStopped {
val p = StreamTestKit.PublisherProbe[Int]()
Source(p).runForeach(testActor ! _) onFailure {
case ex testActor ! ex
}
val proc = p.expectSubscription
proc.expectRequest()
val ex = new RuntimeException("ex") with NoStackTrace
proc.sendError(ex)
expectMsg(ex)
}
"complete future with failure when function throws" in assertAllStagesStopped {
val error = new Exception with NoStackTrace
val future = Source.single(1).runForeach(_ throw error)
the[Exception] thrownBy Await.result(future, remaining) should be(error)
}
}
}