lundi 22 mars 2021

Is there a way to obtain the "stored" value within a receive definition of an Actor?

I'd like to be able to assert against a value that is "stored" into the Receive function of my actor. Is there a way to do that with any of the Akka Test libraries?

In the example below, I'd like to obtain currentCount.

import akka.actor.Actor

class CounterActor extends Actor {
  import CounterActor._
  
  override def receive: Receive = count(0) 

  def count(currentCount: Int): Receive = { 
    case Increment => context.become(count(currentCount + 1))
    case Decrement => context.become(count(currentCount - 1))
  }
}

object CounterActor {
  case object Increment
  case object Decrement
}

Then I'd like to assert against it with a Test class:

val counterActorRef = TestActorRef(new CounterActor())

"A counter actor" should {
  "have a count of 3" in {
    counterActorRef ! CounterActor.Increment
    counterActorRef ! CounterActor.Increment
    counterActorRef ! CounterActor.Increment
    
    val counterValue = // obtain currentCount from counterActorRef
    assert(counterValue == 3)
  }
}

I've tried using using TestActorRef, but haven't been successful. I know there's a way to satisfy this by adding a message case to obtain it, or adding some Debug/Info level log messages and using EventFilter. Is there simple way to obtain it with a test library that I'm unaware of?

I've searched through akka.testkit libraries for a while now, but I can't find my solution.

Any help is appreciated, thank you.

Aucun commentaire:

Enregistrer un commentaire