use of actor4j.core.actors.SecondaryActor 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);
}
Aggregations