Search in sources :

Example 1 with PrimaryActor

use of actor4j.core.actors.PrimaryActor in project actor4j-core by relvaner.

the class PrimarySecondaryActorFeature method test.

@Test(timeout = 2000)
public void test() {
    ActorSystem system = new ActorSystem();
    CountDownLatch testDone = new CountDownLatch(system.getParallelismMin() * system.getParallelismFactor());
    AtomicBoolean primaryReceivedFromSystem = new AtomicBoolean(false);
    AtomicInteger secondaryReceived = new AtomicInteger(0);
    AtomicBoolean primaryReceived = new AtomicBoolean(false);
    ConcurrentActorGroup group = new ConcurrentActorGroup();
    UUID primary = system.addActor(() -> new PrimaryActor("primary", group, "instances", (id) -> () -> new SecondaryActor(group, id) {

        @Override
        public void receive(ActorMessage<?> message) {
            if (message.source == primary) {
                secondaryReceived.incrementAndGet();
                testDone.countDown();
            } else if (message.source == system.SYSTEM_ID)
                publish(message);
        }
    }, system.getParallelismMin() * system.getParallelismFactor() - 1) {

        @Override
        public void preStart() {
            super.preStart();
            group.addAll(hub.getPorts());
        }

        @Override
        public void receive(ActorMessage<?> message) {
            if (message.source == system.SYSTEM_ID) {
                primaryReceivedFromSystem.set(true);
                publish(message);
            } else if (message.tag == 101) {
                primaryReceived.set(true);
                testDone.countDown();
            }
        }
    });
    system.start();
    system.send(new ActorMessage<>(null, 0, system.SYSTEM_ID, primary));
    system.send(new ActorMessage<>(null, 101, system.SYSTEM_ID, group.peek()));
    try {
        testDone.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(primaryReceivedFromSystem.get());
    assertEquals(3, secondaryReceived.get());
    assertTrue(primaryReceived.get());
    system.shutdownWithActors(true);
}
Also used : ActorSystem(actor4j.core.ActorSystem) PrimaryActor(actor4j.core.actors.PrimaryActor) SecondaryActor(actor4j.core.actors.SecondaryActor) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentActorGroup(actor4j.core.utils.ConcurrentActorGroup) ActorSystem(actor4j.core.ActorSystem) PrimaryActor(actor4j.core.actors.PrimaryActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) UUID(java.util.UUID) Assert(org.junit.Assert) ActorMessage(actor4j.core.messages.ActorMessage) ConcurrentActorGroup(actor4j.core.utils.ConcurrentActorGroup) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecondaryActor(actor4j.core.actors.SecondaryActor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ActorSystem (actor4j.core.ActorSystem)1 PrimaryActor (actor4j.core.actors.PrimaryActor)1 SecondaryActor (actor4j.core.actors.SecondaryActor)1 ActorMessage (actor4j.core.messages.ActorMessage)1 ConcurrentActorGroup (actor4j.core.utils.ConcurrentActorGroup)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Assert (org.junit.Assert)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Test (org.junit.Test)1