Search in sources :

Example 16 with KeyedMessageWithType

use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.

the class TestSimple method testSimple.

@Test
public void testSimple() throws Exception {
    final AtomicLong count = new AtomicLong(0L);
    try (final NodeManager nm = new NodeManager();
        final DefaultThreadingModel tm = new DefaultThreadingModel("TB", -1, 1)) {
        final Node n = new Node.Builder("test-app").defaultRoutingStrategyId("net.dempsy.router.simple").receiver(new Dummy()).cluster("start").adaptor(new Adaptor() {

            private Dispatcher disp;

            boolean done = false;

            @Override
            public void stop() {
                done = true;
            }

            @Override
            public void start() {
                try {
                    while (!done) {
                        disp.dispatch(new KeyedMessageWithType(Integer.valueOf(1), "Hello", "string"));
                        // This is here for when the Container has a max pending and it gets starved for CPU cycles
                        // in this particular test.
                        Thread.yield();
                    }
                } catch (final InterruptedException ie) {
                    if (!done)
                        LOGGER.error("Interrupted but not stopping.");
                }
            }

            @Override
            public void setDispatcher(final Dispatcher dispatcher) {
                this.disp = dispatcher;
            }
        }).cluster("mp").mp(new MessageProcessor(MpFactory.make(() -> new Mp() {

            @Override
            public KeyedMessageWithType[] handle(final KeyedMessage message) {
                count.incrementAndGet();
                return null;
            }
        }, "string"))).build();
        nm.node(n).collaborator(new LocalClusterSessionFactory().createSession()).threadingModel(tm.start("nodeid"));
        nm.start();
        assertTrue(ConditionPoll.poll(o -> count.get() > 100000));
    }
}
Also used : Adaptor(net.dempsy.messages.Adaptor) Logger(org.slf4j.Logger) Node(net.dempsy.config.Node) MessageProcessor(net.dempsy.lifecycle.simple.MessageProcessor) Mp(net.dempsy.lifecycle.simple.Mp) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) Dispatcher(net.dempsy.messages.Dispatcher) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) NodeAddress(net.dempsy.transport.NodeAddress) Listener(net.dempsy.transport.Listener) LoggerFactory(org.slf4j.LoggerFactory) KeyedMessage(net.dempsy.messages.KeyedMessage) Assert.assertTrue(org.junit.Assert.assertTrue) ConditionPoll(net.dempsy.utils.test.ConditionPoll) Test(org.junit.Test) MpFactory(net.dempsy.lifecycle.simple.MpFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) Adaptor(net.dempsy.messages.Adaptor) MessageTransportException(net.dempsy.transport.MessageTransportException) Receiver(net.dempsy.transport.Receiver) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) Mp(net.dempsy.lifecycle.simple.Mp) Node(net.dempsy.config.Node) MessageProcessor(net.dempsy.lifecycle.simple.MessageProcessor) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) Dispatcher(net.dempsy.messages.Dispatcher) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) AtomicLong(java.util.concurrent.atomic.AtomicLong) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) KeyedMessage(net.dempsy.messages.KeyedMessage) Test(org.junit.Test)

Example 17 with KeyedMessageWithType

use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.

the class TestSimpleRoutingStrategy method testInboundWithOutbound.

