Created
October 26, 2017 03:34
-
-
Save hardbyte/76bd5fbc888fce87c51a5fa7a808be8c to your computer and use it in GitHub Desktop.
Untyped Simple Prime Akka example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lightbend.akka.sample; | |
import akka.actor.Props; | |
import akka.actor.UntypedAbstractActor; | |
class UntyptedSimplePrime { | |
static class IsPrimeActor extends UntypedAbstractActor { | |
// Define the messages | |
static class IsPrimeRequest { | |
long candidate; | |
IsPrimeRequest(long candidate){this.candidate = candidate;} | |
} | |
static class IsPrimeResponse { | |
long candidate; | |
boolean isPrime; | |
IsPrimeResponse(long candidate, boolean isPrime){ | |
this.candidate = candidate; | |
this.isPrime = isPrime; | |
} | |
} | |
IsPrimeResponse onPrimeRequest(IsPrimeRequest m) { | |
return new IsPrimeResponse(m.candidate, isPrime(m.candidate)); | |
} | |
@Override | |
public void onReceive(Object message) throws Throwable { | |
IsPrimeResponse reply = onPrimeRequest(IsPrimeRequest.class.cast(message)); | |
getSender().tell(reply, getSelf()); | |
} | |
private boolean isPrime(long n) { | |
if (n <= 3) { | |
return n > 1; | |
} else if (n % 2 == 0 || n % 3 == 0) { | |
return false; | |
} else { | |
for (int i = 5; i * i <= n; i += 6) { | |
if (n % i == 0 || n % (i + 2) == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
public static Props props() { | |
return Props.create(IsPrimeActor.class); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lightbend.akka.sample; | |
import akka.actor.ActorRef; | |
import akka.actor.ActorSystem; | |
import akka.testkit.javadsl.TestKit; | |
import org.junit.AfterClass; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
public class UntyptedSimplePrimeTest { | |
static ActorSystem system; | |
@BeforeClass | |
public static void setup() { | |
system = ActorSystem.create(); | |
} | |
@AfterClass | |
public static void teardown() { | |
TestKit.shutdownActorSystem(system); | |
system = null; | |
} | |
@Test | |
public void testReplyWithSmallCandidates() { | |
TestKit probe = new TestKit(system); | |
ActorRef primeActor = system.actorOf(UntyptedSimplePrime.IsPrimeActor.props()); | |
primeActor.tell(new UntyptedSimplePrime.IsPrimeActor.IsPrimeRequest(42L), probe.getRef()); | |
UntyptedSimplePrime.IsPrimeActor.IsPrimeResponse response = probe.expectMsgClass(UntyptedSimplePrime.IsPrimeActor.IsPrimeResponse.class); | |
assertEquals(42L, response.candidate); | |
assertEquals(false, response.isPrime); | |
} | |
@Test | |
public void testReplyWithMultipleCandidates() { | |
TestKit probe = new TestKit(system); | |
ActorRef primeActor = system.actorOf(UntyptedSimplePrime.IsPrimeActor.props()); | |
for (long i = 0; i < 10000; i++) { | |
primeActor.tell(new UntyptedSimplePrime.IsPrimeActor.IsPrimeRequest(i), probe.getRef()); | |
UntyptedSimplePrime.IsPrimeActor.IsPrimeResponse response = probe.expectMsgClass(UntyptedSimplePrime.IsPrimeActor.IsPrimeResponse.class); | |
assertEquals(i, response.candidate); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment