Search in sources :

Example 6 with ActorFactory

use of actor4j.core.utils.ActorFactory in project actor4j-core by relvaner.

the class BehaviourFeature method test_become.

@Test(timeout = 2000)
public void test_become() {
    CountDownLatch testDone = new CountDownLatch(1);
    final AtomicBoolean behaviour = 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.set(true);
                        testDone.countDown();
                    }
                };

                @Override
                public void receive(ActorMessage<?> message) {
                    become(newBehaviour);
                }
            };
        }
    });
    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.get());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(java.util.function.Consumer) Actor(actor4j.core.actors.Actor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) ActorFactory(actor4j.core.utils.ActorFactory) Test(org.junit.Test)

Example 7 with ActorFactory

use of actor4j.core.utils.ActorFactory in project actor4j-core by relvaner.

the class BehaviourFeature method test_stack_become_unbecome.

@Test(timeout = 2000)
public void test_stack_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<?>> newBehaviour1 = new Consumer<ActorMessage<?>>() {

                    protected boolean first = true;

                    @Override
                    public void accept(ActorMessage<?> t) {
                        if (first) {
                            become(newBehaviour2, false);
                            first = false;
                        } else {
                            behaviour[1].set(true);
                            testDone.countDown();
                        }
                    }
                };

                protected Consumer<ActorMessage<?>> newBehaviour2 = new Consumer<ActorMessage<?>>() {

                    @Override
                    public void accept(ActorMessage<?> t) {
                        behaviour[0].set(true);
                        unbecome();
                    }
                };

                @Override
                public void receive(ActorMessage<?> message) {
                    become(newBehaviour1);
                }
            };
        }
    });
    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());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(java.util.function.Consumer) Actor(actor4j.core.actors.Actor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) ActorFactory(actor4j.core.utils.ActorFactory) Test(org.junit.Test)

Example 8 with ActorFactory

use of actor4j.core.utils.ActorFactory in project actor4j-core by relvaner.

the class SafetyFeature 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("SafetyFeatureActor") {

                @Override
                public void receive(ActorMessage<?> message) {
                    throw new NullPointerException();
                }

                @Override
                public void preRestart(Exception reason) {
                    super.preRestart(reason);
                    assertEquals(new NullPointerException().getMessage(), reason.getMessage());
                }

                @Override
                public void postRestart(Exception reason) {
                    super.postRestart(reason);
                    assertEquals(new NullPointerException().getMessage(), reason.getMessage());
                }
            };
        }
    });
    ErrorHandler errorHandler = system.underlyingImpl().getExecuterService().getSafetyManager().getErrorHandler();
    system.underlyingImpl().getExecuterService().getSafetyManager().setErrorHandler(new ErrorHandler() {

        @Override
        public void handle(Throwable t, String message, UUID uuid) {
            errorHandler.handle(t, message, uuid);
            assertEquals(new NullPointerException().getMessage(), t.getMessage());
            assertEquals("actor", message);
            assertEquals(dest, uuid);
            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.start();
    try {
        testDone.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    system.shutdown(true);
}
Also used : ErrorHandler(safety4j.ErrorHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Actor(actor4j.core.actors.Actor) UUID(java.util.UUID) ActorFactory(actor4j.core.utils.ActorFactory) Test(org.junit.Test)

Aggregations

Actor (actor4j.core.actors.Actor)8 ActorFactory (actor4j.core.utils.ActorFactory)8 UUID (java.util.UUID)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 Test (org.junit.Test)8 ActorMessage (actor4j.core.messages.ActorMessage)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Consumer (java.util.function.Consumer)5 ActorSystem (actor4j.core.ActorSystem)1 ActorTimer (actor4j.core.ActorTimer)1 PseudoActor (actor4j.core.actors.PseudoActor)1 ActorLogger.logger (actor4j.core.utils.ActorLogger.logger)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 Appender (org.apache.log4j.Appender)1 LoggingEvent (org.apache.log4j.spi.LoggingEvent)1 Assert (org.junit.Assert)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 Captor (org.mockito.Captor)1 Mock (org.mockito.Mock)1