Thursday, December 12, 2013

Testing Unhandled Messages in Akka

I had an occasion to verify that a message was truly not being handled by an actor when sending it a message while not yet initialized. After doing a little research, I didn't find any simple method signatures that would test for that, but the setup is simple enough.
1:  TestProbe subscriber = TestProbe.apply(system);  
2:  system().eventStream().subscribe(subscriber.ref(), UnhandledMessages.class);  
3:  // send your message here  
4:  subscriber.expectMsgClass(duration("1 second"), UnhandledMessage.class);  
You simply need to create a TestProbe that subscribes to the system's event stream and watch for UnhandledMessage.class. In line 4, the expectMsgClass will throw an assertion error if the message you sent is not unhandled by the actor. You can, of course, come up with more complex scenarios, but this handles the simple case.

No comments:

Post a Comment