Package akka.testkit

Examples of akka.testkit.JavaTestKit


  }

  @Test
  public void testIt() {

    new JavaTestKit(system) {{
      ActorRef testactor = system.actorOf(Props.create(MessageInitExample.class), "testactor");
      String msg = "U OK?";

      testactor.tell(msg, getRef());
      expectNoMsg(Duration.create(1, TimeUnit.SECONDS));
View Full Code Here



  @Test
  public void demonstrateUsageOfConsistentHashableRouter() {

    new JavaTestKit(system) {{

      //#consistent-hashing-router     
     
      final ConsistentHashMapper hashMapper = new ConsistentHashMapper() {
        @Override
View Full Code Here

      system.actorOf(Props.create(MyBoundedUntypedActor.class));
  }

  @Test
  public void priorityDispatcher() throws Exception {
    JavaTestKit probe = new JavaTestKit(system);
    //#prio-dispatcher

    class Demo extends UntypedActor {
      LoggingAdapter log = Logging.getLogger(getContext().system(), this);
      {
        for (Object msg : new Object[] { "lowpriority", "lowpriority",
            "highpriority", "pigdog", "pigdog2", "pigdog3", "highpriority",
            PoisonPill.getInstance() }) {
          getSelf().tell(msg, getSelf());
        }
      }

      public void onReceive(Object message) {
        log.info(message.toString());
      }
    }

    // We create a new Actor that just prints out what it processes
    ActorRef myActor = system.actorOf(Props.create(Demo.class, this)
        .withDispatcher("prio-dispatcher"));

    /*
    Logs:
      'highpriority
      'highpriority
      'pigdog
      'pigdog2
      'pigdog3
      'lowpriority
      'lowpriority
    */
    //#prio-dispatcher

    probe.watch(myActor);
    probe.expectMsgClass(Terminated.class);
  }
View Full Code Here

    probe.expectMsgClass(Terminated.class);
  }

  @Test
  public void controlAwareDispatcher() throws Exception {
    JavaTestKit probe = new JavaTestKit(system);
    //#control-aware-dispatcher

    class Demo extends UntypedActor {
      LoggingAdapter log = Logging.getLogger(getContext().system(), this);
      {
        for (Object msg : new Object[] { "foo", "bar", new MyControlMessage(),
                PoisonPill.getInstance() }) {
          getSelf().tell(msg, getSelf());
        }
      }

      public void onReceive(Object message) {
        log.info(message.toString());
      }
    }

    // We create a new Actor that just prints out what it processes
    ActorRef myActor = system.actorOf(Props.create(Demo.class, this)
            .withDispatcher("control-aware-dispatcher"));

    /*
    Logs:
      'MyControlMessage
      'foo
      'bar
    */
    //#control-aware-dispatcher

    probe.watch(myActor);
    probe.expectMsgClass(Terminated.class);
  }
View Full Code Here

  private final ActorSystem system = actorSystemResource.getSystem();

  @Test
  public void testIt() {

    new JavaTestKit(system) {{
      ActorRef testactor = system.actorOf(Props.create(MessageInitExample.class), "testactor");
      String probe = "U OK?";

      testactor.tell(probe, getRef());
      expectNoMsg();
View Full Code Here

  public void testIt() {
    /*
     * Wrap the whole test procedure within a testkit constructor
     * if you want to receive actor replies or use Within(), etc.
     */
    new JavaTestKit(system) {{
      final Props props = Props.create(SomeActor.class);
      final ActorRef subject = system.actorOf(props);

      // can also use JavaTestKit “from the outside”
      final JavaTestKit probe = new JavaTestKit(system);
      // “inject” the probe by passing it to the test subject
      // like a real resource would be passed in production
      subject.tell(probe.getRef(), getRef());
      // await the correct response
      expectMsgEquals(duration("1 second"), "done");
     
      // the run() method needs to finish within 3 seconds
      new Within(duration("3 seconds")) {
        protected void run() {

          subject.tell("hello", getRef());

          // This is a demo: would normally use expectMsgEquals().
          // Wait time is bounded by 3-second deadline above.
          new AwaitCond() {
            protected boolean cond() {
              return probe.msgAvailable();
            }
          };

          // response must have been enqueued to us before probe
          expectMsgEquals(Duration.Zero(), "world");
          // check that the probe we injected earlier got the msg
          probe.expectMsgEquals(Duration.Zero(), "hello");
          Assert.assertEquals(getRef(), probe.getLastSender());

          // Will wait for the rest of the 3 seconds
          expectNoMsg();
        }
      };
View Full Code Here

  private final ActorSystem system = actorSystemResource.getSystem();

  @Test
  public void demonstrateInbox() {
    final JavaTestKit probe = new JavaTestKit(system);
    final ActorRef target = probe.getRef();
    //#inbox
    final Inbox inbox = Inbox.create(system);
    inbox.send(target, "hello");
    //#inbox
    probe.expectMsgEquals("hello");
    probe.send(probe.getLastSender(), "world");
    //#inbox
    try {
      assert inbox.receive(Duration.create(1, TimeUnit.SECONDS)).equals("world");
    } catch (java.util.concurrent.TimeoutException e) {
      // timeout
View Full Code Here

    //#inbox
  }
 
  @Test
  public void demonstrateWatch() {
    final JavaTestKit probe = new JavaTestKit(system);
    final ActorRef target = probe.getRef();
    //#watch
    final Inbox inbox = Inbox.create(system);
    inbox.watch(target);
    target.tell(PoisonPill.getInstance(), ActorRef.noSender());
    try {
View Full Code Here

    system = null;
  }

  @Test
  public void TheArithmeticServiceShouldCalculateConstantExpressionsProperly(){
    new JavaTestKit(system) {{
      final ActorRef service =
        system.actorOf(Props.create(ArithmeticService.class));
      final ActorRef probe = getRef();
      IntStream.range(-2, 3).forEach(x -> {
        service.tell(new Const(x), probe);
View Full Code Here

    }};
  }

  @Test
  public void TheArithmeticServiceShouldCalculateAdditionProperly(){
    new JavaTestKit(system) {{
      final ActorRef service =
        system.actorOf(Props.create(ArithmeticService.class));
      final ActorRef probe = getRef();
      IntStream.range(-2, 3).forEach(x ->
        IntStream.range(-2, 3).forEach(y -> {
View Full Code Here

TOP

Related Classes of akka.testkit.JavaTestKit

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.