2011-10-16 18:33:35 +02:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
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-06-29 13:33:20 +02:00
|
|
|
import scala.concurrent.util.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 {
|
|
|
|
|
implicit val someActor = system.actorOf(Props(ctx ⇒ Actor.emptyBehavior))
|
|
|
|
|
Future(42) pipeTo testActor pipeTo testActor
|
|
|
|
|
expectMsgAllOf(1 second, 42, 42)
|
|
|
|
|
lastSender must be(someActor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"support convenient sending with explicit sender" in {
|
|
|
|
|
val someActor = system.actorOf(Props(ctx ⇒ Actor.emptyBehavior))
|
|
|
|
|
Future(42).to(testActor, someActor)
|
|
|
|
|
expectMsgAllOf(1 second, 42)
|
|
|
|
|
lastSender must be(someActor)
|
|
|
|
|
}
|
|
|
|
|
|
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 = {
|
2011-10-22 16:06:20 +02: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
|
|
|
}
|
|
|
|
|
}))
|
2011-12-12 22:50:08 +01:00
|
|
|
Await.result(actor ? "do", timeout.duration) must be(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)
|
2012-07-21 11:42:16 +02:00
|
|
|
}.getCause.isInstanceOf[AssertionError] must be(true)
|
2011-10-16 18:33:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|