Question

Je pense que je trouve un problème avec le EventHandler. Les spécifications ci-dessous fonctionnera toujours. Fondamentalement, le EventHandler.info() fera cela. J'ai essayé d'appeler EventHandler.shutdown() en utilisant la spécification après. Mais, sans chance. Pensez-vous que je manque quelque chose?

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)
    }
  }
}
Était-ce utile?

La solution

Dans mes tests d'Akka Acteur, j'ai un trait particulier qui appelle la registry.shutdownAll après l'exécution de tous les fragments. De cette façon, alors que vous devez toujours veiller à ce que vos tests peuvent fonctionner en parallèle sans marcher sur l'autre, les choses se nettoyer après tous les tests effectués. Voici le 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)
    }
  }
}

Autres conseils

J'ai essayé avec EventHandler.shutdown() et cela a fonctionné correctement (sans raccrocher). Voici la sortie:

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

Et le 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()
    }
  }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top