use of actor4j.core.actors.EmbeddedActor in project actor4j-core by relvaner.
the class EmbeddedActorFeature method test_become_unbecome.
@Test(timeout = 2000)
public void test_become_unbecome() {
CountDownLatch testDone = new CountDownLatch(2);
AtomicInteger counter = new AtomicInteger(0);
ActorSystem system = new ActorSystem();
UUID host = system.addActor(() -> new Actor("host") {
protected EmbeddedActor client;
@Override
public void preStart() {
client = new EmbeddedActor("host:client", this) {
@Override
public boolean receive(ActorMessage<?> message) {
boolean result = false;
if (message.tag == SWAP) {
become(msg -> {
logger().debug(String.format("Received String message: %s", msg.valueAsString()));
unbecome();
if (counter.incrementAndGet() == 1)
assertEquals("Hello World!", msg.valueAsString());
else
assertEquals("Hello World Again!", msg.valueAsString());
testDone.countDown();
return true;
}, false);
result = true;
}
return result;
}
};
}
@Override
public void receive(ActorMessage<?> message) {
if (!client.embedded(message))
unhandled(message);
}
});
system.send(new ActorMessage<Object>(null, SWAP, system.SYSTEM_ID, host));
system.send(new ActorMessage<Object>("Hello World!", 0, system.SYSTEM_ID, host));
system.send(new ActorMessage<Object>(null, SWAP, system.SYSTEM_ID, host));
system.send(new ActorMessage<Object>("Hello World Again!", 0, system.SYSTEM_ID, host));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdownWithActors(true);
}
Aggregations