Search in sources :

Example 1 with TestKit

use of akka.testkit.TestKit in project controller by opendaylight.

the class MeteredBoundedMailboxTest method shouldSendMsgToDeadLetterWhenQueueIsFull.

@Test
public void shouldSendMsgToDeadLetterWhenQueueIsFull() throws InterruptedException {
    final TestKit mockReceiver = new TestKit(actorSystem);
    actorSystem.eventStream().subscribe(mockReceiver.testActor(), DeadLetter.class);
    final FiniteDuration twentySeconds = new FiniteDuration(20, TimeUnit.SECONDS);
    ActorRef pingPongActor = actorSystem.actorOf(PingPongActor.props(lock).withMailbox(config.getMailBoxName()), "pingpongactor");
    actorSystem.mailboxes().settings();
    lock.lock();
    try {
        // 12th message is sent to dead letter.
        for (int i = 0; i < 12; i++) {
            pingPongActor.tell("ping", mockReceiver.testActor());
        }
        mockReceiver.expectMsgClass(twentySeconds, DeadLetter.class);
    } finally {
        lock.unlock();
    }
    mockReceiver.receiveN(11, twentySeconds);
}
Also used : ActorRef(akka.actor.ActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) TestKit(akka.testkit.TestKit) Test(org.junit.Test)

Example 2 with TestKit

use of akka.testkit.TestKit in project tutorials by eugenp.

the class AkkaActorsUnitTest method givenAnActor_sendHimAMessageUsingTell.

@Test
public void givenAnActor_sendHimAMessageUsingTell() {
    final TestKit probe = new TestKit(system);
    ActorRef myActorRef = probe.childActorOf(Props.create(MyActor.class));
    myActorRef.tell("printit", probe.testActor());
    probe.expectMsg("Got Message");
}
Also used : ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.TestKit) Test(org.junit.Test)

Example 3 with TestKit

use of akka.testkit.TestKit in project tutorials by eugenp.

the class AkkaActorsUnitTest method givenAnActor_sendHimAMessageUsingAsk.

@Test
public void givenAnActor_sendHimAMessageUsingAsk() throws ExecutionException, InterruptedException {
    final TestKit probe = new TestKit(system);
    ActorRef wordCounterActorRef = probe.childActorOf(Props.create(WordCounterActor.class));
    CompletableFuture<Object> future = ask(wordCounterActorRef, new WordCounterActor.CountWords("this is a text"), 1000).toCompletableFuture();
    Integer numberOfWords = (Integer) future.get();
    assertTrue("The actor should count 4 words", 4 == numberOfWords);
}
Also used : ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.TestKit) Test(org.junit.Test)

Example 4 with TestKit

use of akka.testkit.TestKit in project tutorials by eugenp.

the class AkkaActorsUnitTest method givenAnActor_whenTheMessageIsNull_respondWithException.

@Test
public void givenAnActor_whenTheMessageIsNull_respondWithException() {
    final TestKit probe = new TestKit(system);
    ActorRef wordCounterActorRef = probe.childActorOf(Props.create(WordCounterActor.class));
    CompletableFuture<Object> future = ask(wordCounterActorRef, new WordCounterActor.CountWords(null), 1000).toCompletableFuture();
    try {
        future.get(1000, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        assertTrue("Invalid error message", e.getMessage().contains("The text to process can't be null!"));
    } catch (InterruptedException | TimeoutException e) {
        fail("Actor should respond with an exception instead of timing out !");
    }
}
Also used : ActorRef(akka.actor.ActorRef) TestKit(akka.testkit.TestKit) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

ActorRef (akka.actor.ActorRef)4 TestKit (akka.testkit.TestKit)4 Test (org.junit.Test)4 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 FiniteDuration (scala.concurrent.duration.FiniteDuration)1