akka.testkit.JavaTestKit Java Examples

The following examples show how to use akka.testkit.JavaTestKit. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaristaTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSendCoffeePreparedWithRandomCoffeeForInaccurateResponse() {
    new JavaTestKit(system) {{
        Integer accuracy = 50;
        Long runs = 1000L;
        ActorRef guest = system.deadLetters();
        ActorRef barista = system.actorOf(Barista.props(duration("0 milliseconds"), accuracy));
        List<Coffee> coffees = new ArrayList<>();
        for (int i = 0; i < runs; i++) {
            barista.tell(new Barista.PrepareCoffee(new Coffee.Akkaccino(), guest), getRef());
            Barista.CoffeePrepared cp = expectMsgClass(duration("50 milliseconds"), Barista.CoffeePrepared.class);
            coffees.add(cp.coffee);
        }
        Long expectedCount = runs * accuracy / 100;
        Long variation = expectedCount / 10;
        Long numberOfCorrectCoffee = coffees.stream().filter(c -> c.equals(new Coffee.Akkaccino())).count();
        assertThat(numberOfCorrectCoffee).isBetween(expectedCount - variation, expectedCount + variation);
    }};
}
 
Example #2
Source File: CoffeeHouseTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRestartWaiterAndResendPrepareCoffeeToBaristaOnFailure() {
    new JavaTestKit(system) {{
        createActor(CoffeeHouse.class, "resend-prepare-coffee", () -> new CoffeeHouse(Integer.MAX_VALUE) {
            @Override
            protected ActorRef createBarista() {
                return getRef();
            }

            @Override
            protected ActorRef createWaiter() { //stubbing out the waiter actor to always throw exception
                return context().actorOf(Props.create(AbstractActor.class, () -> new AbstractActor() {
                    @Override
                    public Receive createReceive() {
                        return receiveBuilder().matchAny(o -> {
                            throw new Waiter.FrustratedException(new Coffee.Akkaccino(), system.deadLetters());
                        }).build();
                    }
                }), "waiter");
            }
        });
        ActorRef waiter = expectActor(this, "/user/resend-prepare-coffee/waiter");
        waiter.tell("Blow up", ActorRef.noSender());
        expectMsgEquals(new Barista.PrepareCoffee(new Coffee.Akkaccino(), system.deadLetters()));
    }};
}
 
Example #3
Source File: TaskManagerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testTerminationOnFatalError() {
	highAvailabilityServices.setJobMasterLeaderRetriever(
		HighAvailabilityServices.DEFAULT_JOB_ID,
		new SettableLeaderRetrievalService());

	new JavaTestKit(system){{

		final ActorGateway taskManager = TestingUtils.createTaskManager(
				system,
				highAvailabilityServices, // no jobmanager
				new Configuration(),
				true,
				false);

		try {
			watch(taskManager.actor());
			taskManager.tell(new FatalError("test fatal error", new Exception("something super bad")));
			expectTerminated(d, taskManager.actor());
		}
		finally {
			taskManager.tell(Kill.getInstance());
		}
	}};
}
 
Example #4
Source File: PingPongActorTest.java    From learning-akka with Apache License 2.0 6 votes vote down vote up
@Test
public void testPongActor() {
    new JavaTestKit(system) {{
        final ActorRef pongActor = system.actorOf(PongActor.props());
        pongActor.tell(new PingActor.PingMessage("ping"), getRef());

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

        assertEquals("pong", pongResult);
    }};
}
 
Example #5
Source File: BaristaTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSendCoffeePreparedWithRandomCoffeeForInaccurateResponse() {
    new JavaTestKit(system) {{
        Integer accuracy = 50;
        Long runs = 1000L;
        ActorRef guest = system.deadLetters();
        ActorRef barista = system.actorOf(Barista.props(duration("0 milliseconds"), accuracy));
        List<Coffee> coffees = new ArrayList<>();
        for (int i = 0; i < runs; i++) {
            barista.tell(new Barista.PrepareCoffee(new Coffee.Akkaccino(), guest), getRef());
            Barista.CoffeePrepared cp = expectMsgClass(duration("50 milliseconds"), Barista.CoffeePrepared.class);
            coffees.add(cp.coffee);
        }
        Long expectedCount = runs * accuracy / 100;
        Long variation = expectedCount / 10;
        Long numberOfCorrectCoffee = coffees.stream().filter(c -> c.equals(new Coffee.Akkaccino())).count();
        assertThat(numberOfCorrectCoffee).isBetween(expectedCount - variation, expectedCount + variation);
    }};
}
 
