Search in sources :

Example 1 with OutputMessage

use of net.dempsy.container.mocks.OutputMessage in project Dempsy by Dempsy.

the class TestContainer method addOutputCatchStage.

public NodeManager addOutputCatchStage() throws InterruptedException {
    // =======================================================
    // configure an output catcher tier
    final Node out = new Node("test-app").defaultRoutingStrategyId("net.dempsy.router.simple").receiver(new BlockingQueueReceiver(new ArrayBlockingQueue<>(16))).setNodeStatsCollector(// same app as the spring file.
    new BasicNodeStatsCollector());
    out.cluster("output-catch").mp(new MessageProcessor<OutputCatcher>(new OutputCatcher()));
    out.validate();
    final NodeManager nman = track(new NodeManager()).node(out).collaborator(track(sessionFactory.createSession())).start();
    // wait until we can actually reach the output-catch cluster from the main node
    assertTrue(poll(o -> {
        try {
            return canReach(getRouter(manager), "output-catch", new KeyExtractor().extract(new OutputMessage("foo", 1, 1)).iterator().next());
        } catch (final Exception e) {
            return false;
        }
    }));
    // =======================================================
    return nman;
}
Also used : Arrays(java.util.Arrays) Node(net.dempsy.config.Node) NodeManager(net.dempsy.NodeManager) SystemPropertyManager(net.dempsy.utils.test.SystemPropertyManager) ContainerTestMessage(net.dempsy.container.mocks.ContainerTestMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) After(org.junit.After) Parameterized(org.junit.runners.Parameterized) MessageKey(net.dempsy.lifecycle.annotation.MessageKey) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) Collection(java.util.Collection) Functional.uncheck(net.dempsy.util.Functional.uncheck) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Activation(net.dempsy.lifecycle.annotation.Activation) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) LockingContainer(net.dempsy.container.locking.LockingContainer) OutputMessage(net.dempsy.container.mocks.OutputMessage) Output(net.dempsy.lifecycle.annotation.Output) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Adaptor(net.dempsy.messages.Adaptor) AccessUtil.canReach(net.dempsy.AccessUtil.canReach) Start(net.dempsy.lifecycle.annotation.Start) Assert.assertFalse(org.junit.Assert.assertFalse) Evictable(net.dempsy.lifecycle.annotation.Evictable) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) AccessUtil.getRouter(net.dempsy.AccessUtil.getRouter) NonLockingContainer(net.dempsy.container.nonlocking.NonLockingContainer) Mp(net.dempsy.lifecycle.annotation.Mp) BlockingQueueReceiver(net.dempsy.transport.blockingqueue.BlockingQueueReceiver) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) MessageType(net.dempsy.lifecycle.annotation.MessageType) Dispatcher(net.dempsy.messages.Dispatcher) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) DempsyException(net.dempsy.DempsyException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) Functional.recheck(net.dempsy.util.Functional.recheck) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) KeyedMessage(net.dempsy.messages.KeyedMessage) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Passivation(net.dempsy.lifecycle.annotation.Passivation) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClusterStatsCollector(net.dempsy.monitoring.ClusterStatsCollector) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) NodeManager(net.dempsy.NodeManager) OutputMessage(net.dempsy.container.mocks.OutputMessage) BlockingQueueReceiver(net.dempsy.transport.blockingqueue.BlockingQueueReceiver) Node(net.dempsy.config.Node) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) DempsyException(net.dempsy.DempsyException) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector)

Example 2 with OutputMessage

use of net.dempsy.container.mocks.OutputMessage in project Dempsy by Dempsy.

the class TestContainer method testInvokeOutput.

@Test
public void testInvokeOutput() throws Exception {
    outputMessages = Collections.newSetFromMap(new ConcurrentHashMap<>());
    cache = new ConcurrentHashMap<>();
    final TestAdaptor adaptor = context.getBean(TestAdaptor.class);
    assertNotNull(adaptor.dispatcher);
    adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo"));
    adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("bar"));
    assertTrue(poll(container, c -> c.getProcessorCount() > 1));
    Thread.sleep(100);
    assertEquals("number of MP instances", 2, container.getProcessorCount());
    try (NodeManager nman = addOutputCatchStage()) {
        final TestProcessor mp = cache.get("foo");
        assertTrue(poll(mp, m -> mp.invocationCount > 0));
        Thread.sleep(100);
        assertEquals("invocation count, 1st message", 1, mp.invocationCount);
        // because the sessionFactory is shared and the appname is the same, we should be in the same app
        container.outputPass();
        assertTrue(poll(outputMessages, o -> o.size() > 1));
        Thread.sleep(100);
        assertEquals(2, outputMessages.size());
        // no new mps created in the first one
        assertEquals("did not create MP", 2, container.getProcessorCount());
        // but the invocation count should have increased since the output cycles feeds messages back to this cluster
        assertTrue(poll(mp, m -> mp.invocationCount > 1));
        Thread.sleep(100);
        assertEquals("invocation count, 1st message", 2, mp.invocationCount);
        // // order of messages is not guaranteed, so we need to aggregate keys
        final HashSet<String> messageKeys = new HashSet<String>();
        final Iterator<OutputMessage> iter = outputMessages.iterator();
        messageKeys.add(iter.next().getKey());
        messageKeys.add(iter.next().getKey());
        assertTrue("first MP sent output", messageKeys.contains("foo"));
        assertTrue("second MP sent output", messageKeys.contains("bar"));
    }
}
Also used : Arrays(java.util.Arrays) Node(net.dempsy.config.Node) NodeManager(net.dempsy.NodeManager) SystemPropertyManager(net.dempsy.utils.test.SystemPropertyManager) ContainerTestMessage(net.dempsy.container.mocks.ContainerTestMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) After(org.junit.After) Parameterized(org.junit.runners.Parameterized) MessageKey(net.dempsy.lifecycle.annotation.MessageKey) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) Collection(java.util.Collection) Functional.uncheck(net.dempsy.util.Functional.uncheck) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Activation(net.dempsy.lifecycle.annotation.Activation) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) LockingContainer(net.dempsy.container.locking.LockingContainer) OutputMessage(net.dempsy.container.mocks.OutputMessage) Output(net.dempsy.lifecycle.annotation.Output) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Adaptor(net.dempsy.messages.Adaptor) AccessUtil.canReach(net.dempsy.AccessUtil.canReach) Start(net.dempsy.lifecycle.annotation.Start) Assert.assertFalse(org.junit.Assert.assertFalse) Evictable(net.dempsy.lifecycle.annotation.Evictable) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) AccessUtil.getRouter(net.dempsy.AccessUtil.getRouter) NonLockingContainer(net.dempsy.container.nonlocking.NonLockingContainer) Mp(net.dempsy.lifecycle.annotation.Mp) BlockingQueueReceiver(net.dempsy.transport.blockingqueue.BlockingQueueReceiver) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) MessageType(net.dempsy.lifecycle.annotation.MessageType) Dispatcher(net.dempsy.messages.Dispatcher) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) DempsyException(net.dempsy.DempsyException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) Functional.recheck(net.dempsy.util.Functional.recheck) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) KeyedMessage(net.dempsy.messages.KeyedMessage) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Passivation(net.dempsy.lifecycle.annotation.Passivation) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClusterStatsCollector(net.dempsy.monitoring.ClusterStatsCollector) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) NodeManager(net.dempsy.NodeManager) OutputMessage(net.dempsy.container.mocks.OutputMessage) ContainerTestMessage(net.dempsy.container.mocks.ContainerTestMessage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 AccessUtil.canReach (net.dempsy.AccessUtil.canReach)2 AccessUtil.getRouter (net.dempsy.AccessUtil.getRouter)2 DempsyException (net.dempsy.DempsyException)2 NodeManager (net.dempsy.NodeManager)2