Search in sources :

Example 1 with Activation

use of net.dempsy.lifecycle.annotation.Activation in project Dempsy by Dempsy.

the class TestContainer method createAndGet.

private TestProcessor createAndGet(final String foo) throws Exception {
    cache = new HashMap<>();
    final TestAdaptor adaptor = context.getBean(TestAdaptor.class);
    assertNotNull(adaptor.dispatcher);
    adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage(foo));
    assertTrue(poll(o -> container.getProcessorCount() > 0));
    Thread.sleep(100);
    assertEquals("did not create MP", 1, container.getProcessorCount());
    assertTrue(poll(cache, c -> c.get(foo) != null));
    final TestProcessor mp = cache.get(foo);
    assertNotNull("MP not associated with expected key", mp);
    assertEquals("activation count, 1st message", 1, mp.activationCount);
    assertEquals("invocation count, 1st message", 1, mp.invocationCount);
    return mp;
}
Also used : Arrays(java.util.Arrays) Node(net.dempsy.config.Node) NodeManager(net.dempsy.NodeManager) 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) Operation(net.dempsy.container.Container.Operation) 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) 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) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) 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) ContainerTestMessage(net.dempsy.container.mocks.ContainerTestMessage)

Example 2 with Activation

use of net.dempsy.lifecycle.annotation.Activation in project Dempsy by Dempsy.

the class TestContainer method testMessageDispatch.

@Test
public void testMessageDispatch() throws Exception {
    cache = new ConcurrentHashMap<>();
    final TestAdaptor adaptor = context.getBean(TestAdaptor.class);
    assertNotNull(adaptor.dispatcher);
    adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo"));
    assertTrue(poll(o -> container.getProcessorCount() > 0));
    Thread.sleep(100);
    assertEquals("did not create MP", 1, container.getProcessorCount());
    assertTrue(poll(cache, c -> c.get("foo") != null));
    final TestProcessor mp = cache.get("foo");
    assertNotNull("MP not associated with expected key", mp);
    assertEquals("activation count, 1st message", 1, mp.activationCount);
    assertEquals("invocation count, 1st message", 1, mp.invocationCount);
    adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo"));
    assertTrue(poll(mp, o -> o.invocationCount > 1));
    Thread.sleep(100);
    assertEquals("activation count, 2nd message", 1, mp.activationCount);
    assertEquals("invocation count, 2nd message", 2, mp.invocationCount);
}
Also used : Arrays(java.util.Arrays) Node(net.dempsy.config.Node) NodeManager(net.dempsy.NodeManager) 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) Operation(net.dempsy.container.Container.Operation) 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) 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) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) 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) ContainerTestMessage(net.dempsy.container.mocks.ContainerTestMessage) Test(org.junit.Test)

Example 3 with Activation

use of net.dempsy.lifecycle.annotation.Activation in project Dempsy by Dempsy.

the class TestContainer method testEvictableWithBusyMp.

@Test
public void testEvictableWithBusyMp() throws Throwable {
    final TestProcessor mp = createAndGet("foo");
    // now we're going to cause the processing to be held up.
    mp.latch = new CountDownLatch(1);
    // allow eviction
    mp.evict.set(true);
    // sending it a message will now cause it to hang up while processing
    final TestAdaptor adaptor = context.getBean(TestAdaptor.class);
    adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo"));
    final TestProcessor prototype = context.getBean(TestProcessor.class);
    // keep track of the cloneCount for later checking
    final int tmpCloneCount = prototype.cloneCount.intValue();
    // invocation count should go to 2
    assertTrue(poll(mp, o -> o.invocationCount == 2));
    // now kick off the evict in a separate thread since we expect it to hang
    // until the mp becomes unstuck.
    // this will allow us to see the evict pass complete
    final AtomicBoolean evictIsComplete = new AtomicBoolean(false);
    final Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            container.evict();
            evictIsComplete.set(true);
        }
    });
    thread.start();
    // now check to make sure eviction doesn't complete.
    // just a little to give any mistakes a change to work themselves through
    Thread.sleep(100);
    // make sure eviction didn't finish
    assertFalse(evictIsComplete.get());
    // this lets it go
    mp.latch.countDown();
    // wait until the eviction completes
    assertTrue(poll(evictIsComplete, o -> o.get()));
    Thread.sleep(100);
    assertEquals("activation count, 2nd message", 1, mp.activationCount);
    assertEquals("invocation count, 2nd message", 2, mp.invocationCount);
    adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo"));
    assertTrue(poll(o -> prototype.cloneCount.intValue() > tmpCloneCount));
    Thread.sleep(1000);
    assertEquals("Clone count, 2nd message", tmpCloneCount + 1, prototype.cloneCount.intValue());
}
Also used : Arrays(java.util.Arrays) Node(net.dempsy.config.Node) NodeManager(net.dempsy.NodeManager) 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) Operation(net.dempsy.container.Container.Operation) 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) 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) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ContainerTestMessage(net.dempsy.container.mocks.ContainerTestMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with Activation

use of net.dempsy.lifecycle.annotation.Activation in project Dempsy by Dempsy.

the class TestContainer method testFeedbackLoop.

@Test
public void testFeedbackLoop() throws Exception {
    cache = new ConcurrentHashMap<>();
    final TestAdaptor adaptor = context.getBean(TestAdaptor.class);
    assertNotNull(adaptor.dispatcher);
    adaptor.dispatcher.dispatchAnnotated(new MyMessage("foo"));
    assertTrue(poll(o -> container.getProcessorCount() > 0));
    Thread.sleep(100);
    assertEquals("did not create MP", 1, container.getProcessorCount());
    assertTrue(poll(cache, c -> c.get("foo") != null));
    final TestProcessor mp = cache.get("foo");
    assertNotNull("MP not associated with expected key", mp);
    assertTrue(poll(mp, o -> o.invocationCount > 1));
    assertEquals("activation count, 1st message", 1, mp.activationCount);
    assertEquals("invocation count, 1st message", 2, mp.invocationCount);
    adaptor.dispatcher.dispatchAnnotated(new ContainerTestMessage("foo"));
    assertTrue(poll(mp, o -> o.invocationCount > 2));
    Thread.sleep(100);
    assertEquals("activation count, 2nd message", 1, mp.activationCount);
    assertEquals("invocation count, 2nd message", 3, mp.invocationCount);
}
Also used : Arrays(java.util.Arrays) Node(net.dempsy.config.Node) NodeManager(net.dempsy.NodeManager) 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) Operation(net.dempsy.container.Container.Operation) 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) 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) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) 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) ContainerTestMessage(net.dempsy.container.mocks.ContainerTestMessage) Test(org.junit.Test)

Aggregations

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