Search in sources :

Example 6 with SystemPropertyManager

use of net.dempsy.utils.test.SystemPropertyManager in project Dempsy by Dempsy.

the class TestWordCount method testWordCountWithRank.

@Test
public void testWordCountWithRank() throws Throwable {
    try (@SuppressWarnings("resource") final SystemPropertyManager props = new SystemPropertyManager().set("min_nodes", "1")) {
        final String[][] ctxs = { { "classpath:/word-count/adaptor-kjv.xml", "classpath:/word-count/mp-word-count.xml", "classpath:/word-count/mp-word-rank.xml" } };
        // need to make it wait.
        WordProducer.latch = new CountDownLatch(1);
        runCombos("testWordCountWithRank", ctxs, n -> {
            final List<NodeManagerWithContext> nodes = n.nodes;
            final NodeManager manager = nodes.get(0).manager;
            // wait until I can reach the cluster from the adaptor.
            assertTrue(poll(o -> manager.getRouter().allReachable("test-cluster1").size() == 1));
            final ClassPathXmlApplicationContext ctx = nodes.get(0).ctx;
            final WordProducer adaptor;
            final ClusterMetricGetters stats;
            WordProducer.latch.countDown();
            adaptor = ctx.getBean(WordProducer.class);
            stats = (ClusterMetricGetters) manager.getClusterStatsCollector(new ClusterId(currentAppName, "test-cluster1"));
            assertTrue(poll(o -> adaptor.done.get()));
            assertTrue(poll(o -> adaptor.numDispatched == stats.getProcessedMessageCount()));
            // wait until all of the counts are also passed to WordRank
            final ClusterMetricGetters wrStats = (ClusterMetricGetters) manager.getClusterStatsCollector(new ClusterId(currentAppName, "test-cluster2"));
            assertTrue(poll(wrStats, s -> adaptor.numDispatched == s.getProcessedMessageCount()));
            stopSystem();
            // pull the Rank mp from the manager
            final MessageProcessorLifecycle<?> mp = AccessUtil.getMp(manager, "test-cluster2");
            @SuppressWarnings("unchecked") final WordRank prototype = ((MessageProcessor<WordRank>) mp).getPrototype();
            final List<Rank> ranks = prototype.getPairs();
            Collections.sort(ranks, (o1, o2) -> o2.rank.compareTo(o1.rank));
            final List<Rank> top10 = ranks.subList(0, 10);
            top10.forEach(r -> assertTrue(finalResults.contains(r.word)));
        });
    }
}
Also used : MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) Mp(net.dempsy.lifecycle.annotation.Mp) BufferedInputStream(java.io.BufferedInputStream) MessageType(net.dempsy.lifecycle.annotation.MessageType) Dispatcher(net.dempsy.messages.Dispatcher) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SystemPropertyManager(net.dempsy.utils.test.SystemPropertyManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) ThreadingModel(net.dempsy.threading.ThreadingModel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) AfterClass(org.junit.AfterClass) MessageKey(net.dempsy.lifecycle.annotation.MessageKey) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) NodeMetricGetters(net.dempsy.container.NodeMetricGetters) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) StringWriter(java.io.StringWriter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterMetricGetters(net.dempsy.container.ClusterMetricGetters) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Activation(net.dempsy.lifecycle.annotation.Activation) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) Output(net.dempsy.lifecycle.annotation.Output) CountDownLatch(java.util.concurrent.CountDownLatch) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Adaptor(net.dempsy.messages.Adaptor) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) ConditionPoll.assertTrue(net.dempsy.utils.test.ConditionPoll.assertTrue) MessageProcessorLifecycle(net.dempsy.messages.MessageProcessorLifecycle) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) ClusterId(net.dempsy.config.ClusterId) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) CountDownLatch(java.util.concurrent.CountDownLatch) SystemPropertyManager(net.dempsy.utils.test.SystemPropertyManager) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClusterMetricGetters(net.dempsy.container.ClusterMetricGetters) Test(org.junit.Test)

Example 7 with SystemPropertyManager

use of net.dempsy.utils.test.SystemPropertyManager in project Dempsy by Dempsy.

the class TestWordCount method runTestWithOutputCycle.

private void runTestWithOutputCycle(final String testName, final String outputSchedCtx) throws Exception {
    final String[][] ctxs = { // adaptor only node
    { "classpath:/word-count/adaptor-kjv.xml" }, { "classpath:/word-count/mp-word-count.xml", "classpath:/word-count/mp-word-count-output.xml", outputSchedCtx }, { "classpath:/word-count/mp-word-count.xml", "classpath:/word-count/mp-word-count-output.xml", outputSchedCtx }, { "classpath:/word-count/mp-rank-catcher.xml" } };
    // the adaptor is the first one, rank catcher the last.
    final int NUM_WC = ctxs.length - 2;
    try (@SuppressWarnings("resource") final SystemPropertyManager props = new SystemPropertyManager().set("min_nodes", Integer.toString(NUM_WC)).set("routing-group", ":group").set("send_threads", "1").set("receive_threads", "1").set("blocking-queue-size", "500000")) {
        // need to make it wait.
        WordProducer.latch = new CountDownLatch(1);
        runCombos(testName, (r, c, s, t, ser) -> isElasticRoutingStrategy(r), ctxs, n -> {
            final List<NodeManagerWithContext> nodes = n.nodes;
            final NodeManager[] managers = nodes.stream().map(nm -> nm.manager).toArray(NodeManager[]::new);
            // wait until I can reach the cluster from the adaptor.
            assertTrue(poll(o -> managers[0].getRouter().allReachable("test-cluster1").size() == NUM_WC));
            assertTrue(poll(o -> managers[0].getRouter().allReachable("test-cluster2").size() == NUM_WC));
            assertTrue(poll(o -> managers[0].getRouter().allReachable("test-cluster3").size() == 1));
            for (int i = 0; i < NUM_WC; i++) {
                // the +1 is because the first (0th) manager is the adaptor
                final int managerIndex = i + 1;
                assertTrue(poll(o -> managers[managerIndex].getRouter().allReachable("test-cluster3").size() == 1));
            }
            WordProducer.latch.countDown();
            final WordProducer adaptor = nodes.get(0).ctx.getBean(WordProducer.class);
            waitForAllSent(adaptor);
            // the last node has the RankCatcher
            final NodeManager manager = nodes.get(nodes.size() - 1).manager;
            final MessageProcessorLifecycle<?> mp = AccessUtil.getMp(manager, "test-cluster3");
            @SuppressWarnings("unchecked") final RankCatcher prototype = ((MessageProcessor<RankCatcher>) mp).getPrototype();
            final HashSet<String> expected = new HashSet<>(Arrays.asList("the", "that", "unto", "in", "and", "And", "of", "shall", "to", "he"));
            assertTrue(() -> {
                return "FAILURE:" + expected.toString() + " != " + prototype.topRef.get();
            }, poll(prototype.topRef, tr -> {
                final List<Rank> cur = tr.get();
                final HashSet<String> topSet = new HashSet<>(cur.stream().map(r -> r.word).collect(Collectors.toSet()));
                // once more than 1/2 of the list is there then we're done.
                final int threshold = (expected.size() / 2) + 1;
                int matches = 0;
                for (final String exp : expected) if (topSet.contains(exp))
                    matches++;
                return matches >= threshold;
            }));
        });
    }
}
Also used : MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) Mp(net.dempsy.lifecycle.annotation.Mp) BufferedInputStream(java.io.BufferedInputStream) MessageType(net.dempsy.lifecycle.annotation.MessageType) Dispatcher(net.dempsy.messages.Dispatcher) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SystemPropertyManager(net.dempsy.utils.test.SystemPropertyManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) ThreadingModel(net.dempsy.threading.ThreadingModel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) AfterClass(org.junit.AfterClass) MessageKey(net.dempsy.lifecycle.annotation.MessageKey) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) NodeMetricGetters(net.dempsy.container.NodeMetricGetters) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) StringWriter(java.io.StringWriter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterMetricGetters(net.dempsy.container.ClusterMetricGetters) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Activation(net.dempsy.lifecycle.annotation.Activation) KeyExtractor(net.dempsy.lifecycle.annotation.utils.KeyExtractor) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) Output(net.dempsy.lifecycle.annotation.Output) CountDownLatch(java.util.concurrent.CountDownLatch) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Adaptor(net.dempsy.messages.Adaptor) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) ConditionPoll.assertTrue(net.dempsy.utils.test.ConditionPoll.assertTrue) MessageProcessorLifecycle(net.dempsy.messages.MessageProcessorLifecycle) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) CountDownLatch(java.util.concurrent.CountDownLatch) SystemPropertyManager(net.dempsy.utils.test.SystemPropertyManager) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 8 with SystemPropertyManager

use of net.dempsy.utils.test.SystemPropertyManager in project Dempsy by Dempsy.

the class TestContainer method setUp.

@Before
public void setUp() throws Exception {
    justThrowMe = null;
    throwMeInActivation = null;
    track(new SystemPropertyManager()).set("container-type", containerId);
    context = track(new ClassPathXmlApplicationContext(ctx));
    sessionFactory = new LocalClusterSessionFactory();
    final Node node = context.getBean(Node.class);
    manager = track(new NodeManager()).node(node).collaborator(track(sessionFactory.createSession())).start();
    statsCollector = manager.getClusterStatsCollector(new ClusterId("test-app", "test-cluster"));
    container = manager.getContainers().get(0);
    assertTrue(poll(manager, m -> m.isReady()));
}
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) SystemPropertyManager(net.dempsy.utils.test.SystemPropertyManager) NodeManager(net.dempsy.NodeManager) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClusterId(net.dempsy.config.ClusterId) Node(net.dempsy.config.Node) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) Before(org.junit.Before)

Aggregations

ArrayList (java.util.ArrayList)8 Arrays (java.util.Arrays)8 List (java.util.List)8 ConditionPoll.poll (net.dempsy.utils.test.ConditionPoll.poll)8 SystemPropertyManager (net.dempsy.utils.test.SystemPropertyManager)8 Assert.assertTrue (org.junit.Assert.assertTrue)8 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 Collections (java.util.Collections)7 HashSet (java.util.HashSet)7 Map (java.util.Map)7 Set (java.util.Set)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Function (java.util.function.Function)7 Collectors (java.util.stream.Collectors)7 ClusterId (net.dempsy.config.ClusterId)7 Activation (net.dempsy.lifecycle.annotation.Activation)7 MessageHandler (net.dempsy.lifecycle.annotation.MessageHandler)7 MessageKey (net.dempsy.lifecycle.annotation.MessageKey)7