Search in sources :

Example 1 with MockInputMessage

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

the class TestContainerLoadHandling method runSimpleProcessingWithoutQueuing.

/*
     * Actual Unit Tests
     */
public void runSimpleProcessingWithoutQueuing(final boolean block) throws Exception {
    // set up a gate for starting
    SendMessageThread.startLatch = new CountDownLatch(1);
    // produce the initial MPs
    final ArrayList<Thread> in = new ArrayList<Thread>();
    final KeyedMessage[] messages = new KeyedMessage[NUMMPS];
    for (int i = 0; i < NUMMPS; i++) messages[i] = km(new MockInputMessage("key" + i));
    for (int j = 0; j < NUMMPS; j++) in.add(startMessagePump(container, messages, block));
    // let the messages go.
    SendMessageThread.startLatch.countDown();
    assertTrue(poll(o -> container.getProcessorCount() == NUMMPS));
    stillRunning = false;
    assertTrue(poll(o -> in.stream().filter(t -> t.isAlive()).count() == 0));
    final int numMessagePumped = dispatcher.messages.size();
    final long initialDiscard = clusterStats.getMessageCollisionCount();
    dispatcher.messages.clear();
    // Send NTHREADS Messages and wait for them to come out, then repeat
    for (int i = 0; i < NUMRUNS; i++) {
        // set up a gate for starting
        SendMessageThread.startLatch = new CountDownLatch(1);
        SendMessageThread.finishedCount.set(0);
        SendMessageThread.processingCount.set(0);
        in.clear();
        for (int j = 0; j < NUMTHREADS; j++) in.add(sendMessages(container, messages, block, MSGPERTHREAD));
        assertTrue(poll(o -> SendMessageThread.processingCount.get() == NUMTHREADS));
        // let the messages go.
        SendMessageThread.startLatch.countDown();
        // after sends are allowed to proceed
        assertTrue("Timeout waiting on message to be sent", poll(o -> SendMessageThread.finishedCount.get() >= NUMTHREADS));
        assertEquals(NUMTHREADS, SendMessageThread.finishedCount.get());
        assertTrue(poll(o -> clusterStats.getInFlightMessageCount() == 0));
        assertTrue(poll(o -> in.stream().filter(t -> t.isAlive() == true).count() == 0));
        final long discarded = clusterStats.getMessageCollisionCount();
        if (block)
            assertEquals(0L, discarded);
        final int iter = i;
        assertEquals((NUMTHREADS * MSGPERTHREAD) * (iter + 1), dispatcher.messages.size() + discarded - initialDiscard);
        assertEquals((NUMTHREADS * MSGPERTHREAD) * (iter + 1) + numMessagePumped, clusterStats.getProcessedMessageCount() + discarded - initialDiscard);
    }
    checkStat(clusterStats);
}
Also used : MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Arrays(java.util.Arrays) NonLockingContainer(net.dempsy.container.nonlocking.NonLockingContainer) Mp(net.dempsy.lifecycle.annotation.Mp) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) Dispatcher(net.dempsy.messages.Dispatcher) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) LoggerFactory(org.slf4j.LoggerFactory) MockOutputMessage(net.dempsy.container.mocks.MockOutputMessage) Random(java.util.Random) DummyInbound(net.dempsy.container.mocks.DummyInbound) MockInputMessage(net.dempsy.container.mocks.MockInputMessage) ArrayList(java.util.ArrayList) BasicClusterStatsCollector(net.dempsy.monitoring.basic.BasicClusterStatsCollector) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) After(org.junit.After) Manager(net.dempsy.Manager) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) Parameterized(org.junit.runners.Parameterized) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) StatsCollector(net.dempsy.monitoring.StatsCollector) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) Collection(java.util.Collection) KeyedMessage(net.dempsy.messages.KeyedMessage) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) LockingContainer(net.dempsy.container.locking.LockingContainer) Output(net.dempsy.lifecycle.annotation.Output) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClusterStatsCollector(net.dempsy.monitoring.ClusterStatsCollector) NodeStatsCollector(net.dempsy.monitoring.NodeStatsCollector) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Functional.chain(net.dempsy.util.Functional.chain) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) MockInputMessage(net.dempsy.container.mocks.MockInputMessage) CountDownLatch(java.util.concurrent.CountDownLatch) KeyedMessage(net.dempsy.messages.KeyedMessage)

Example 2 with MockInputMessage

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

the class TestContainerLoadHandling method runSimpleProcessingWithoutQueuing.

/*
     * Actual Unit Tests
     */
