2012-01-05 17:59:19 +01:00
|
|
|
/**
|
2013-01-09 01:47:48 +01:00
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
2012-01-05 17:59:19 +01:00
|
|
|
*/
|
2012-05-22 11:37:09 +02:00
|
|
|
package docs.jrouting;
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
import akka.routing.FromConfig;
|
|
|
|
|
import akka.routing.RoundRobinRoutingLogic;
|
|
|
|
|
import akka.routing.Routee;
|
|
|
|
|
import akka.routing.RoutingLogic;
|
|
|
|
|
import akka.routing.SeveralRoutees;
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
|
|
import com.typesafe.config.ConfigFactory;
|
|
|
|
|
|
|
|
|
|
import scala.collection.immutable.IndexedSeq;
|
|
|
|
|
import static akka.japi.Util.immutableIndexedSeq;
|
|
|
|
|
import docs.jrouting.RouterDocTest.Parent;
|
|
|
|
|
import docs.jrouting.RouterDocTest.Workers;
|
|
|
|
|
import docs.routing.CustomRouterDocSpec;
|
|
|
|
|
import akka.testkit.JavaTestKit;
|
2012-09-19 23:55:53 +02:00
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.Props;
|
2013-09-19 08:00:05 +02:00
|
|
|
//#imports1
|
2012-09-19 23:55:53 +02:00
|
|
|
import akka.actor.UntypedActor;
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
//#imports1
|
|
|
|
|
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-05-08 09:42:25 +02:00
|
|
|
public class CustomRouterDocTest {
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
2013-09-19 08:00:05 +02:00
|
|
|
new AkkaJUnitActorSystemResource("CustomRouterDocTest",
|
|
|
|
|
ConfigFactory.parseString(CustomRouterDocSpec.jconfig()));
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
static
|
|
|
|
|
//#routing-logic
|
|
|
|
|
public class RedundancyRoutingLogic implements RoutingLogic {
|
|
|
|
|
private final int nbrCopies;
|
|
|
|
|
|
|
|
|
|
public RedundancyRoutingLogic(int nbrCopies) {
|
|
|
|
|
this.nbrCopies = nbrCopies;
|
|
|
|
|
}
|
|
|
|
|
RoundRobinRoutingLogic roundRobin = new RoundRobinRoutingLogic();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Routee select(Object message, IndexedSeq<Routee> routees) {
|
|
|
|
|
List<Routee> targets = new ArrayList<Routee>();
|
|
|
|
|
for (int i = 0; i < nbrCopies; i++) {
|
|
|
|
|
targets.add(roundRobin.select(message, routees));
|
|
|
|
|
}
|
|
|
|
|
return new SeveralRoutees(targets);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#routing-logic
|
2012-02-10 14:13:40 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
static
|
|
|
|
|
//#unit-test-logic
|
|
|
|
|
public final class TestRoutee implements Routee {
|
|
|
|
|
public final int n;
|
|
|
|
|
|
|
|
|
|
public TestRoutee(int n) {
|
|
|
|
|
this.n = n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void send(Object message, ActorRef sender) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int hashCode() {
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
|
return (obj instanceof TestRoutee) &&
|
|
|
|
|
n == ((TestRoutee) obj).n;
|
|
|
|
|
}
|
2012-02-10 14:13:40 +01:00
|
|
|
}
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
//#unit-test-logic
|
2012-02-10 14:13:40 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
static public class Storage extends UntypedActor {
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
getSender().tell(msg, getSelf());
|
|
|
|
|
}
|
2012-02-10 14:13:40 +01:00
|
|
|
}
|
2012-02-18 22:15:39 +01:00
|
|
|
|
|
|
|
|
@Test
|
2013-09-19 08:00:05 +02:00
|
|
|
public void unitTestRoutingLogic() {
|
|
|
|
|
//#unit-test-logic
|
|
|
|
|
RedundancyRoutingLogic logic = new RedundancyRoutingLogic(3);
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
List<Routee> routeeList = new ArrayList<Routee>();
|
|
|
|
|
for (int n = 1; n <= 7; n++) {
|
|
|
|
|
routeeList.add(new TestRoutee(n));
|
|
|
|
|
}
|
|
|
|
|
IndexedSeq<Routee> routees = immutableIndexedSeq(routeeList);
|
|
|
|
|
|
|
|
|
|
SeveralRoutees r1 = (SeveralRoutees) logic.select("msg", routees);
|
|
|
|
|
assertEquals(r1.getRoutees().get(0), routeeList.get(0));
|
|
|
|
|
assertEquals(r1.getRoutees().get(1), routeeList.get(1));
|
|
|
|
|
assertEquals(r1.getRoutees().get(2), routeeList.get(2));
|
|
|
|
|
|
|
|
|
|
SeveralRoutees r2 = (SeveralRoutees) logic.select("msg", routees);
|
|
|
|
|
assertEquals(r2.getRoutees().get(0), routeeList.get(3));
|
|
|
|
|
assertEquals(r2.getRoutees().get(1), routeeList.get(4));
|
|
|
|
|
assertEquals(r2.getRoutees().get(2), routeeList.get(5));
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
SeveralRoutees r3 = (SeveralRoutees) logic.select("msg", routees);
|
|
|
|
|
assertEquals(r3.getRoutees().get(0), routeeList.get(6));
|
|
|
|
|
assertEquals(r3.getRoutees().get(1), routeeList.get(0));
|
|
|
|
|
assertEquals(r3.getRoutees().get(2), routeeList.get(1));
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
//#unit-test-logic
|
2012-01-05 17:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
@Test
|
|
|
|
|
public void demonstrateUsageOfCustomRouter() {
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
//#usage-1
|
|
|
|
|
for (int n = 1; n <= 10; n++) {
|
|
|
|
|
system.actorOf(Props.create(Storage.class), "s" + n);
|
|
|
|
|
}
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
List<String> paths = new ArrayList<String>();
|
|
|
|
|
for (int n = 1; n <= 10; n++) {
|
|
|
|
|
paths.add("/user/s" + n);
|
2012-01-05 17:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
ActorRef redundancy1 =
|
|
|
|
|
system.actorOf(new RedundancyGroup(paths, 3).props(),
|
|
|
|
|
"redundancy1");
|
|
|
|
|
redundancy1.tell("important", getTestActor());
|
|
|
|
|
//#usage-1
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
|
expectMsgEquals("important");
|
2012-01-05 17:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
//#usage-2
|
|
|
|
|
ActorRef redundancy2 = system.actorOf(FromConfig.getInstance().props(),
|
|
|
|
|
"redundancy2");
|
|
|
|
|
redundancy2.tell("very important", getTestActor());
|
|
|
|
|
//#usage-2
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
|
expectMsgEquals("very important");
|
|
|
|
|
}
|
|
|
|
|
}};
|
2012-01-05 17:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|