Example #6
Source File: ChatroomTest.java    From learning-akka with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldSendHistoryWhenUserJoin() {
    new JavaTestKit(system) {{
        //Given
        Props props = Props.create(Chatroom.class);
        TestActorRef<Chatroom> ref = TestActorRef.create(system, props);
        Chatroom chatroom = ref.underlyingActor();
        Messages.PostToChatroom msg = new Messages.PostToChatroom("test", "user");
        chatroom.chatHistory.add(msg);

        //When
        UserRef userRef = new UserRef(system.deadLetters(), "user");
        Messages.JoinChatroom request = new Messages.JoinChatroom(userRef);
        ref.tell(request, getRef());

        //Then
        List expected = new ArrayList<Messages.PostToChatroom>();
        expected.add(msg);
        expectMsgEquals(duration("1 second"), expected);
    }};
}
 
Example #7
Source File: WaiterTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void sendingComplaintShouldResultInPrepareCoffeeToBarista() {
    new JavaTestKit(system) {{
        ActorRef barista = getRef();
        TestProbe guest = new TestProbe(system);
        ActorRef waiter = system.actorOf(Waiter.props(system.deadLetters(), barista, 1));

        waiter.tell(new Waiter.Complaint(new Coffee.Akkaccino()), guest.ref());
        expectMsgEquals(new Barista.PrepareCoffee(new Coffee.Akkaccino(), guest.ref()));
    }};
}
 
Example #8
Source File: CoffeeHouseAppTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateNGuestsBasedOnCount() {
    new JavaTestKit(system) {{
        new CoffeeHouseApp(system) {
            @Override
            protected ActorRef createCoffeeHouse() {
                return getRef();
            }
        }.createGuest(2, new Coffee.Akkaccino(), Integer.MAX_VALUE);
        expectMsgAllOf(new CoffeeHouse.CreateGuest(new Coffee.Akkaccino(), Integer.MAX_VALUE),
                new CoffeeHouse.CreateGuest(new Coffee.Akkaccino(), Integer.MAX_VALUE));
    }};

}
 
Example #9
Source File: WaiterTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void sendingServeCoffeeShouldResultInApproveCoffeeToCoffeeHouse() {
    new JavaTestKit(system) {{
        ActorRef coffeeHouse = getRef();
        TestProbe guest = new TestProbe(system);
        ActorRef waiter = system.actorOf(Waiter.props(coffeeHouse, system.deadLetters(), Integer.MAX_VALUE));
        waiter.tell(new Waiter.ServeCoffee(new Coffee.Akkaccino()), guest.ref());
        expectMsgEquals(new CoffeeHouse.ApproveCoffee(new Coffee.Akkaccino(), guest.ref()));
    }};
}
 
Example #10
Source File: GuestTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void serveCoffeeShouldBeSentAfterFinishCoffeeDuration() {
    new JavaTestKit(system) {{
        ActorRef guest = createGuest(this, getRef());
        new Within(duration("50 milliseconds"), duration("200 milliseconds")) {
            @Override
            protected void run() {
                guest.tell(new Waiter.CoffeeServed(new Coffee.Akkaccino()), ActorRef.noSender());
                expectMsgEquals(new Waiter.ServeCoffee(new Coffee.Akkaccino()));
            }
        };
    }};
}
 
Example #11
Source File: BaristaTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void sendingPrepareCoffeeShouldResultInCoffeePreparedResponse() {
    new JavaTestKit(system) {{
        ActorRef barista = system.actorOf(Barista.props(duration("100 milliseconds")));
        new Within(duration("50 milliseconds"), duration("1000 milliseconds")) {
            @Override
            protected void run() {
                barista.tell(new Barista.PrepareCoffee(new Coffee.Akkaccino(), system.deadLetters()), getRef());
                expectMsgEquals(new Barista.CoffeePrepared(new Coffee.Akkaccino(), system.deadLetters()));
            }
        };
    }};
}
 
Example #12
Source File: BaseAkkaTestCase.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
public ActorRef expectActor(JavaTestKit kit, String path) {
    final ActorRef[] actor = {null};
    kit.new AwaitCond(kit.duration("3 seconds"), kit.duration("150 millis"), "No actor found with " + path) {
        @Override
        protected boolean cond() {
            TestProbe probe = new TestProbe(system);
            system.actorSelection(path).tell(new akka.actor.Identify(101), probe.ref());
            ActorIdentity i = probe.expectMsgClass(kit.duration("100 millis"), ActorIdentity.class);
            actor[0] = i.getRef();
            return i.getRef() != null;
        }
    };
    return actor[0];
}
 
