Search in sources :

Example 1 with ChildSpec

use of co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec in project quasar by puniverse.

the class SupervisorTest method whenChildDiesThenRestart.

@Test
public void whenChildDiesThenRestart() throws Exception {
    final Supervisor sup = new SupervisorActor(RestartStrategy.ONE_FOR_ONE, new ChildSpec("actor1", ChildMode.PERMANENT, 5, 1, TimeUnit.SECONDS, 3, ActorSpec.of(Actor1.class, "actor1"))).spawn(factory);
    ActorRef<Object> a;
    a = getChild(sup, "actor1", 1000);
    for (int i = 0; i < 3; i++) a.send(1);
    a.send(new ShutdownMessage(null));
    assertThat(LocalActor.<Integer>get(a), is(3));
    a = getChild(sup, "actor1", 1000);
    for (int i = 0; i < 5; i++) a.send(1);
    a.send(new ShutdownMessage(null));
    assertThat(LocalActor.<Integer>get(a), is(5));
    sup.shutdown();
    LocalActor.join(sup);
}
Also used : ShutdownMessage(co.paralleluniverse.actors.ShutdownMessage) ChildSpec(co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec) Test(org.junit.Test)

Example 2 with ChildSpec

use of co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec in project quasar by puniverse.

the class SupervisorTest method restartTransientChildDeadOfUnnaturalCause.

@Test
public void restartTransientChildDeadOfUnnaturalCause() throws Exception {
    final Supervisor sup = new SupervisorActor(RestartStrategy.ONE_FOR_ONE, new ChildSpec("actor1", ChildMode.TRANSIENT, 5, 1, TimeUnit.SECONDS, 3, ActorSpec.of(BadActor1.class, "actor1"))).spawn();
    ActorRef<Object> a;
    a = getChild(sup, "actor1", 1000);
    for (int i = 0; i < 3; i++) a.send(1);
    a.send(new ShutdownMessage(null));
    try {
        LocalActor.join(a);
        fail();
    } catch (ExecutionException e) {
    }
    ActorRef<Object> b = getChild(sup, "actor1", 200);
    assertThat(b, not(nullValue()));
    assertThat(b, not(equalTo(a)));
    List<ActorRef<Object>> bcs = getChildren(sup);
    assertThat(bcs.get(0), not(nullValue()));
    assertThat(bcs.get(0), not(equalTo(a)));
    assertEquals(bcs.size(), 1);
    sup.shutdown();
    LocalActor.join(sup);
}
Also used : ShutdownMessage(co.paralleluniverse.actors.ShutdownMessage) ChildSpec(co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec) ActorRef(co.paralleluniverse.actors.ActorRef) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with ChildSpec

use of co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec in project quasar by puniverse.

the class SupervisorTest method dontRestartTemporaryChildDeadOfUnnaturalCause.

@Test
public void dontRestartTemporaryChildDeadOfUnnaturalCause() throws Exception {
    final Supervisor sup = new SupervisorActor(RestartStrategy.ONE_FOR_ONE, new ChildSpec("actor1", ChildMode.TEMPORARY, 5, 1, TimeUnit.SECONDS, 3, ActorSpec.of(BadActor1.class, "actor1"))).spawn(factory);
    ActorRef<Object> a;
    a = getChild(sup, "actor1", 1000);
    for (int i = 0; i < 3; i++) a.send(1);
    a.send(new ShutdownMessage(null));
    try {
        LocalActor.join(a);
        fail();
    } catch (ExecutionException e) {
    }
    a = getChild(sup, "actor1", 200);
    assertThat(a, nullValue());
    sup.shutdown();
    LocalActor.join(sup);
}
Also used : ShutdownMessage(co.paralleluniverse.actors.ShutdownMessage) ChildSpec(co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 4 with ChildSpec

use of co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec in project quasar by puniverse.

the class SupervisorTest method dontRestartTemporaryChildDeadOfNaturalCause.

@Test
public void dontRestartTemporaryChildDeadOfNaturalCause() throws Exception {
    final Supervisor sup = new SupervisorActor(RestartStrategy.ONE_FOR_ONE, new ChildSpec("actor1", ChildMode.TEMPORARY, 5, 1, TimeUnit.SECONDS, 3, ActorSpec.of(Actor1.class, "actor1"))).spawn();
    ActorRef<Object> a;
    a = getChild(sup, "actor1", 1000);
    for (int i = 0; i < 3; i++) a.send(1);
    a.send(new ShutdownMessage(null));
    assertThat(LocalActor.<Integer>get(a), is(3));
    a = getChild(sup, "actor1", 200);
    assertThat(a, nullValue());
    List<ActorRef<Object>> cs = getChildren(sup);
    assertEquals(cs.size(), 0);
    sup.shutdown();
    LocalActor.join(sup);
}
Also used : ShutdownMessage(co.paralleluniverse.actors.ShutdownMessage) ChildSpec(co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec) ActorRef(co.paralleluniverse.actors.ActorRef) Test(org.junit.Test)

Example 5 with ChildSpec

use of co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec in project quasar by puniverse.

the class SupervisorTest method restartPreInstantiatedChild.

@Test
public void restartPreInstantiatedChild() throws Exception {
    final Supervisor sup = new SupervisorActor(RestartStrategy.ONE_FOR_ONE).spawn();
    final ActorRef<Object> a1 = new Actor2("actor1").spawn();
    sup.addChild(new ChildSpec("actor1", ChildMode.PERMANENT, 5, 1, TimeUnit.SECONDS, 3, a1));
    ActorRef<Object> a;
    a = getChild(sup, "actor1", 1);
    assertThat(a, equalTo(a1));
    for (int i = 0; i < 3; i++) a.send(1);
    a.send(new ShutdownMessage(null));
    assertThat(LocalActor.<Integer>get(a), is(3));
    a = getChild(sup, "actor1", 1000);
    assertThat(a, is(not(equalTo(a1))));
    for (int i = 0; i < 5; i++) a.send(1);
    a.send(new ShutdownMessage(null));
    assertThat(LocalActor.<Integer>get(a), is(5));
    sup.shutdown();
    LocalActor.join(sup);
}
Also used : ShutdownMessage(co.paralleluniverse.actors.ShutdownMessage) ChildSpec(co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec) Test(org.junit.Test)

Aggregations

ChildSpec (co.paralleluniverse.actors.behaviors.Supervisor.ChildSpec)8 Test (org.junit.Test)8 ShutdownMessage (co.paralleluniverse.actors.ShutdownMessage)7 ExecutionException (java.util.concurrent.ExecutionException)3 ActorRef (co.paralleluniverse.actors.ActorRef)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1