|
| 1 | +package com.baeldung.akka; |
| 2 | + |
| 3 | +import akka.actor.ActorRef; |
| 4 | +import akka.actor.ActorSystem; |
| 5 | +import akka.actor.Props; |
| 6 | +import akka.testkit.TestKit; |
| 7 | +import org.junit.AfterClass; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import scala.concurrent.duration.Duration; |
| 12 | + |
| 13 | +import java.util.concurrent.CompletableFuture; |
| 14 | +import java.util.concurrent.ExecutionException; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | +import java.util.concurrent.TimeoutException; |
| 17 | + |
| 18 | +import static akka.pattern.PatternsCS.ask; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | +import static org.junit.Assert.fail; |
| 21 | + |
| 22 | +public class AkkaActorsUnitTest { |
| 23 | + |
| 24 | + private static ActorSystem system = null; |
| 25 | + |
| 26 | + @BeforeClass |
| 27 | + public static void setup() { |
| 28 | + system = ActorSystem.create("test-system"); |
| 29 | + } |
| 30 | + |
| 31 | + @AfterClass |
| 32 | + public static void teardown() { |
| 33 | + TestKit.shutdownActorSystem(system, Duration.apply(1000, TimeUnit.MILLISECONDS), true); |
| 34 | + system = null; |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenAnActor_sendHimAMessageUsingTell() { |
| 39 | + |
| 40 | + final TestKit probe = new TestKit(system); |
| 41 | + ActorRef myActorRef = probe.childActorOf(Props.create(MyActor.class)); |
| 42 | + myActorRef.tell("printit", probe.testActor()); |
| 43 | + |
| 44 | + probe.expectMsg("Got Message"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenAnActor_sendHimAMessageUsingAsk() throws ExecutionException, InterruptedException { |
| 49 | + |
| 50 | + final TestKit probe = new TestKit(system); |
| 51 | + ActorRef wordCounterActorRef = probe.childActorOf(Props.create(WordCounterActor.class)); |
| 52 | + |
| 53 | + CompletableFuture<Object> future = |
| 54 | + ask(wordCounterActorRef, new WordCounterActor.CountWords("this is a text"), 1000).toCompletableFuture(); |
| 55 | + |
| 56 | + Integer numberOfWords = (Integer) future.get(); |
| 57 | + assertTrue("The actor should count 4 words", 4 == numberOfWords); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void givenAnActor_whenTheMessageIsNull_respondWithException() { |
| 62 | + final TestKit probe = new TestKit(system); |
| 63 | + ActorRef wordCounterActorRef = probe.childActorOf(Props.create(WordCounterActor.class)); |
| 64 | + |
| 65 | + CompletableFuture<Object> future = |
| 66 | + ask(wordCounterActorRef, new WordCounterActor.CountWords(null), 1000).toCompletableFuture(); |
| 67 | + |
| 68 | + try { |
| 69 | + future.get(1000, TimeUnit.MILLISECONDS); |
| 70 | + } catch (ExecutionException e) { |
| 71 | + assertTrue("Invalid error message", e.getMessage().contains("The text to process can't be null!")); |
| 72 | + } catch (InterruptedException | TimeoutException e) { |
| 73 | + fail("Actor should respond with an exception instead of timing out !"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void givenAnAkkaSystem_countTheWordsInAText() { |
| 79 | + ActorSystem system = ActorSystem.create("test-system"); |
| 80 | + ActorRef myActorRef = system.actorOf(Props.create(MyActor.class), "my-actor"); |
| 81 | + myActorRef.tell("printit", null); |
| 82 | +// system.stop(myActorRef); |
| 83 | +// myActorRef.tell(PoisonPill.getInstance(), ActorRef.noSender()); |
| 84 | +// myActorRef.tell(Kill.getInstance(), ActorRef.noSender()); |
| 85 | + |
| 86 | + ActorRef readingActorRef = system.actorOf(ReadingActor.props(TEXT), "readingActor"); |
| 87 | + readingActorRef.tell(new ReadingActor.ReadLines(), ActorRef.noSender()); //ActorRef.noSender() means the sender ref is akka://test-system/deadLetters |
| 88 | + |
| 89 | +// Future<Terminated> terminateResponse = system.terminate(); |
| 90 | + } |
| 91 | + |
| 92 | + private static String TEXT = "Lorem Ipsum is simply dummy text\n" + |
| 93 | + "of the printing and typesetting industry.\n" + |
| 94 | + "Lorem Ipsum has been the industry's standard dummy text\n" + |
| 95 | + "ever since the 1500s, when an unknown printer took a galley\n" + |
| 96 | + "of type and scrambled it to make a type specimen book.\n" + |
| 97 | + " It has survived not only five centuries, but also the leap\n" + |
| 98 | + "into electronic typesetting, remaining essentially unchanged.\n" + |
| 99 | + " It was popularised in the 1960s with the release of Letraset\n" + |
| 100 | + " sheets containing Lorem Ipsum passages, and more recently with\n" + |
| 101 | + " desktop publishing software like Aldus PageMaker including\n" + |
| 102 | + "versions of Lorem Ipsum."; |
| 103 | + |
| 104 | +} |
0 commit comments