use of actor4j.core.utils.ActorFactory 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);
}
use of actor4j.core.utils.ActorFactory in project actor4j-core by relvaner.
the class UnhandledFeature method test.
@Test(timeout = 2000)
public void test() {
CountDownLatch testDone = new CountDownLatch(1);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor("UnhandledFeatureActor") {
@Mock
protected Appender mockAppender;
@Captor
protected ArgumentCaptor<LoggingEvent> captorLoggingEvent;
@Override
public void receive(ActorMessage<?> message) {
MockitoAnnotations.initMocks(this);
logger().removeAllAppenders();
logger().addAppender(mockAppender);
unhandled(message);
verify(mockAppender, times(1)).doAppend(captorLoggingEvent.capture());
LoggingEvent loggingEvent = captorLoggingEvent.getValue();
assertTrue(loggingEvent.getMessage().toString().contains("Unhandled message"));
testDone.countDown();
}
};
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
}
use of actor4j.core.utils.ActorFactory in project actor4j-core by relvaner.
the class AwaitFeature method test_await.
@Test(timeout = 2000)
public void test_await() {
CountDownLatch testDone = new CountDownLatch(1);
AtomicBoolean[] postconditions = new AtomicBoolean[2];
for (int i = 0; i < postconditions.length; i++) postconditions[i] = new AtomicBoolean(false);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor() {
protected Consumer<ActorMessage<?>> action = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
postconditions[0].set(true);
}
};
protected boolean first = true;
@Override
public void receive(ActorMessage<?> message) {
if (first) {
await(1, action);
first = false;
} else {
postconditions[1].set(true);
testDone.countDown();
}
}
};
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 1, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 1, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
assertEquals(true, postconditions[0].get());
assertEquals(true, postconditions[1].get());
}
use of actor4j.core.utils.ActorFactory in project actor4j-core by relvaner.
the class BehaviourFeature method test_stack_become_unbecomeAll.
@Test(timeout = 2000)
public void test_stack_become_unbecomeAll() {
CountDownLatch testDone = new CountDownLatch(1);
final AtomicBoolean[] behaviour = new AtomicBoolean[3];
for (int i = 0; i < behaviour.length; i++) behaviour[i] = new AtomicBoolean(false);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor() {
protected Consumer<ActorMessage<?>> newBehaviour1 = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
behaviour[0].set(true);
become(newBehaviour2, false);
}
};
protected Consumer<ActorMessage<?>> newBehaviour2 = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
behaviour[1].set(true);
unbecomeAll();
}
};
protected boolean first = true;
@Override
public void receive(ActorMessage<?> message) {
if (first) {
become(newBehaviour1);
first = false;
} else {
behaviour[2].set(true);
testDone.countDown();
}
}
};
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
assertEquals(true, behaviour[0].get());
assertEquals(true, behaviour[1].get());
assertEquals(true, behaviour[2].get());
}
use of actor4j.core.utils.ActorFactory in project actor4j-core by relvaner.
the class BehaviourFeature method test_become_unbecome.
@Test(timeout = 2000)
public void test_become_unbecome() {
CountDownLatch testDone = new CountDownLatch(1);
final AtomicBoolean[] behaviour = new AtomicBoolean[2];
for (int i = 0; i < behaviour.length; i++) behaviour[i] = new AtomicBoolean(false);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor() {
protected Consumer<ActorMessage<?>> newBehaviour = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
behaviour[0].set(true);
unbecome();
}
};
protected boolean first = true;
@Override
public void receive(ActorMessage<?> message) {
if (first) {
become(newBehaviour);
first = false;
} else {
behaviour[1].set(true);
testDone.countDown();
}
}
};
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
assertEquals(true, behaviour[0].get());
assertEquals(true, behaviour[1].get());
}
Aggregations