Skip to content

Instantly share code, notes, and snippets.

@hardbyte
Created October 26, 2017 03:10
Show Gist options
  • Save hardbyte/ac34085cf3789f0983ffc6ba54b1756b to your computer and use it in GitHub Desktop.
Save hardbyte/ac34085cf3789f0983ffc6ba54b1756b to your computer and use it in GitHub Desktop.
Stateless Akka Prime Example
package com.lightbend.akka.sample;
import akka.actor.AbstractLoggingActor;
import akka.actor.Props;
import java.util.HashMap;
import java.util.Map;
class StatelessPrime {
static class IsPrimeActor extends AbstractLoggingActor {
// Define the actor protocol
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;
}
}
public static Props props() {
return Props.create(IsPrimeActor.class);
}
Receive waitingForCandidates(Map<Long, Boolean> knownPrimes) {
return receiveBuilder()
.match(IsPrimeRequest.class, r -> {
long candidate = r.candidate;
Boolean isPrime = knownPrimes.get(candidate);
if (isPrime == null) {
isPrime = isPrime(candidate);
}
knownPrimes.put(candidate, isPrime);
getSender().tell(new IsPrimeResponse(candidate, isPrime), getSelf());
getContext().become(waitingForCandidates(knownPrimes));
})
.build();
}
@Override
public Receive createReceive() {
return waitingForCandidates(new HashMap<Long, Boolean>());
}
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;
}
}
}
}
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 StatelessPrimeTest {
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(StatelessPrime.IsPrimeActor.props());
primeActor.tell(new StatelessPrime.IsPrimeActor.IsPrimeRequest(42L), probe.getRef());
StatelessPrime.IsPrimeActor.IsPrimeResponse response = probe.expectMsgClass(StatelessPrime.IsPrimeActor.IsPrimeResponse.class);
assertEquals(42L, response.candidate);
assertEquals(false, response.isPrime);
}
@Test
public void testReplyWithMultipleCandidates() {
TestKit probe = new TestKit(system);
ActorRef primeActor = system.actorOf(StatelessPrime.IsPrimeActor.props());
for (long i = 0; i < 10000; i++) {
primeActor.tell(new StatelessPrime.IsPrimeActor.IsPrimeRequest(i), probe.getRef());
StatelessPrime.IsPrimeActor.IsPrimeResponse response = probe.expectMsgClass(StatelessPrime.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