Example #13
Source File: CoffeeHouseAppTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateATopLevelActorCalledCoffeeHouse() {
    new JavaTestKit(system) {{
        new CoffeeHouseApp(system);
        String path = "/user/coffee-house";
        expectActor(this, path);
    }};
}
 
Example #14
Source File: GuestTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSendComplaintWhenWrongDrinkIsSent() {
    new JavaTestKit(system) {{
        ActorRef guest = createGuest(this, getRef());
        guest.tell(new Waiter.CoffeeServed(new Coffee.MochaPlay()), ActorRef.noSender());
        expectMsgEquals(new Waiter.Complaint((new Coffee.Akkaccino())));
    }};
}
 
Example #15
Source File: CoffeeHouseTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateChildActorCalledBaristaWhenCreated() {
    new JavaTestKit(system) {{
        system.actorOf(CoffeeHouse.props(Integer.MAX_VALUE), "create-barista");
        expectActor(this, "/user/create-barista/waiter");
    }};
}
 
Example #16
Source File: CoffeeHouseAppTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateATopLevelActorCalledCoffeeHouse() {
    new JavaTestKit(system) {{
        new CoffeeHouseApp(system);
        String path = "/user/coffee-house";
        expectActor(this, path);
    }};
}
 
Example #17
Source File: BaseAkkaTestCase.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
public ActorRef expectActor(JavaTestKit kit, String path) {
    final ActorRef[] actor = {null};
    kit.new AwaitCond(kit.duration("3 seconds"), kit.duration("150 millis"), "No actor found with " + path) {
        @Override
        protected boolean cond() {
            TestProbe probe = new TestProbe(system);
            system.actorSelection(path).tell(new akka.actor.Identify(101), probe.ref());
            ActorIdentity i = probe.expectMsgClass(kit.duration("100 millis"), ActorIdentity.class);
            actor[0] = i.getRef();
            return i.getRef() != null;
        }
    };
    return actor[0];
}
 
Example #18
Source File: CoffeeHouseTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateGuestActorsWhenCreateGuestMessageSent() {
    new JavaTestKit(system) {{
        ActorRef coffeeHouse = system.actorOf(Props.create(CoffeeHouse.class), "create-guest");
        coffeeHouse.tell(CoffeeHouse.CreateGuest.Instance, ActorRef.noSender());
        expectActor(this, "/user/create-guest/$*");
    }};
}
 
Example #19
Source File: CoffeeHouseTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateGuestActorsWhenCreateGuestMessageSent() {
    new JavaTestKit(system) {{
        ActorRef coffeeHouse = system.actorOf(CoffeeHouse.props(Integer.MAX_VALUE), "create-guest");
        coffeeHouse.tell(new CoffeeHouse.CreateGuest(new Coffee.Akkaccino(), Integer.MAX_VALUE), ActorRef.noSender());
        expectActor(this, "/user/create-guest/$*");
    }};
}
 
Example #20
Source File: WaiterTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void sendingServeCoffeeShouldResultInCoffeeServedResponse() {
    new JavaTestKit(system) {{
        ActorRef waiter = system.actorOf(Waiter.props());
        waiter.tell(new Waiter.ServeCoffee(new Coffee.Akkaccino()), getRef());
        expectMsgEquals(new Waiter.CoffeeServed(new Coffee.Akkaccino()));
    }};
}
 
Example #21
Source File: WaiterTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void sendingServeCoffeeShouldResultInApproveCoffeeToCoffeeHouse() {
    new JavaTestKit(system) {{
        ActorRef coffeeHouse = getRef();
        TestProbe guest = new TestProbe(system);
        ActorRef waiter = system.actorOf(Waiter.props(coffeeHouse));
        waiter.tell(new Waiter.ServeCoffee(new Coffee.Akkaccino()), guest.ref());
        expectMsgEquals(new CoffeeHouse.ApproveCoffee(new Coffee.Akkaccino(), guest.ref()));
    }};
}
 
Example #22
Source File: CoffeeHouseAppTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateATopLevelActorCalledCoffeeHouse() {
    new JavaTestKit(system) {{
        new CoffeeHouseApp(system);
        String path = "/user/coffee-house";
        expectActor(this, path);
    }};
}
 