@Test
public void testInboundWithOutbound() throws Exception {
    final Manager<RoutingStrategy.Inbound> manager = new Manager<>(RoutingStrategy.Inbound.class);
    try (final RoutingStrategy.Inbound ib = manager.getAssociatedInstance(SimpleRoutingStrategy.class.getPackage().getName())) {
        assertNotNull(ib);
        assertTrue(SimpleInboundSide.class.isAssignableFrom(ib.getClass()));
        final ClusterId cid = new ClusterId("test", "test");
        ib.setContainerDetails(cid, new ContainerAddress(new DummyNodeAddress("here"), 0), (l, m) -> {
        });
        ib.start(infra);
        assertTrue(waitForReg(session));
        try (final ClusterInfoSession ses2 = sessFact.createSession()) {
            try (final RoutingStrategyManager obman = chain(new RoutingStrategyManager(), o -> o.start(makeInfra(ses2, sched)));
                final RoutingStrategy.Factory obf = obman.getAssociatedInstance(SimpleRoutingStrategy.class.getPackage().getName())) {
                obf.start(makeInfra(ses2, sched));
                final RoutingStrategy.Router ob = obf.getStrategy(cid);
                final KeyedMessageWithType km = new KeyedMessageWithType(null, null, "");
                assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
                final ContainerAddress ca = ob.selectDestinationForMessage(km);
                assertNotNull(ca);
                assertEquals("here", ((DummyNodeAddress) ca.node).name);
                // now distupt the session
                session.close();
                // the destination should clear until a new in runs
                assertTrue(poll(o -> ob.selectDestinationForMessage(km) == null));
                try (ClusterInfoSession ses3 = sessFact.createSession();
                    RoutingStrategy.Inbound ib2 = manager.getAssociatedInstance(SimpleRoutingStrategy.class.getPackage().getName())) {
                    ib2.setContainerDetails(cid, ca, (l, m) -> {
                    });
                    ib2.start(makeInfra(ses3, sched));
                    assertTrue(poll(o -> ob.selectDestinationForMessage(km) != null));
                }
            }
        }
    }
}
Also used : ClusterInfoException(net.dempsy.cluster.ClusterInfoException) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) Assert.assertNotNull(org.junit.Assert.assertNotNull) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) NodeAddress(net.dempsy.transport.NodeAddress) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) AutoDisposeSingleThreadScheduler(net.dempsy.util.executor.AutoDisposeSingleThreadScheduler) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Infrastructure(net.dempsy.Infrastructure) RoutingStrategy(net.dempsy.router.RoutingStrategy) Functional.chain(net.dempsy.util.Functional.chain) After(org.junit.After) Manager(net.dempsy.Manager) TestInfrastructure(net.dempsy.util.TestInfrastructure) Assert.assertEquals(org.junit.Assert.assertEquals) ClusterId(net.dempsy.config.ClusterId) Before(org.junit.Before) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) ClusterId(net.dempsy.config.ClusterId) RoutingStrategyManager(net.dempsy.router.RoutingStrategyManager) Manager(net.dempsy.Manager) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) RoutingStrategy(net.dempsy.router.RoutingStrategy) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) Test(org.junit.Test)

Example 18 with KeyedMessageWithType

use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.

the class TestInstanceManager method testSingleInstanceTwoMessagesDifferentClassSeparateExecution.

@Test
public void testSingleInstanceTwoMessagesDifferentClassSeparateExecution() throws Exception {
    final CombinedMP prototype = new CombinedMP();
    try (final LockingContainer manager = setupContainer(new MessageProcessor<CombinedMP>(prototype))) {
        final DummyDispatcher dispatcher = ((DummyDispatcher) manager.getDispatcher());
        assertEquals("starts with no instances", 0, manager.getProcessorCount());
        final KeyedMessageWithType message1 = km(new MessageOne(123));
        final InstanceWrapper wrapper = manager.getInstanceForKey(message1.key, message1.message);
        manager.dispatch(message1, Operation.handle, true);
        final CombinedMP instance = (CombinedMP) wrapper.getInstance();
        assertEquals("instance was created", 1, manager.getProcessorCount());
        assertEquals("instance activated", 1, instance.activationCount);
        assertTrue("real activation time", instance.activationTime > 0);
        assertSame("instance received message", message1.message, instance.messages.get(0));
        assertEquals("message count", 1, instance.messages.size());
        assertTrue("activated before first message", instance.activationTime < instance.firstMessageTime);
        assertEquals(new ReturnString("MessageOne"), dispatcher.lastDispatched.message);
        final KeyedMessageWithType message2 = km(new MessageTwo(123));
        assertSame("same wrapper returned for second message", wrapper, manager.getInstanceForKey(message2.key, message2.message));
        manager.dispatch(message2, Operation.handle, true);
        assertEquals("no other instance was created", 1, manager.getProcessorCount());
        assertEquals("no second activation", 1, instance.activationCount);
        assertEquals("both messages delivered", 2, instance.messages.size());
        assertSame("message1 delivered first", message1.message, instance.messages.get(0));
        assertSame("message2 delivered second", message2.message, instance.messages.get(1));
        assertEquals(new ReturnString("MessageTwo"), dispatcher.lastDispatched.message);
    }
}
Also used : InstanceWrapper(net.dempsy.container.locking.LockingContainer.InstanceWrapper) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) Test(org.junit.Test)

Example 19 with KeyedMessageWithType

use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.

the class TestInstanceManager method testOutputShortCircuitsIfNoOutputMethod.

@Test
public void testOutputShortCircuitsIfNoOutputMethod() throws Exception {
    final CombinedMP prototype = new CombinedMP();
    final Container manager = setupContainer(new MessageProcessor<CombinedMP>(prototype));
    final DummyDispatcher dispatcher = ((DummyDispatcher) manager.getDispatcher());
    // we need to dispatch messages to create MP instances
    final KeyedMessageWithType message1 = km(new MessageOne(1));
    final KeyedMessageWithType message2 = km(new MessageOne(2));
    manager.dispatch(message1, Operation.handle, true);
    manager.dispatch(message2, Operation.handle, true);
    assertEquals(new ReturnString("MessageOne"), dispatcher.lastDispatched.message);
    manager.invokeOutput();
    // output messages are NOT considered "processed" if there is no output method on the MP.
    assertEquals("number of processed messages should include outputs.", 2, ((ClusterMetricGetters) statsCollector).getProcessedMessageCount());
}
Also used : NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) Container(net.dempsy.container.Container) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) Test(org.junit.Test)

Example 20 with KeyedMessageWithType

use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.

the class TestInstanceManager method testOutputCountsOkay.

@Test
public void testOutputCountsOkay() throws Exception {
    final OutputTestMP prototype = new OutputTestMP();
    try (final Container manager = setupContainer(new MessageProcessor<OutputTestMP>(prototype))) {
        final DummyDispatcher dispatcher = ((DummyDispatcher) manager.getDispatcher());
        // we need to dispatch messages to create MP instances
        final KeyedMessageWithType message1 = km(new MessageOne(1));
        final KeyedMessageWithType message2 = km(new MessageOne(2));
        manager.dispatch(message1, Operation.handle, true);
        manager.dispatch(message2, Operation.handle, true);
        assertEquals(new ReturnString("MessageOne"), dispatcher.lastDispatched.message);
        manager.invokeOutput();
        assertEquals("number of processed messages should include outputs.", 4, ((ClusterMetricGetters) statsCollector).getProcessedMessageCount());
    }
}
Also used : Container(net.dempsy.container.Container) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) Test(org.junit.Test)

Aggregations

KeyedMessageWithType (net.dempsy.messages.KeyedMessageWithType)26 Test (org.junit.Test)21 InstanceWrapper (net.dempsy.container.locking.LockingContainer.InstanceWrapper)7 NodeAddress (net.dempsy.transport.NodeAddress)5 DempsyException (net.dempsy.DempsyException)4 Container (net.dempsy.container.Container)4 RoutingStrategy (net.dempsy.router.RoutingStrategy)4 ContainerAddress (net.dempsy.router.RoutingStrategy.ContainerAddress)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 Infrastructure (net.dempsy.Infrastructure)3 Manager (net.dempsy.Manager)3 ClusterInfoException (net.dempsy.cluster.ClusterInfoException)3 ClusterInfoSession (net.dempsy.cluster.ClusterInfoSession)3 ClusterId (net.dempsy.config.ClusterId)3 ContainerException (net.dempsy.container.ContainerException)3 KeyedMessage (net.dempsy.messages.KeyedMessage)3 RoutingStrategyManager (net.dempsy.router.RoutingStrategyManager)3