Domanda

Credo che ho trovato un problema con il EventHandler. Le specifiche di seguito verrà eseguito sempre. Fondamentalmente la EventHandler.info() causerà questo. Ho provato a chiamare EventHandler.shutdown() utilizzando il Dopo spec. Ma senza fortuna. Pensi che mi manca qualcosa?

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)
    }
  }
}
È stato utile?

Soluzione

Nel mio test Attore Akka, ho una speciale caratteristica che chiama la registry.shutdownAll dopo l'esecuzione di tutti i frammenti. In questo modo, mentre è ancora necessario stare attenti che i test possono essere eseguiti in parallelo senza pestare l'altro, le cose vengono ripulite dopo tutti i test eseguiti. Ecco il tratto:

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

Altri suggerimenti

Ho provato con EventHandler.shutdown() e ha funzionato correttamente (senza riagganciare). Qui viene emesso:

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

E il codice:

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()
    }
  }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top