Example #23
Source File: GuestTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void sendingCoffeeFinishedShouldResultInServeCoffeeMessageToWaiter() {
    new JavaTestKit(system) {{
        ActorRef guest = createGuest(this, getRef());
        guest.tell(Guest.CoffeeFinished.Instance, ActorRef.noSender());
        expectMsgEquals(new Waiter.ServeCoffee(new Coffee.Akkaccino()));
    }};
}
 
Example #24
Source File: GuestTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void sendingCoffeeFinishedShouldInExceptionIfCaffeineLimitExceeded() {
    new JavaTestKit(system) {{
        ActorRef guest = system.actorOf(Guest.props(system.deadLetters(), new Coffee.Akkaccino(), duration("100 millis"), -1));
        eventFilter(this, Guest.CaffeineException.class, "", 1, () -> {
            guest.tell(Guest.CoffeeFinished.Instance, ActorRef.noSender());
        });
    }};
}
 
Example #25
Source File: WaiterTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void sendingServeCoffeeShouldResultInApproveCoffeeToCoffeeHouse() {
    new JavaTestKit(system) {{
        ActorRef coffeeHouse = getRef();
        TestProbe guest = new TestProbe(system);
        ActorRef waiter = system.actorOf(Waiter.props(coffeeHouse, system.deadLetters(), Integer.MAX_VALUE));
        waiter.tell(new Waiter.ServeCoffee(new Coffee.Akkaccino()), guest.ref());
        expectMsgEquals(new CoffeeHouse.ApproveCoffee(new Coffee.Akkaccino(), guest.ref()));
    }};
}
 
Example #26
Source File: BaseAkkaTestCase.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
public ActorRef expectActor(JavaTestKit kit, String path) {
    final ActorRef[] actor = {null};
    kit.new AwaitCond(kit.duration("3 seconds"), kit.duration("150 millis"), "No actor found with " + path) {
        @Override
        protected boolean cond() {
            TestProbe probe = new TestProbe(system);
            system.actorSelection(path).tell(new akka.actor.Identify(101), probe.ref());
            ActorIdentity i = probe.expectMsgClass(kit.duration("100 millis"), ActorIdentity.class);
            actor[0] = i.getRef();
            return i.getRef() != null;
        }
    };
    return actor[0];
}
 
Example #27
Source File: CoffeeHouseAppTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateATopLevelActorCalledCoffeeHouse() {
    new JavaTestKit(system) {{
        new CoffeeHouseApp(system);
        String path = "/user/coffee-house";
        expectActor(this, path);
    }};
}
 
Example #28
Source File: CoffeeHouseTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void sendingApproveCoffeeShouldResultInLoggingStatusMessageWhenLimitReached() {
    new JavaTestKit(system) {{
        ActorRef coffeeHouse = system.actorOf(CoffeeHouse.props(1), "caffeine-limit");
        coffeeHouse.tell(new CoffeeHouse.CreateGuest(new Coffee.Akkaccino()), ActorRef.noSender());
        ActorRef guest = expectActor(this, "/user/caffeine-limit/$*");
        interceptInfoLogMessage(this, ".*[Ss]orry.*", 1, () -> coffeeHouse.tell(
                new CoffeeHouse.ApproveCoffee(new Coffee.Akkaccino(), guest), ActorRef.noSender()));
    }};
}
 
Example #29
Source File: GuestTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void serveCoffeeShouldBeSentAfterFinishCoffeeDuration() {
    new JavaTestKit(system) {{
        ActorRef guest = createGuest(this, getRef());
        new Within(duration("50 milliseconds"), duration("200 milliseconds")) {
            @Override
            protected void run() {
                guest.tell(new Waiter.CoffeeServed(new Coffee.Akkaccino()), ActorRef.noSender());
                expectMsgEquals(new Waiter.ServeCoffee(new Coffee.Akkaccino()));
            }
        };
    }};
}
 
Example #30
Source File: CoffeeHouseTest.java    From oreilly-reactive-architecture-student with Apache License 2.0 5 votes vote down vote up
@Test
public void sendingApproveCoffeeShouldResultInStoppingGuestWhenLimitReached() {
    new JavaTestKit(system) {{
        ActorRef coffeeHouse = system.actorOf(CoffeeHouse.props(1), "guest-terminated");
        coffeeHouse.tell(new CoffeeHouse.CreateGuest(new Coffee.Akkaccino(), Integer.MAX_VALUE), ActorRef.noSender());
        ActorRef guest = expectActor(this, "/user/guest-terminated/$*");
        watch(guest);
        coffeeHouse.tell(new CoffeeHouse.ApproveCoffee(new Coffee.Akkaccino(), guest), ActorRef.noSender());
        expectTerminated(guest);
    }};
}