use of actor4j.core.ActorSystem in project actor4j-core by relvaner.
the class StatelessActorFeature method test.
@Test(timeout = 10000)
public void test() {
ActorSystem system = new ActorSystem();
system.setParallelismFactor(2);
CountDownLatch testDone = new CountDownLatch(system.getParallelismMin() * system.getParallelismFactor());
ActorGroup group = new ActorGroupSet();
system.setAlias(system.addActor(() -> new StatelessActor(group) {
protected boolean first = true;
@Override
public void receive(ActorMessage<?> message) {
logger().debug(String.format("from thread %s of actor %s%n", Thread.currentThread().getName(), self()));
if (first) {
testDone.countDown();
first = false;
}
}
}, system.getParallelismMin() * system.getParallelismFactor()), "instances");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
system.sendViaAlias(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, null), "instances");
}
}, 0, 50);
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
timer.cancel();
system.shutdownWithActors(true);
}
use of actor4j.core.ActorSystem 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);
}
use of actor4j.core.ActorSystem in project actor4j-core by relvaner.
the class PersistenceFeature method test.
@Test(timeout = 10000)
public void test() {
CountDownLatch testDone = new CountDownLatch(2);
ActorSystem system = new ActorSystem();
AtomicBoolean first = new AtomicBoolean(true);
UUID id = system.addActor(() -> new PersistentActor<MyState, MyEvent>("example") {
@Override
public void receive(ActorMessage<?> message) {
MyEvent event1 = new MyEvent("I am the first event!");
MyEvent event2 = new MyEvent("I am the second event!");
saveSnapshot(null, null, new MyState("I am a state!"));
persist((s) -> logger().debug(String.format("Event: %s", s)), (e) -> logger().error(String.format("Error: %s", e.getMessage())), event1, event2);
if (first.getAndSet(false))
tell(null, Actor.RESTART, self());
}
@Override
public void recover(String json) {
if (!Recovery.isError(json)) {
logger().debug(String.format("Recovery: %s", json));
Recovery<MyState, MyEvent> obj = Recovery.convertValue(json, new TypeReference<Recovery<MyState, MyEvent>>() {
});
logger().debug(String.format("Recovery: %s", obj.toString()));
if (first.get())
assertEquals("{\"state\":{}}", json);
else {
assertEquals("I am a state!", obj.state.title);
assertTrue(obj.events.size() == 2);
assertEquals("I am the first event!", obj.events.get(0).title);
assertEquals("I am the second event!", obj.events.get(1).title);
}
testDone.countDown();
} else
logger().error(String.format("Error: %s", Recovery.getErrorMsg(json)));
}
@Override
public UUID persistenceId() {
/* e.g. https://www.uuidgenerator.net/ */
return UUID.fromString("60f086af-27d3-44e9-8fd7-eb095c98daed");
}
});
// Drop database
MongoClient client = new MongoClient("localhost", 27017);
client.dropDatabase("actor4j-test");
client.close();
system.persistenceMode("localhost", 27017, "actor4j-test");
system.start();
system.sendWhenActive(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, id));
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdownWithActors(true);
}
use of actor4j.core.ActorSystem in project actor4j-core by relvaner.
the class BehaviourFeature method before.
@Before
public void before() {
system = new ActorSystem();
system.setParallelismMin(1);
}
use of actor4j.core.ActorSystem in project actor4j-core by relvaner.
the class PseudoActorFeature method test.
@Test(timeout = 2000)
public void test() {
CountDownLatch testDone = new CountDownLatch(10);
ActorSystem system = new ActorSystem();
PseudoActor main = new PseudoActor(system, false) {
@Override
public void receive(ActorMessage<?> message) {
}
};
final int[] postconditions_numbers = new int[] { 341, 351, 451, 318, 292, 481, 240, 478, 382, 502, 158, 401, 438, 353, 165, 344, 6, 9, 18, 31, 77, 90, 45, 63, 190, 1 };
UUID numberGenerator = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor("numberGenerator") {
protected ActorTimer timer;
protected int counter = 0;
@Override
public void preStart() {
timer = system.timer().schedule(() -> new ActorMessage<Integer>(postconditions_numbers[counter++], 0, self(), null), main.getId(), 0, 25);
}
@Override
public void receive(ActorMessage<?> message) {
logger().debug(String.format("numberGenerator received a message.tag (%d) from main%n", message.tag));
testDone.countDown();
}
@Override
public void postStop() {
timer.cancel();
}
};
}
});
Timer timer = new Timer();
timer.schedule(new TimerTask() {
protected int i;
protected int counter = 0;
@Override
public void run() {
main.runWithRx().take(2).forEach(msg -> {
assertEquals(postconditions_numbers[counter++], msg.valueAsInt());
logger().debug("-> main received a message.value (" + msg.valueAsInt() + ") from numberGenerator");
});
main.send(new ActorMessage<>(null, i++, main.getId(), numberGenerator));
}
}, 0, 50);
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
timer.cancel();
system.shutdownWithActors(true);
}
Aggregations