Akka EventHandler spinning forever. How should I stop it?
-
25-10-2019 - |
문제
I think I found a problem with the EventHandler
. The below spec will run forever.
Basically the EventHandler.info()
will cause this. I tried calling EventHandler.shutdown()
using the After spec. But, without luck. Do you think I'm missing something?
Akka: 1.3-RC1
class EventHandlerProblem extends Specification {
def is =
"This describes a possible problem with the EventHandler" ^
p ^
"The EventHandler should" ^
"not keep spinning forever...." ! e1
end
def e1 = {
// no need to start the actor
val ac = TestActorRef[PrintMessageActor]
true must beTrue
}
}
class PrintMessageActor extends Actor {
EventHandler.info(this, "Printer actor is starting up...")
def receive = {
case msg => {
println("Recieved: " + msg)
}
}
}
해결책
In my Akka Actor tests, I have a special trait which calls the registry.shutdownAll
after running all of the fragments. This way, while you still need to be careful that your tests can run in parallel without stepping on each other, things get cleaned up after all the tests run. Here's the trait:
import org.specs2.Specification
import org.specs2.specification.{Step,Fragments}
import akka.actor.Actor.registry
trait AkkaSpec extends Specification {
override def map(fs: => Fragments) = fs ^ Step(registry.shutdownAll)
}
class EventHandlerProblem extends AkkaSpec {
def is =
"This describes a possible problem with the EventHandler" ^
p ^
"The EventHandler should" ^
"not keep spinning forever...." ! e1
end
def e1 = {
// no need to start the actor
val ac = TestActorRef[PrintMessageActor]
true must beTrue
}
}
class PrintMessageActor extends Actor {
EventHandler.info(this, "Printer actor is starting up...")
def receive = {
case msg => {
println("Recieved: " + msg)
}
}
}
다른 팁
I tried with EventHandler.shutdown()
and it worked properly (without hanging up).
Here is output:
Testing started at 11:03 ...
[INFO] [29/11/11 11:03] [specs2.DefaultExecutionStrategy1] [PrintMessageActor] Printer actor is starting up...
[INFO] [29/11/11 11:03] [specs2.DefaultExecutionStrategy2] [PrintMessageActor] Printer actor is starting up...
Process finished with exit code 0
And code:
import akka.testkit.TestActorRef
import org.specs2.Specification
import akka.event.EventHandler
class EventHandlerProblemSpec extends Specification {
def is =
"This describes a possible problem with the EventHandler" ^
p ^
"The EventHandler should" ^
"not keep spinning forever...." ! e1 ^
"not keep spinning forever 2...." ! e2 ^
end
def e1 = {
{
val ac = TestActorRef[PrintMessageActor]
true must beTrue
} after {
EventHandler.shutdown()
}
}
def e2 = {
try {
val ac = TestActorRef[PrintMessageActor]
true must beTrue
} finally {
EventHandler.shutdown()
}
}
}
제휴하지 않습니다 StackOverflow