1

I have created an actor which extends UnTypedProcessor. I intend to use this actor to persist some of it's messages to disk. The actor looks like so,

public class Shard extends UntypedProcessor {

  LoggingAdapter log = Logging.getLogger(getContext().system(), this);

  @Override
  public void onReceive(Object message) throws Exception {
    if(message instanceof CreateTransactionChain) {
      System.out.println("Received a CreateTransactionChain message");
      ActorRef transactionChain = getContext().actorOf(Props.create(TransactionChain.class), "transactionChain");
      Address address = transactionChain.path().address();
      getSender().tell(new CreateTransactionReply(address), getSelf());
    }
  }
}

I have a unit test written for this like so,

public class ShardTest extends AbstractActorTest{
  @Test
  public void testOnReceiveCreateTransaction() throws Exception {
System.out.println("running tests");

new JavaTestKit(getSystem()) {{
  final Props props = Props.create(Shard.class);
  final TestActorRef<Shard> subject = TestActorRef.create(getSystem(), props, "test");

  // can also use JavaTestKit “from the outside”
  final JavaTestKit probe = new JavaTestKit(getSystem());

  // the run() method needs to finish within 3 seconds
  new Within(duration("3 seconds")) {
    protected void run() {

      subject.tell(new CreateTransactionChain(), getRef());

      final String out = new ExpectMsg<String>("match hint") {
        // do not put code outside this method, will run afterwards
        protected String match(Object in) {
          if (in instanceof CreateTransactionReply) {
            return "match";
          } else {
            throw noMatch();
          }
        }
      }.get(); // this extracts the received message

      assertEquals("match", out);

      // Will wait for the rest of the 3 seconds
      expectNoMsg();
    }


      };
    }};
  }
}

When I run this test the onReceive method of the UntypeProcessor does not get invoked. If I extended my class from UntypedActor instead things work just fine. Any ideas why extending UntypedProcessor does not work? Is there some configuration I need to add to get this to work? Is there something that needs to be mocked?

4

1 回答 1

1

akka-persistence does not work with the same thread dispatcher which is what the TestActorRef provides. We need to switch to using a simple ActorRef so that a multithreaded dispatcher can be used for the test.

This github issue talks about the very same problem :- https://github.com/akka/akka/issues/15293

于 2014-06-17T02:24:44.537 回答