pekko/akka-docs/rst/java/code/docs/actor/FaultHandlingTestBase.java

224 lines
6.1 KiB
Java
Raw Normal View History

/**
2012-01-19 18:21:06 +01:00
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
*/
package docs.actor;
2011-12-15 23:33:15 +01:00
//#testkit
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.SupervisorStrategy;
import static akka.actor.SupervisorStrategy.*;
2011-12-15 23:33:15 +01:00
import akka.actor.OneForOneStrategy;
import akka.actor.Props;
import akka.actor.Terminated;
import akka.actor.UntypedActor;
import scala.concurrent.Await;
import static akka.pattern.Patterns.ask;
import scala.concurrent.util.Duration;
2011-12-15 23:33:15 +01:00
import akka.testkit.AkkaSpec;
import akka.testkit.TestProbe;
//#testkit
import akka.testkit.ErrorFilter;
import akka.testkit.EventFilter;
import akka.testkit.TestEvent;
import static java.util.concurrent.TimeUnit.SECONDS;
import akka.japi.Function;
import scala.Option;
import scala.collection.JavaConverters;
import scala.collection.Seq;
import org.junit.Test;
import org.junit.BeforeClass;
import org.junit.AfterClass;
//#testkit
public class FaultHandlingTestBase {
//#testkit
static
2011-12-15 23:33:15 +01:00
//#supervisor
public class Supervisor extends UntypedActor {
//#strategy
private static SupervisorStrategy strategy =
new OneForOneStrategy(10, Duration.parse("1 minute"),
new Function<Throwable, Directive>() {
@Override
public Directive apply(Throwable t) {
if (t instanceof ArithmeticException) {
return resume();
} else if (t instanceof NullPointerException) {
return restart();
} else if (t instanceof IllegalArgumentException) {
return stop();
} else {
return escalate();
}
}
});
@Override
public SupervisorStrategy supervisorStrategy() {
return strategy;
}
//#strategy
2011-12-15 23:33:15 +01:00
public void onReceive(Object o) {
if (o instanceof Props) {
getSender().tell(getContext().actorOf((Props) o), getSelf());
} else {
unhandled(o);
2011-12-15 23:33:15 +01:00
}
}
}
2011-12-15 23:33:15 +01:00
//#supervisor
static
2011-12-15 23:33:15 +01:00
//#supervisor2
public class Supervisor2 extends UntypedActor {
//#strategy2
private static SupervisorStrategy strategy = new OneForOneStrategy(10,
Duration.parse("1 minute"),
new Function<Throwable, Directive>() {
@Override
public Directive apply(Throwable t) {
if (t instanceof ArithmeticException) {
return resume();
} else if (t instanceof NullPointerException) {
return restart();
} else if (t instanceof IllegalArgumentException) {
return stop();
} else {
return escalate();
}
}
});
@Override
public SupervisorStrategy supervisorStrategy() {
return strategy;
}
//#strategy2
2011-12-15 23:33:15 +01:00
public void onReceive(Object o) {
if (o instanceof Props) {
getSender().tell(getContext().actorOf((Props) o), getSelf());
} else {
unhandled(o);
2011-12-15 23:33:15 +01:00
}
}
2011-12-15 23:33:15 +01:00
@Override
public void preRestart(Throwable cause, Option<Object> msg) {
// do not kill all children, which is the default here
}
}
2011-12-15 23:33:15 +01:00
//#supervisor2
static
2011-12-15 23:33:15 +01:00
//#child
public class Child extends UntypedActor {
2011-12-15 23:33:15 +01:00
int state = 0;
2011-12-15 23:33:15 +01:00
public void onReceive(Object o) throws Exception {
if (o instanceof Exception) {
throw (Exception) o;
} else if (o instanceof Integer) {
state = (Integer) o;
} else if (o.equals("get")) {
getSender().tell(state, getSelf());
} else {
unhandled(o);
2011-12-15 23:33:15 +01:00
}
}
}
2011-12-15 23:33:15 +01:00
//#child
2011-12-15 23:33:15 +01:00
//#testkit
static ActorSystem system;
Duration timeout = Duration.create(5, SECONDS);
2011-12-15 23:33:15 +01:00
@BeforeClass
public static void start() {
system = ActorSystem.create("test", AkkaSpec.testConf());
}
2011-12-15 23:33:15 +01:00
@AfterClass
public static void cleanup() {
system.shutdown();
}
2011-12-15 23:33:15 +01:00
@Test
public void mustEmploySupervisorStrategy() throws Exception {
2011-12-15 23:33:15 +01:00
// code here
//#testkit
EventFilter ex1 = (EventFilter) new ErrorFilter(ArithmeticException.class);
EventFilter ex2 = (EventFilter) new ErrorFilter(NullPointerException.class);
EventFilter ex3 = (EventFilter) new ErrorFilter(IllegalArgumentException.class);
EventFilter ex4 = (EventFilter) new ErrorFilter(Exception.class);
Seq<EventFilter> ignoreExceptions = seq(ex1, ex2, ex3, ex4);
system.eventStream().publish(new TestEvent.Mute(ignoreExceptions));
2011-12-15 23:33:15 +01:00
//#create
Props superprops = new Props(Supervisor.class);
2011-12-15 23:33:15 +01:00
ActorRef supervisor = system.actorOf(superprops, "supervisor");
ActorRef child = (ActorRef) Await.result(ask(supervisor,
new Props(Child.class), 5000), timeout);
2011-12-15 23:33:15 +01:00
//#create
2011-12-15 23:33:15 +01:00
//#resume
child.tell(42, null);
2011-12-17 17:18:50 -08:00
assert Await.result(ask(child, "get", 5000), timeout).equals(42);
child.tell(new ArithmeticException(), null);
2011-12-17 17:18:50 -08:00
assert Await.result(ask(child, "get", 5000), timeout).equals(42);
2011-12-15 23:33:15 +01:00
//#resume
2011-12-15 23:33:15 +01:00
//#restart
child.tell(new NullPointerException(), null);
2011-12-17 17:18:50 -08:00
assert Await.result(ask(child, "get", 5000), timeout).equals(0);
2011-12-15 23:33:15 +01:00
//#restart
2011-12-15 23:33:15 +01:00
//#stop
final TestProbe probe = new TestProbe(system);
probe.watch(child);
child.tell(new IllegalArgumentException(), null);
2012-08-20 11:22:52 +02:00
probe.expectMsgClass(Terminated.class);
2011-12-15 23:33:15 +01:00
//#stop
2011-12-15 23:33:15 +01:00
//#escalate-kill
child = (ActorRef) Await.result(ask(supervisor,
new Props(Child.class), 5000), timeout);
2011-12-15 23:33:15 +01:00
probe.watch(child);
2011-12-17 17:18:50 -08:00
assert Await.result(ask(child, "get", 5000), timeout).equals(0);
child.tell(new Exception(), null);
2012-08-20 11:22:52 +02:00
probe.expectMsgClass(Terminated.class);
2011-12-15 23:33:15 +01:00
//#escalate-kill
2011-12-15 23:33:15 +01:00
//#escalate-restart
superprops = new Props(Supervisor2.class);
supervisor = system.actorOf(superprops);
child = (ActorRef) Await.result(ask(supervisor,
new Props(Child.class), 5000), timeout);
child.tell(23, null);
2011-12-17 17:18:50 -08:00
assert Await.result(ask(child, "get", 5000), timeout).equals(23);
child.tell(new Exception(), null);
2011-12-17 17:18:50 -08:00
assert Await.result(ask(child, "get", 5000), timeout).equals(0);
2011-12-15 23:33:15 +01:00
//#escalate-restart
//#testkit
}
//#testkit
2011-12-15 23:33:15 +01:00
public <A> Seq<A> seq(A... args) {
return JavaConverters.collectionAsScalaIterableConverter(
java.util.Arrays.asList(args)).asScala().toSeq();
2011-12-15 23:33:15 +01:00
}
//#testkit
2011-12-15 23:33:15 +01:00
}
//#testkit