public void runSimpleProcessingWithoutQueuing() throws Exception {
    // set up a gate for starting
    SendMessageThread.startLatch = new CountDownLatch(1);
    // produce the initial MPs
    final ArrayList<Thread> in = new ArrayList<Thread>();
    final KeyedMessage[] messages = new KeyedMessage[NUMMPS];
    for (int i = 0; i < NUMMPS; i++) messages[i] = km(new MockInputMessage("key" + i));
    for (int j = 0; j < NUMMPS; j++) in.add(startMessagePump(container, messages));
    // let the messages go.
    SendMessageThread.startLatch.countDown();
    assertTrue(poll(o -> container.getProcessorCount() == NUMMPS));
    stillRunning = false;
    assertTrue(poll(o -> in.stream().filter(t -> t.isAlive()).count() == 0));
    final int numMessagePumped = dispatcher.messages.size();
    final long initialDiscard = clusterStats.getMessageCollisionCount();
    dispatcher.messages.clear();
    // Send NTHREADS Messages and wait for them to come out, then repeat
    for (int i = 0; i < NUMRUNS; i++) {
        // set up a gate for starting
        SendMessageThread.startLatch = new CountDownLatch(1);
        SendMessageThread.finishedCount.set(0);
        SendMessageThread.processingCount.set(0);
        in.clear();
        for (int j = 0; j < NUMTHREADS; j++) in.add(sendMessages(container, messages, MSGPERTHREAD));
        assertTrue(poll(o -> SendMessageThread.processingCount.get() == NUMTHREADS));
        // let the messages go.
        SendMessageThread.startLatch.countDown();
        // after sends are allowed to proceed
        assertTrue("Timeout waiting on message to be sent", poll(o -> SendMessageThread.finishedCount.get() >= NUMTHREADS));
        assertEquals(NUMTHREADS, SendMessageThread.finishedCount.get());
        assertTrue(poll(o -> clusterStats.getInFlightMessageCount() == 0));
        assertTrue(poll(o -> in.stream().filter(t -> t.isAlive() == true).count() == 0));
        final long discarded = clusterStats.getMessageCollisionCount();
        assertEquals(0L, discarded);
        final int iter = i;
        assertEquals((NUMTHREADS * MSGPERTHREAD) * (iter + 1), dispatcher.messages.size() + discarded - initialDiscard);
        assertEquals((NUMTHREADS * MSGPERTHREAD) * (iter + 1) + numMessagePumped, clusterStats.getProcessedMessageCount() + discarded - initialDiscard);
    }
    checkStat(clusterStats);
}
Also used : MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Arrays(java.util.Arrays) Mp(net.dempsy.lifecycle.annotation.Mp) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) Dispatcher(net.dempsy.messages.Dispatcher) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) LoggerFactory(org.slf4j.LoggerFactory) MockOutputMessage(net.dempsy.container.mocks.MockOutputMessage) Random(java.util.Random) DummyInbound(net.dempsy.container.mocks.DummyInbound) MockInputMessage(net.dempsy.container.mocks.MockInputMessage) ArrayList(java.util.ArrayList) MessageResourceManager(net.dempsy.messages.MessageResourceManager) BasicClusterStatsCollector(net.dempsy.monitoring.basic.BasicClusterStatsCollector) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) After(org.junit.After) Manager(net.dempsy.Manager) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) Parameterized(org.junit.runners.Parameterized) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) StatsCollector(net.dempsy.monitoring.StatsCollector) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) Collection(java.util.Collection) Operation(net.dempsy.container.Container.Operation) KeyedMessage(net.dempsy.messages.KeyedMessage) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) LockingContainer(net.dempsy.container.locking.LockingContainer) Output(net.dempsy.lifecycle.annotation.Output) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClusterStatsCollector(net.dempsy.monitoring.ClusterStatsCollector) NodeStatsCollector(net.dempsy.monitoring.NodeStatsCollector) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Functional.chain(net.dempsy.util.Functional.chain) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) MockInputMessage(net.dempsy.container.mocks.MockInputMessage) CountDownLatch(java.util.concurrent.CountDownLatch) KeyedMessage(net.dempsy.messages.KeyedMessage)

Example 3 with MockInputMessage

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

the class TestContainerLoadHandling method testMessagesCanQueueWithinLimitsBlocking.

/**
 * Test the case where messages arrive faster than they are processed but the queue never exceeds the Thread Pool Size * 2, so no messages are discarded.
 */
