mercredi 22 mars 2017

How to collect actor response on non-actor in akka and write a Junit.

I am new to Akka, I am writing Junit for my Akka actor to test. I am able to excute Actor class using main method.

But when i try to write Junit to test failure case its not getting catch Exception. I am getting always TimeoutException any one tell me why. I have refer this post.

  • I would like to ask, what is the best way to collect response from akka actor after sending message to actor.
  • How to write JUnit in best way to implement Unit testing. I have rerering seerverl post. -I am new to Akka so can any one give some infor or exaple to understand how to collect response back from actor.
  • it will help to write Junit. I am sending response with cdoe "actorRef.tell(new ResponseMessage("Exception in EventScannerActor",e,false), originator);" is this correct way ?

My Actor Class :

public class EventScannerActor extends AbstractLoggingActor {
    final ActorRef actorRef = ACTOR_SYSTEM.actorOf(EventParserActor.props());

    public EventScannerActor() {

        receive(ReceiveBuilder
                .match(EventScannerActorMessage.class, this::onMessaage).build());
    }
    public static Props props() {

        return Props.create(EventScannerActor.class);
    }

    private void onMessaage(EventScannerActorMessage messaage) {
        final ActorRef originator = getContext().sender();
        try {

            List<Path> evensInFolder = Files.walk(Paths.get(messaage.getEventsPath()))
                    .filter(Files::isRegularFile).collect(Collectors.toList());

            evensInFolder.forEach(this::triggerEventParserEvent);

        } catch (IOException e) {
            log().error("Exception  in EventScannerActor:: onMessaage", e);
            actorRef.tell(new ResponseMessage("Exception  in EventScannerActor",e,false), originator);
            //throw new RuntimeException(e);
        }
        actorRef.tell(new ResponseMessage("Success",null,true), originator);

    }

    private void triggerEventParserEvent(Path event) {



        final EventParserActorMessage message = new EventParserActorMessage(event);

        actorRef.tell(message, actorRef);

    }

}

MY Testing JUnit Code:

@Test(expected=RuntimeException.class)
    public void scannerActorTester() throws Exception{
         ActorSystem sys = ActorSystem.apply("test");
         ActorRef ref = sys.actorOf(Props.create(ScannerActor.class), "mytest");        
         Timeout t = new Timeout(10, TimeUnit.SECONDS);
         Future<Object> fut = Patterns.ask(ref,new ScannerActorMessage("/invalidPath"), t);
         ResponseMessage response = (ResponseMessage)Await.result(fut, t.duration());
         System.out.println(response);

    }

I am getting always TimeoutException any one tell me why ?

 java.lang.Exception: Unexpected exception, expected<java.lang.RuntimeException> but was<java.util.concurrent.TimeoutException>
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:91)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.util.concurrent.TimeoutException: Futures timed out after [10 seconds]
    at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:219)
    at scala.concurrent.impl.Promise$DefaultPromise.result(Promise.scala:223)
    at scala.concurrent.Await$$anonfun$result$1.apply(package.scala:190)
    at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:53)
    at scala.concurrent.Await$.result(package.scala:190)
    at scala.concurrent.Await.result(package.scala)
    at com.events.reader.actor.EventsScannerActorTest.eventsScannerActorTester(EventsScannerActorTest.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    ... 14 more

Aucun commentaire:

Enregistrer un commentaire