Created
May 8, 2012 13:20
-
-
Save write2munish/2634916 to your computer and use it in GitHub Desktop.
TestKit with Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Test | |
| public void testEchoActor() { | |
| ActorRef echoActorRef = _system.actorOf(new Props(EchoActor.class)); | |
| // pass the reference to implicit sender testActor() otherwise | |
| // message end up in dead mailbox | |
| echoActorRef.tell("Hi there", super.testActor()); | |
| expectMsg("Hi there"); | |
| } | |
| @Test | |
| public void testForwardingActor() { | |
| ActorRef forwardingActorRef = _system.actorOf(new Props( | |
| new UntypedActorFactory() { | |
| public UntypedActor create() { | |
| return new ForwardingActor(testActor()); | |
| } | |
| })); | |
| // pass the reference to implicit sender testActor() otherwise | |
| // message end up in dead mailbox | |
| forwardingActorRef.tell("test message", super.testActor()); | |
| expectMsg("test message"); | |
| } | |
| @Test | |
| public void testSequencingActor() { | |
| final List<Integer> headList = new ArrayList<Integer>(); | |
| final List<Integer> tailList = new ArrayList<Integer>(); | |
| int randomHead = new Random().nextInt(6); | |
| int randomTail = new Random().nextInt(10); | |
| for (int i = 0; i < randomHead; i++) | |
| headList.add(i); | |
| for (int i = 1; i < randomTail; i++) | |
| tailList.add(i); | |
| TestActorRef<SequencingActor> sequencingActorRef = TestActorRef.apply( | |
| new Props(new UntypedActorFactory() { | |
| public UntypedActor create() { | |
| return new SequencingActor(testActor(), headList, | |
| tailList); | |
| } | |
| }), _system); | |
| // pass the reference to implicit sender testActor() otherwise | |
| // message end up in dead mailbox | |
| sequencingActorRef.tell("do something", super.testActor()); | |
| for (Integer value : headList) { | |
| expectMsgClass(Integer.class); | |
| } | |
| expectMsg("do something"); | |
| for (Integer value : tailList) { | |
| expectMsgClass(Integer.class); | |
| } | |
| } | |
| @Test | |
| public void testFilteringActor() { | |
| TestActorRef<FilteringActor> filteringActorRef = TestActorRef.apply( | |
| new Props(new UntypedActorFactory() { | |
| public UntypedActor create() { | |
| return new FilteringActor(testActor()); | |
| } | |
| }), _system); | |
| // pass the reference to implicit sender testActor() otherwise | |
| // message end up in dead mailbox | |
| // first test | |
| filteringActorRef.tell("test message", super.testActor()); | |
| expectMsg("test message"); | |
| // second test | |
| filteringActorRef.tell(1, super.testActor()); | |
| expectNoMsg(); | |
| } | |
| /** | |
| * if you want to test whether the Supervisor strategy is working fine | |
| */ | |
| @Test | |
| public void testSupervisorStrategy1() throws Exception { | |
| final TestActorRef<SupervisorActor> supervisorActorRef1 = TestActorRef | |
| .apply(new Props(new UntypedActorFactory() { | |
| public UntypedActor create() { | |
| return new SupervisorActor(); | |
| } | |
| }), "supervisor1", _system); | |
| Duration timeout = Duration.parse("5 second"); | |
| // register the BoomActor with the Supervisor | |
| final ActorRef child = (ActorRef) Await.result( | |
| ask(supervisorActorRef1, new Props(BoomActor.class), 5000), | |
| timeout); | |
| child.tell(123); | |
| Assert.assertFalse(child.isTerminated()); | |
| } | |
| @Test | |
| public void testSupervisorStrategy2() throws Exception { | |
| TestActorRef<SupervisorActor> supervisorActorRef2 = TestActorRef.apply( | |
| new Props(new UntypedActorFactory() { | |
| public UntypedActor create() { | |
| return new SupervisorActor(); | |
| } | |
| }), "supervisor2", _system); | |
| final TestProbe probe = new TestProbe(_system); | |
| // register the BoomActor with the Supervisor | |
| final ActorRef child = (ActorRef) Await.result( | |
| ask(supervisorActorRef2, new Props(BoomActor.class), 5000), | |
| Duration.parse("5 second")); | |
| probe.watch(child); | |
| // second check | |
| child.tell("do something"); | |
| probe.expectMsg(new Terminated(child)); | |
| } | |
| @Test | |
| public void testBoomActor() { | |
| final TestActorRef child = TestActorRef.apply( | |
| new Props(BoomActor.class), _system); | |
| try { | |
| child.receive("do something"); | |
| //should not reach here | |
| Assert.assertTrue(false); | |
| } catch (IllegalArgumentException e) { | |
| Assert.assertEquals(e.getMessage(), "boom!"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment