2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
2011-10-16 18:33:35 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2011-10-16 18:33:35 +02:00
|
|
|
package akka.dataflow
|
|
|
|
|
|
2012-06-21 16:09:14 +02:00
|
|
|
import language.postfixOps
|
|
|
|
|
|
2011-10-16 18:33:35 +02:00
|
|
|
import akka.actor.{ Actor, Props }
|
2012-07-04 15:25:30 +02:00
|
|
|
import scala.concurrent.Future
|
2012-06-29 16:06:26 +02:00
|
|
|
import scala.concurrent.Await
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2012-06-29 16:06:26 +02:00
|
|
|
import akka.testkit.{ AkkaSpec, DefaultTimeout }
|
2012-02-01 14:04:01 +01:00
|
|
|
import akka.pattern.{ ask, pipe }
|
2012-07-21 11:42:16 +02:00
|
|
|
import scala.concurrent.ExecutionException
|
2011-10-16 18:33:35 +02:00
|
|
|
|
2011-12-05 20:01:42 +01:00
|
|
|
class Future2ActorSpec extends AkkaSpec with DefaultTimeout {
|
2012-07-17 17:21:08 +02:00
|
|
|
implicit val ec = system.dispatcher
|
2011-10-16 18:33:35 +02:00
|
|
|
"The Future2Actor bridge" must {
|
|
|
|
|
|
|
|
|
|
"support convenient sending to multiple destinations" in {
|
|
|
|
|
Future(42) pipeTo testActor pipeTo testActor
|
|
|
|
|
expectMsgAllOf(1 second, 42, 42)
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-24 12:04:31 +02:00
|
|
|
"support convenient sending to multiple destinations with implicit sender" in {
|
2012-08-29 18:00:14 +02:00
|
|
|
implicit val someActor = system.actorOf(Props(new Actor { def receive = Actor.emptyBehavior }))
|
2012-04-24 12:04:31 +02:00
|
|
|
Future(42) pipeTo testActor pipeTo testActor
|
|
|
|
|
expectMsgAllOf(1 second, 42, 42)
|
2015-01-16 11:09:59 +01:00
|
|
|
lastSender should ===(someActor)
|
2012-04-24 12:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"support convenient sending with explicit sender" in {
|
2012-08-29 18:00:14 +02:00
|
|
|
val someActor = system.actorOf(Props(new Actor { def receive = Actor.emptyBehavior }))
|
2012-04-24 12:04:31 +02:00
|
|
|
Future(42).to(testActor, someActor)
|
|
|
|
|
expectMsgAllOf(1 second, 42)
|
2015-01-16 11:09:59 +01:00
|
|
|
lastSender should ===(someActor)
|
2012-04-24 12:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-29 15:13:13 +02:00
|
|
|
"support reply via sender" in {
|
2011-11-16 17:18:36 +01:00
|
|
|
val actor = system.actorOf(Props(new Actor {
|
2011-10-16 18:33:35 +02:00
|
|
|
def receive = {
|
2014-01-16 15:16:35 +01:00
|
|
|
case "do" ⇒ Future(31) pipeTo context.sender()
|
|
|
|
|
case "ex" ⇒ Future(throw new AssertionError) pipeTo context.sender()
|
2011-10-16 18:33:35 +02:00
|
|
|
}
|
|
|
|
|
}))
|
2015-01-16 11:09:59 +01:00
|
|
|
Await.result(actor ? "do", timeout.duration) should ===(31)
|
2012-07-21 11:42:16 +02:00
|
|
|
intercept[ExecutionException] {
|
2011-12-12 22:50:08 +01:00
|
|
|
Await.result(actor ? "ex", timeout.duration)
|
2015-01-16 11:09:59 +01:00
|
|
|
}.getCause.isInstanceOf[AssertionError] should ===(true)
|
2011-10-16 18:33:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|