Search in sources :

Example 6 with MetricGetters

use of com.nokia.dempsy.monitoring.coda.MetricGetters in project Dempsy by Dempsy.

the class TestMpContainer method testEvictCollisionWithBlocking.

@Test
public void testEvictCollisionWithBlocking() throws Throwable {
    // for this test we need to undo the setup
    tearDown();
    // and re-setUp setting failFast to false.
    setUp("false");
    // This forces the instantiation of an Mp
    BlockingQueueAdaptor adaptor = context.getBean(BlockingQueueAdaptor.class);
    assertNotNull(adaptor);
    inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
    // Once the poll finishes the Mp is instantiated and handling messages.
    assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
    assertEquals("did not create MP", 1, container.getProcessorCount());
    TestProcessor mp = (TestProcessor) container.getMessageProcessor("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);
    // now we're going to cause the passivate to be held up.
    mp.blockPassivate = new CountDownLatch(1);
    // allow eviction
    mp.evict.set(true);
    // 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);
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            container.evict();
            evictIsComplete.set(true);
        }
    });
    thread.start();
    //let it get going.
    Thread.sleep(500);
    // check to see we're hung.
    assertFalse(evictIsComplete.get());
    final MetricGetters sc = (MetricGetters) container.getStatsCollector();
    assertEquals(0, sc.getMessageCollisionCount());
    // sending it a message will now cause it to have the collision tick up
    inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
    // give it some time.
    Thread.sleep(100);
    // make sure there's no collision
    assertEquals(0, sc.getMessageCollisionCount());
    // make sure the message didn't get through
    assertNull(outputQueue.peek());
    // make sure no message got handled
    // 1 is the initial invocation that caused the instantiation.
    assertEquals(1, mp.invocationCount);
    // now let the evict finish
    mp.blockPassivate.countDown();
    // wait until the eviction completes
    assertTrue(TestUtils.poll(baseTimeoutMillis, evictIsComplete, new TestUtils.Condition<AtomicBoolean>() {

        @Override
        public boolean conditionMet(AtomicBoolean o) {
            return o.get();
        }
    }));
    // Once the poll finishes a new Mp is instantiated and handling messages.
    assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
    // get the new Mp
    TestProcessor mp2 = (TestProcessor) container.getMessageProcessor("foo");
    // invocationCount should be 1 from the initial invocation that caused the clone, and no more
    assertEquals(1, mp.invocationCount);
    assertEquals(1, mp2.invocationCount);
    assertTrue(mp != mp2);
    // send a message that should go through
    inputQueue.add(serializer.serialize(new ContainerTestMessage("foo")));
    // Once the poll finishes mp2 invocationCount should be incremented
    assertNotNull(outputQueue.poll(baseTimeoutMillis, TimeUnit.MILLISECONDS));
    assertEquals(1, mp.invocationCount);
    assertEquals(2, mp2.invocationCount);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingQueueAdaptor(com.nokia.dempsy.messagetransport.blockingqueue.BlockingQueueAdaptor) ContainerTestMessage(com.nokia.dempsy.container.mocks.ContainerTestMessage) CountDownLatch(java.util.concurrent.CountDownLatch) MetricGetters(com.nokia.dempsy.monitoring.coda.MetricGetters) Test(org.junit.Test)

Aggregations

MetricGetters (com.nokia.dempsy.monitoring.coda.MetricGetters)6 Test (org.junit.Test)5 Condition (com.nokia.dempsy.TestUtils.Condition)4 ClusterId (com.nokia.dempsy.config.ClusterId)4 ApplicationContext (org.springframework.context.ApplicationContext)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ClusterInfoSession (com.nokia.dempsy.cluster.ClusterInfoSession)2 ClusterInfoSessionFactory (com.nokia.dempsy.cluster.ClusterInfoSessionFactory)2 ContainerTestMessage (com.nokia.dempsy.container.mocks.ContainerTestMessage)2 BlockingQueueAdaptor (com.nokia.dempsy.messagetransport.blockingqueue.BlockingQueueAdaptor)2 Node (com.nokia.dempsy.Dempsy.Application.Cluster.Node)1 JunkDestination (com.nokia.dempsy.TestUtils.JunkDestination)1 DisruptibleSession (com.nokia.dempsy.cluster.DisruptibleSession)1 DefaultRouterSlotInfo (com.nokia.dempsy.router.DecentralizedRoutingStrategy.DefaultRouterSlotInfo)1