#2093 - Adding support for setting the sender when using TestActorRef.receive

This commit is contained in:
Viktor Klang 2012-06-04 17:07:44 +02:00
parent f9a8cde812
commit 54febffb28
2 changed files with 19 additions and 2 deletions

View file

@ -56,7 +56,17 @@ class TestActorRef[T <: Actor](
* thrown will be available to you, while still being able to use
* become/unbecome.
*/
def receive(o: Any): Unit = underlying.receiveMessage(o)
def receive(o: Any): Unit = receive(o, underlying.system.deadLetters)
/**
* Directly inject messages into actor receive behavior. Any exceptions
* thrown will be available to you, while still being able to use
* become/unbecome.
*/
def receive(o: Any, sender: ActorRef): Unit = try {
underlying.currentMessage = Envelope(o, if (sender eq null) underlying.system.deadLetters else sender)(underlying.system)
underlying.receiveMessage(o)
} finally underlying.currentMessage = null
/**
* Retrieve reference to the underlying actor, where the static type matches the factory used inside the

View file

@ -246,11 +246,18 @@ class TestActorRefSpec extends AkkaSpec("disp1.type=Dispatcher") with BeforeAndA
a.underlying.dispatcher.getClass must be(classOf[Dispatcher])
}
"proxy receive for the underlying actor" in {
"proxy receive for the underlying actor without sender" in {
val ref = TestActorRef[WorkerActor]
ref.receive("work")
ref.isTerminated must be(true)
}
"proxy receive for the underlying actor with sender" in {
val ref = TestActorRef[WorkerActor]
ref.receive("work", testActor)
ref.isTerminated must be(true)
expectMsg("workDone")
}
}
}