2014-09-02 18:13:36 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2014-10-27 14:35:41 +01:00
|
|
|
package akka.stream.scaladsl
|
2014-09-02 18:13:36 +02:00
|
|
|
|
|
|
|
|
import scala.util.control.NoStackTrace
|
2015-01-27 18:29:20 +01:00
|
|
|
import akka.stream.ActorFlowMaterializer
|
2014-10-27 14:35:41 +01:00
|
|
|
import akka.stream.testkit.{ AkkaSpec, StreamTestKit }
|
2015-03-05 18:26:17 +01:00
|
|
|
import scala.concurrent.Await
|
2015-04-16 20:13:43 +02:00
|
|
|
import akka.stream.testkit.StreamTestKit.assertAllStagesStopped
|
2014-10-27 14:35:41 +01:00
|
|
|
|
2014-09-02 18:13:36 +02:00
|
|
|
class FlowForeachSpec extends AkkaSpec {
|
|
|
|
|
|
2015-01-27 18:29:20 +01:00
|
|
|
implicit val mat = ActorFlowMaterializer()
|
2014-09-02 18:13:36 +02:00
|
|
|
import system.dispatcher
|
|
|
|
|
|
|
|
|
|
"A Foreach" must {
|
|
|
|
|
|
2015-04-16 20:13:43 +02:00
|
|
|
"call the procedure for each element" in assertAllStagesStopped {
|
2015-01-26 14:57:05 +01:00
|
|
|
Source(1 to 3).runForeach(testActor ! _) onSuccess {
|
2014-09-02 18:13:36 +02:00
|
|
|
case _ ⇒ testActor ! "done"
|
|
|
|
|
}
|
|
|
|
|
expectMsg(1)
|
|
|
|
|
expectMsg(2)
|
|
|
|
|
expectMsg(3)
|
|
|
|
|
expectMsg("done")
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-16 20:13:43 +02:00
|
|
|
"complete the future for an empty stream" in assertAllStagesStopped {
|
2015-03-05 12:21:17 +01:00
|
|
|
Source.empty[String].runForeach(testActor ! _) onSuccess {
|
2014-09-02 18:13:36 +02:00
|
|
|
case _ ⇒ testActor ! "done"
|
|
|
|
|
}
|
|
|
|
|
expectMsg("done")
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-16 20:13:43 +02:00
|
|
|
"yield the first error" in assertAllStagesStopped {
|
2014-09-02 18:13:36 +02:00
|
|
|
val p = StreamTestKit.PublisherProbe[Int]()
|
2015-01-26 14:57:05 +01:00
|
|
|
Source(p).runForeach(testActor ! _) onFailure {
|
2014-09-02 18:13:36 +02:00
|
|
|
case ex ⇒ testActor ! ex
|
|
|
|
|
}
|
|
|
|
|
val proc = p.expectSubscription
|
|
|
|
|
proc.expectRequest()
|
|
|
|
|
val ex = new RuntimeException("ex") with NoStackTrace
|
|
|
|
|
proc.sendError(ex)
|
|
|
|
|
expectMsg(ex)
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-16 20:13:43 +02:00
|
|
|
"complete future with failure when function throws" in assertAllStagesStopped {
|
2015-03-05 18:26:17 +01:00
|
|
|
val error = new Exception with NoStackTrace
|
|
|
|
|
val future = Source.single(1).runForeach(_ ⇒ throw error)
|
|
|
|
|
the[Exception] thrownBy Await.result(future, remaining) should be(error)
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-02 18:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
2015-03-05 12:21:17 +01:00
|
|
|
}
|