Search in sources :

Example 1 with EmbeddedActor

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);
}
Also used : ActorSystem(actor4j.core.ActorSystem) CountDownLatch(java.util.concurrent.CountDownLatch) EmbeddedActor(actor4j.core.actors.EmbeddedActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmbeddedActor(actor4j.core.actors.EmbeddedActor) Actor(actor4j.core.actors.Actor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ActorSystem (actor4j.core.ActorSystem)1 Actor (actor4j.core.actors.Actor)1 EmbeddedActor (actor4j.core.actors.EmbeddedActor)1 ActorMessage (actor4j.core.messages.ActorMessage)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1