Search in sources :

Example 6 with Container

use of net.dempsy.container.Container in project Dempsy by Dempsy.

the class TestAlmostSimple method testSimple.

@Test
public void testSimple() throws Exception {
    final MpEven mpEven = new MpEven();
    final MpOdd mpOdd = new MpOdd();
    final AtomicLong count = new AtomicLong(0);
    Adaptor adaptor = null;
    try (final DefaultThreadingModel tm = new DefaultThreadingModel("TB", -1, 1);
        final NodeManager nm = new NodeManager()) {
        final Node n = new Node.Builder("test-app").defaultRoutingStrategyId("net.dempsy.router.simple").containerTypeId(containerTypeId).receiver(new Dummy()).cluster("start").adaptor(adaptor = new Adaptor() {

            private Dispatcher disp;

            boolean done = false;

            int cur = 0;

            AtomicBoolean exitedLoop = new AtomicBoolean(false);

            @Override
            public void stop() {
                done = true;
                while (!exitedLoop.get()) Thread.yield();
            }

            @Override
            public void start() {
                try {
                    while (!done) {
                        uncheck(() -> disp.dispatchAnnotated(new Message(cur++)));
                        count.incrementAndGet();
                    }
                } finally {
                    exitedLoop.set(true);
                }
            }

            @Override
            public void setDispatcher(final Dispatcher dispatcher) {
                this.disp = dispatcher;
            }
        }).cluster("mpEven").mp(new MessageProcessor<>(mpEven)).evictionFrequency(1, TimeUnit.SECONDS).cluster("mpOdd").mp(new MessageProcessor<>(mpOdd)).build();
        nm.node(n).collaborator(new LocalClusterSessionFactory().createSession()).threadingModel(tm.start(n.application));
        nm.start();
        assertTrue(ConditionPoll.poll(c -> count.get() > 100000));
        // make sure an eviction happened before closing
        // at this point all of the mps should have been checked for eviction
        assertTrue(ConditionPoll.poll(o -> 2 == mpEven.allMps.size()));
        mpEven.allMps.forEach(m -> uncheck(() -> assertTrue(ConditionPoll.poll(o -> m.evictCalled))));
        // now enable them to actually be removed from the container.
        // we need to stop the adaptor or we'll keep recreating them
        adaptor.stop();
        // need to wait for all of the adaptor messages to play through before evicting
        Thread.sleep(2000);
        MpEven.allowEviction.set(true);
        final List<Container> containers = nm.getContainers().stream().filter(c -> "mpEven".equals(c.getClusterId().clusterName)).collect(Collectors.toList());
        assertEquals(1, containers.size());
        final Container container = containers.get(0);
        assertTrue(ConditionPoll.poll(o -> 0 == container.getProcessorCount()));
    }
    Thread.sleep(2000);
    // make sure the messages were distributed correctly.
    assertEquals(2, mpEven.allMps.size());
    assertEquals(2, mpOdd.allMps.size());
    final MpEven mpEvenYes = mpEven.allMps.stream().filter(m -> "yes".equals(m.key)).findAny().get();
    // all messages here should be even numbers
    assertTrue(mpEvenYes.received.stream().allMatch(m -> (m.message & 0x01) == 0));
    final MpEven mpEvenNo = mpEven.allMps.stream().filter(m -> "no".equals(m.key)).findAny().get();
    // all messages here should be odd numbers
    assertTrue(mpEvenNo.received.stream().allMatch(m -> (m.message & 0x01) == 1));
    final MpOdd mpOddYes = mpOdd.allMps.stream().filter(m -> "yes".equals(m.key)).findAny().get();
    // all messages here should be odd numbers
    assertTrue(mpOddYes.received.stream().allMatch(m -> (m.message & 0x01) == 1));
    final MpOdd mpOddNo = mpOdd.allMps.stream().filter(m -> "no".equals(m.key)).findAny().get();
    // all messages here should be even numbers
    assertTrue(mpOddNo.received.stream().allMatch(m -> (m.message & 0x01) == 0));
}
Also used : Adaptor(net.dempsy.messages.Adaptor) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Arrays(java.util.Arrays) Mp(net.dempsy.lifecycle.annotation.Mp) Node(net.dempsy.config.Node) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) MessageType(net.dempsy.lifecycle.annotation.MessageType) Dispatcher(net.dempsy.messages.Dispatcher) NodeAddress(net.dempsy.transport.NodeAddress) Listener(net.dempsy.transport.Listener) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConditionPoll(net.dempsy.utils.test.ConditionPoll) ArrayList(java.util.ArrayList) NonLockingAltBulkContainer(net.dempsy.container.altnonlockingbulk.NonLockingAltBulkContainer) Receiver(net.dempsy.transport.Receiver) Parameterized(org.junit.runners.Parameterized) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) MessageKey(net.dempsy.lifecycle.annotation.MessageKey) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) Collection(java.util.Collection) Functional.uncheck(net.dempsy.util.Functional.uncheck) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Activation(net.dempsy.lifecycle.annotation.Activation) Collectors(java.util.stream.Collectors) LockingContainer(net.dempsy.container.locking.LockingContainer) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Adaptor(net.dempsy.messages.Adaptor) Functional.chain(net.dempsy.util.Functional.chain) Evictable(net.dempsy.lifecycle.annotation.Evictable) Container(net.dempsy.container.Container) MessageTransportException(net.dempsy.transport.MessageTransportException) Assert.assertEquals(org.junit.Assert.assertEquals) Node(net.dempsy.config.Node) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) Dispatcher(net.dempsy.messages.Dispatcher) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) NonLockingAltBulkContainer(net.dempsy.container.altnonlockingbulk.NonLockingAltBulkContainer) LockingContainer(net.dempsy.container.locking.LockingContainer) Container(net.dempsy.container.Container) Test(org.junit.Test)

