문제

I'm new to Akka and actors model, and I'm struggling while trying to test an actor.

From the Akka documentation you could create a simple actor that bounds itself to a local port and listens to incoming connections with the following code:

class Server extends Actor {

  import Tcp._
  import context.system

  IO(Tcp) ! Bind(self, new InetSocketAddress("localhost", 12345))

  def receive = {
    case b @ Bound(localAddress) =>
      // do some logging or setup ...

    // rest omitted for brevity
  }

}

I'd like to write a test that creates a Server actor and then verifies that the actor has received the Bound message from IO(Tcp). I guess this is possible but I think I'm missing something. My understanding of expectMsg is that this is applied to an actor that is sending a message to the actor you're testing, receiving something back and asserting on that something it has received. Can I actually check what an actor I created has received?

도움이 되었습니까?

해결책

This test would be testing if Akka IO works/that you are using the API correctly and could remove your actor from the equation and instead use a TestProbe.

It could be a good idea to separate the setup/register logic from constructing your actor instance. This way you could test:

  1. that your setup code is correct using a TestProbe verifying that you get a Bound message
  2. Send your actor the same messages that IO would send to it and verify that it behaves correctly
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top