@Test
public void testMessagesCanQueueWithinLimitsBlocking() throws Exception {
    // produce the initial MPs
    final ArrayList<Thread> in = new ArrayList<Thread>();
    final KeyedMessage[] messages = new KeyedMessage[NUMMPS];
    for (int i = 0; i < NUMMPS; i++) messages[i] = km(new MockInputMessage("key" + i));
    for (int j = 0; j < NUMMPS; j++) in.add(sendMessages(container, new KeyedMessage[] { messages[j] }, 1));
    // let the messages go.
    SendMessageThread.startLatch.countDown();
    assertTrue(poll(o -> in.stream().filter(t -> t.isAlive()).count() == 0));
    assertEquals(NUMMPS, container.getProcessorCount());
    final int numMessagePumped = dispatcher.messages.size();
    assertEquals(numMessagePumped, clusterStats.getProcessedMessageCount());
    dispatcher.messages.clear();
    for (int i = 0; i < NUMRUNS; i++) {
        final int iter = i;
        final long initialMessagesDispatched = clusterStats.getDispatchedMessageCount();
        // set up a gate for starting
        SendMessageThread.startLatch = new CountDownLatch(1);
        SendMessageThread.finishedCount.set(0);
        SendMessageThread.processingCount.set(0);
        commonLongRunningHandler = new CountDownLatch(1);
        in.clear();
        for (int j = 0; j < NUMMPS; j++) in.add(sendMessages(container, new KeyedMessage[] { km(new MockInputMessage("key" + j % NUMMPS)) }, DUPFACTOR));
        assertTrue(poll(o -> SendMessageThread.processingCount.get() == NUMMPS));
        // let the messages go.
        SendMessageThread.startLatch.countDown();
        // NUMMPS messages should be "dispatched"
        assertTrue(poll(o -> (clusterStats.getDispatchedMessageCount() - initialMessagesDispatched) == NUMMPS));
        // 1 message from each thread should be "in flight"
        assertTrue(poll(o -> container.getMessageWorkingCount() == NUMMPS));
        Thread.sleep(50);
        // NUMMPS messages should STILL be "dispatched"
        assertTrue(poll(o -> (clusterStats.getDispatchedMessageCount() - initialMessagesDispatched) == NUMMPS));
        // let the rest of them go
        commonLongRunningHandler.countDown();
        assertTrue("Timeout waiting on message to be sent", poll(o -> in.stream().filter(t -> t.isAlive() == true).count() == 0));
        // after sends are allowed to proceed
        assertEquals(NUMMPS, SendMessageThread.finishedCount.get());
        assertEquals(0, clusterStats.getInFlightMessageCount());
        assertEquals((DUPFACTOR * NUMMPS) * (iter + 1), dispatcher.messages.size());
        assertEquals((DUPFACTOR * NUMMPS) * (iter + 1) + numMessagePumped, clusterStats.getProcessedMessageCount());
        assertEquals(0L, clusterStats.getMessageCollisionCount());
        checkStat(clusterStats);
    }
}
Also used : MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Arrays(java.util.Arrays) Mp(net.dempsy.lifecycle.annotation.Mp) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) Dispatcher(net.dempsy.messages.Dispatcher) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) LoggerFactory(org.slf4j.LoggerFactory) MockOutputMessage(net.dempsy.container.mocks.MockOutputMessage) Random(java.util.Random) DummyInbound(net.dempsy.container.mocks.DummyInbound) MockInputMessage(net.dempsy.container.mocks.MockInputMessage) ArrayList(java.util.ArrayList) MessageResourceManager(net.dempsy.messages.MessageResourceManager) BasicClusterStatsCollector(net.dempsy.monitoring.basic.BasicClusterStatsCollector) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) After(org.junit.After) Manager(net.dempsy.Manager) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) Parameterized(org.junit.runners.Parameterized) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) StatsCollector(net.dempsy.monitoring.StatsCollector) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) Collection(java.util.Collection) Operation(net.dempsy.container.Container.Operation) KeyedMessage(net.dempsy.messages.KeyedMessage) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) LockingContainer(net.dempsy.container.locking.LockingContainer) Output(net.dempsy.lifecycle.annotation.Output) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClusterStatsCollector(net.dempsy.monitoring.ClusterStatsCollector) NodeStatsCollector(net.dempsy.monitoring.NodeStatsCollector) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Functional.chain(net.dempsy.util.Functional.chain) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) MockInputMessage(net.dempsy.container.mocks.MockInputMessage) KeyedMessage(net.dempsy.messages.KeyedMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 List (java.util.List)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Manager (net.dempsy.Manager)3 ServiceTracker (net.dempsy.ServiceTracker)3 ClusterId (net.dempsy.config.ClusterId)3 NonLockingAltContainer (net.dempsy.container.altnonlocking.NonLockingAltContainer)3 LockingContainer (net.dempsy.container.locking.LockingContainer)3 DummyInbound (net.dempsy.container.mocks.DummyInbound)3 MockInputMessage (net.dempsy.container.mocks.MockInputMessage)3 MockOutputMessage (net.dempsy.container.mocks.MockOutputMessage)3 MessageHandler (net.dempsy.lifecycle.annotation.MessageHandler)3 MessageProcessor (net.dempsy.lifecycle.annotation.MessageProcessor)3 Mp (net.dempsy.lifecycle.annotation.Mp)3 Output (net.dempsy.lifecycle.annotation.Output)3