Example 7 with Container

use of net.dempsy.container.Container in project Dempsy by Dempsy.

the class TestInstanceManager method testOutputShortCircuitsIfNoOutputMethod.

@Test
public void testOutputShortCircuitsIfNoOutputMethod() throws Exception {
    final CombinedMP prototype = new CombinedMP();
    final Container manager = setupContainer(new MessageProcessor<CombinedMP>(prototype));
    final DummyDispatcher dispatcher = ((DummyDispatcher) manager.getDispatcher());
    // we need to dispatch messages to create MP instances
    final KeyedMessageWithType message1 = km(new MessageOne(1));
    final KeyedMessageWithType message2 = km(new MessageOne(2));
    manager.dispatch(message1, Operation.handle, true);
    manager.dispatch(message2, Operation.handle, true);
    assertEquals(new ReturnString("MessageOne"), dispatcher.lastDispatched.message);
    manager.invokeOutput();
    // output messages are NOT considered "processed" if there is no output method on the MP.
    assertEquals("number of processed messages should include outputs.", 2, ((ClusterMetricGetters) statsCollector).getProcessedMessageCount());
}
Also used : NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) Container(net.dempsy.container.Container) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) Test(org.junit.Test)

Example 8 with Container

use of net.dempsy.container.Container in project Dempsy by Dempsy.

the class TestInstanceManager method testMpThrows.

@Test
public void testMpThrows() throws Exception {
    try (final Container dispatcher = setupContainer(new MessageProcessor<ThrowMe>(new ThrowMe()))) {
        dispatcher.dispatch(km(new MessageOne(123)), Operation.handle, true);
        assertEquals(1, ((ClusterMetricGetters) statsCollector).getMessageFailedCount());
    }
}
Also used : NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) Container(net.dempsy.container.Container) Test(org.junit.Test)

Example 9 with Container

use of net.dempsy.container.Container in project Dempsy by Dempsy.

the class TestOutputSchedulers method testRelativeSchedule.

/**
 * Test relative schedule.
 *
 * @throws Exception the exception
 */
@Test
public void testRelativeSchedule() throws Exception {
    try (final RelativeOutputSchedule relativeOutputSchedule = new RelativeOutputSchedule(1, TimeUnit.SECONDS)) {
        relativeOutputSchedule.setOutputInvoker(container);
        relativeOutputSchedule.start(new TestInfrastructure(null));
        assertTrue(poll(outputInvoked, oi -> oi.get()));
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) TestInfrastructure(net.dempsy.util.TestInfrastructure) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Container(net.dempsy.container.Container) Test(org.junit.Test) Before(org.junit.Before) TestInfrastructure(net.dempsy.util.TestInfrastructure) Test(org.junit.Test)

Example 10 with Container

use of net.dempsy.container.Container in project Dempsy by Dempsy.

the class TestOutputSchedulers method testCronSchedule.

/**
 * Test cron schedule.
 *
 * @throws Exception the exception
 */
@Test
public void testCronSchedule() throws Exception {
    try (final CronOutputSchedule cronOutputSchedule = new CronOutputSchedule("0/1 * * * * ?")) {
        cronOutputSchedule.setOutputInvoker(container);
        cronOutputSchedule.start(new TestInfrastructure(null));
        assertTrue(poll(outputInvoked, oi -> oi.get()));
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) TestInfrastructure(net.dempsy.util.TestInfrastructure) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Container(net.dempsy.container.Container) Test(org.junit.Test) Before(org.junit.Before) TestInfrastructure(net.dempsy.util.TestInfrastructure) Test(org.junit.Test)

Aggregations

Container (net.dempsy.container.Container)12 Test (org.junit.Test)11 NonLockingAltContainer (net.dempsy.container.altnonlocking.NonLockingAltContainer)8 TimeUnit (java.util.concurrent.TimeUnit)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Assert.assertTrue (org.junit.Assert.assertTrue)6 ConditionPoll.poll (net.dempsy.utils.test.ConditionPoll.poll)5 KeyedMessageWithType (net.dempsy.messages.KeyedMessageWithType)4 TestInfrastructure (net.dempsy.util.TestInfrastructure)4 Before (org.junit.Before)4 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Collectors (java.util.stream.Collectors)2 Node (net.dempsy.config.Node)2 Adaptor (net.dempsy.messages.Adaptor)2 DefaultThreadingModel (net.dempsy.threading.DefaultThreadingModel)2 NodeAddress (net.dempsy.transport.NodeAddress)2 Receiver (net.dempsy.transport.Receiver)2