use of actor4j.core.utils.ActorFactory in project actor4j-core by relvaner.
the class BehaviourFeature method test_become.
@Test(timeout = 2000)
public void test_become() {
CountDownLatch testDone = new CountDownLatch(1);
final AtomicBoolean behaviour = new AtomicBoolean(false);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor() {
protected Consumer<ActorMessage<?>> newBehaviour = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
behaviour.set(true);
testDone.countDown();
}
};
@Override
public void receive(ActorMessage<?> message) {
become(newBehaviour);
}
};
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
assertEquals(true, behaviour.get());
}
use of actor4j.core.utils.ActorFactory in project actor4j-core by relvaner.
the class BehaviourFeature method test_stack_become_unbecome.
@Test(timeout = 2000)
public void test_stack_become_unbecome() {
CountDownLatch testDone = new CountDownLatch(1);
final AtomicBoolean[] behaviour = new AtomicBoolean[2];
for (int i = 0; i < behaviour.length; i++) behaviour[i] = new AtomicBoolean(false);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor() {
protected Consumer<ActorMessage<?>> newBehaviour1 = new Consumer<ActorMessage<?>>() {
protected boolean first = true;
@Override
public void accept(ActorMessage<?> t) {
if (first) {
become(newBehaviour2, false);
first = false;
} else {
behaviour[1].set(true);
testDone.countDown();
}
}
};
protected Consumer<ActorMessage<?>> newBehaviour2 = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
behaviour[0].set(true);
unbecome();
}
};
@Override
public void receive(ActorMessage<?> message) {
become(newBehaviour1);
}
};
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
assertEquals(true, behaviour[0].get());
assertEquals(true, behaviour[1].get());
}
use of actor4j.core.utils.ActorFactory in project actor4j-core by relvaner.
the class SafetyFeature method test.
@Test(timeout = 2000)
public void test() {
CountDownLatch testDone = new CountDownLatch(1);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor("SafetyFeatureActor") {
@Override
public void receive(ActorMessage<?> message) {
throw new NullPointerException();
}
@Override
public void preRestart(Exception reason) {
super.preRestart(reason);
assertEquals(new NullPointerException().getMessage(), reason.getMessage());
}
@Override
public void postRestart(Exception reason) {
super.postRestart(reason);
assertEquals(new NullPointerException().getMessage(), reason.getMessage());
}
};
}
});
ErrorHandler errorHandler = system.underlyingImpl().getExecuterService().getSafetyManager().getErrorHandler();
system.underlyingImpl().getExecuterService().getSafetyManager().setErrorHandler(new ErrorHandler() {
@Override
public void handle(Throwable t, String message, UUID uuid) {
errorHandler.handle(t, message, uuid);
assertEquals(new NullPointerException().getMessage(), t.getMessage());
assertEquals("actor", message);
assertEquals(dest, uuid);
testDone.countDown();
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
}
Aggregations