Pregunta

Creo que encontré un problema con el EventHandler. La siguiente especificación se ejecutará para siempre. Básicamente el EventHandler.info() causará esto. Intenté llamar EventHandler.shutdown() Usando la especificación posterior. Pero, sin suerte. ¿Crees que me estoy perdiendo algo?

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)
    }
  }
}
¿Fue útil?

Solución

En mis pruebas de actor de Akka, tengo un rasgo especial que llama al registry.shutdownAll Después de ejecutar todos los fragmentos. De esta manera, aunque aún debe tener cuidado de que sus pruebas puedan funcionar en paralelo sin pisar el uno al otro, las cosas se limpian después de que se ejecutan todas las pruebas. Aquí está el rasgo:

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)
    }
  }
}

Otros consejos

Lo intenté con EventHandler.shutdown() Y funcionó correctamente (sin colgar). Aquí está la salida:

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

Y código:

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()
    }
  }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top