Search in sources :

Example 1 with TestInfrastructure

use of net.dempsy.util.TestInfrastructure in project Dempsy by Dempsy.

the class BlockingQueueTest method testBlockingQueueOverflow.

/**
 * Test overflow for a blocking Transport around a queue with depth one. While the transport will not call the, and does not even have a , overflow handler,
 * every message will call the overflow handler on
 * the receiver since the queue is always full.
 *
 * @throws Throwable
 */
@Test
public void testBlockingQueueOverflow() throws Throwable {
    final AtomicReference<String> message = new AtomicReference<String>(null);
    final ArrayBlockingQueue<Object> input = new ArrayBlockingQueue<>(1);
    try (@SuppressWarnings("resource") SystemPropertyManager // test only works when the blocking queue blocks
    props = new SystemPropertyManager().set(BlockingQueueSenderFactory.class.getPackageName() + "." + BlockingQueueSenderFactory.BLOCKING_KEY, "true");
        final TestInfrastructure infra = new TestInfrastructure(new DefaultThreadingModel("BQTest-testBlockingQueueOverflow-"));
        final Receiver r = new BlockingQueueReceiver(input);
        final TransportManager tranMan = chain(new TransportManager(), c -> c.start(infra));
        final SenderFactory sf = tranMan.getAssociatedInstance(transportTypeId)) {
        final Sender sender = sf.getSender(r.getAddress(infra));
        final AtomicBoolean finallySent = new AtomicBoolean(false);
        final AtomicLong receiveCount = new AtomicLong();
        // fill up queue
        sender.send("Hello");
        final Thread t = new Thread(() -> {
            try {
                sender.send("Hello again");
            } catch (final MessageTransportException | InterruptedException e) {
                throw new RuntimeException(e);
            }
            finallySent.set(true);
        });
        t.start();
        Thread.sleep(100);
        // the thread should be hung blocked on the send
        assertFalse(finallySent.get());
        // Start the receiver to read
        r.start((final String msg) -> {
            message.set(new String(msg));
            receiveCount.incrementAndGet();
            return true;
        }, infra);
        // 2 messages should have been read and the 2nd should be "Hello again"
        assertTrue(poll(o -> "Hello again".equals(message.get())));
        // The thread should shut down eventually
        assertTrue(poll(o -> !t.isAlive()));
    }
}
Also used : Sender(net.dempsy.transport.Sender) TransportManager(net.dempsy.transport.TransportManager) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) SenderFactory(net.dempsy.transport.SenderFactory) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Assert.assertFalse(org.junit.Assert.assertFalse) Functional.chain(net.dempsy.util.Functional.chain) TestInfrastructure(net.dempsy.util.TestInfrastructure) MessageTransportException(net.dempsy.transport.MessageTransportException) Receiver(net.dempsy.transport.Receiver) TestInfrastructure(net.dempsy.util.TestInfrastructure) Receiver(net.dempsy.transport.Receiver) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) Sender(net.dempsy.transport.Sender) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) MessageTransportException(net.dempsy.transport.MessageTransportException) SenderFactory(net.dempsy.transport.SenderFactory) TransportManager(net.dempsy.transport.TransportManager) Test(org.junit.Test)

Example 2 with TestInfrastructure

use of net.dempsy.util.TestInfrastructure in project Dempsy by Dempsy.

the class TestManagedRoutingStrategy method testInboundDoubleHappyPathRegister.

@Test
public void testInboundDoubleHappyPathRegister() throws Exception {
    final int numShardsToExpect = Integer.parseInt(Utils.DEFAULT_TOTAL_SHARDS);
    try (final RoutingStrategy.Inbound ib1 = new Manager<>(RoutingStrategy.Inbound.class).getAssociatedInstance(ManagedInbound.class.getPackage().getName());
        final RoutingStrategy.Inbound ib2 = new Manager<>(RoutingStrategy.Inbound.class).getAssociatedInstance(ManagedInbound.class.getPackage().getName())) {
        final ClusterId clusterId = new ClusterId("test", "test");
        final ContainerAddress node1Ca = new ContainerAddress(new DummyNodeAddress("node1"), 0);
        final Utils<ContainerAddress> utils = new Utils<>(infra, clusterId.clusterName, node1Ca);
        ib1.setContainerDetails(clusterId, node1Ca, (l, m) -> {
        });
        ib1.start(infra);
        final ContainerAddress node2Ca = new ContainerAddress(new DummyNodeAddress("node2"), 0);
        ib2.setContainerDetails(clusterId, node2Ca, (l, m) -> {
        });
        try (final ClusterInfoSession session2 = sessFact.createSession()) {
            ib2.start(new TestInfrastructure(session2, infra.getScheduler()));
            assertTrue(waitForShards(session, utils, numShardsToExpect));
            // if this worked right then numShardsToExpect/2 should be owned by each ... eventually.
            checkForShardDistribution(session, utils, numShardsToExpect, 2);
            // disrupt the session. This should cause a reshuffle but not fail
            disruptor.accept(session2);
            // everything should settle back
            checkForShardDistribution(session, utils, numShardsToExpect, 2);
            // now kill the second session.
            // this will disconnect the second Inbound and so the first should take over
            session2.close();
            // see if we now have 1 session and it has all shards
            checkForShardDistribution(session, utils, numShardsToExpect, 1);
        }
    }
}
Also used : TestInfrastructure(net.dempsy.util.TestInfrastructure) ClusterId(net.dempsy.config.ClusterId) Utils(net.dempsy.router.shardutils.Utils) RoutingStrategy(net.dempsy.router.RoutingStrategy) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) Test(org.junit.Test)

Example 3 with TestInfrastructure

use of net.dempsy.util.TestInfrastructure in project Dempsy by Dempsy.

the class TcpTransportTest method testConnectionRecovery.

@Test
public void testConnectionRecovery() throws Exception {
    try (final ServiceTracker tr = new ServiceTracker()) {
        final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).numHandlers(2).useLocalHost(true);
        // can't test connection recovery here.
        if (!(r instanceof DisruptableRecevier))
            return;
        final ThreadingModel tm = tr.track(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testConnectionRecovery"));
        final Infrastructure infra = tr.track(new TestInfrastructure(tm));
        final TcpAddress addr = r.getAddress(infra);
        LOGGER.debug(addr.toString());
        final AtomicReference<RoutedMessage> rm = new AtomicReference<>(null);
        r.start((Listener<RoutedMessage>) msg -> {
            rm.set(msg);
            return true;
        }, infra);
        try (final SenderFactory sf = senderFactory.get()) {
            sf.start(new TestInfrastructure(tm) {

                @Override
                public String getNodeId() {
                    return "test";
                }
            });
            final Sender sender = sf.getSender(addr);
            sender.send(new RoutedMessage(new int[] { 0 }, "Hello", "Hello"));
            assertTrue(poll(o -> rm.get() != null));
            assertEquals("Hello", rm.get().message);
            assertTrue(((DisruptableRecevier) r).disrupt(addr));
            final AtomicBoolean stop = new AtomicBoolean(false);
            final RoutedMessage resetMessage = new RoutedMessage(new int[] { 0 }, "RESET", "RESET");
            final Thread senderThread = new Thread(() -> {
                try {
                    while (!stop.get()) {
                        sender.send(resetMessage);
                        dontInterrupt(() -> Thread.sleep(100));
                    }
                } catch (final InterruptedException ie) {
                    if (!stop.get())
                        LOGGER.error("Interrupted send");
                }
            }, "testConnectionRecovery-sender");
            senderThread.start();
            try {
                assertTrue(poll(o -> "RESET".equals(rm.get().message)));
            } finally {
                stop.set(true);
                if (!poll(senderThread, t -> {
                    dontInterrupt(() -> t.join(10000));
                    return !t.isAlive();
                }))
                    LOGGER.error("FAILED TO SHUT DOWN TEST SENDING THREAD. THREAD LEAK!");
            }
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) NioReceiver(net.dempsy.transport.tcp.nio.NioReceiver) Arrays(java.util.Arrays) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) NioUtils.dontInterrupt(net.dempsy.transport.tcp.nio.internal.NioUtils.dontInterrupt) Listener(net.dempsy.transport.Listener) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) JsonSerializer(net.dempsy.serialization.jackson.JsonSerializer) KryoSerializer(net.dempsy.serialization.kryo.KryoSerializer) Serializer(net.dempsy.serialization.Serializer) Map(java.util.Map) ThreadingModel(net.dempsy.threading.ThreadingModel) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) DisruptableRecevier(net.dempsy.transport.DisruptableRecevier) Receiver(net.dempsy.transport.Receiver) Parameterized(org.junit.runners.Parameterized) NioSenderFactory(net.dempsy.transport.tcp.nio.NioSenderFactory) Sender(net.dempsy.transport.Sender) Logger(org.slf4j.Logger) Collection(java.util.Collection) Functional.uncheck(net.dempsy.util.Functional.uncheck) NetworkInterface(java.net.NetworkInterface) SenderFactory(net.dempsy.transport.SenderFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Inet4Address(java.net.Inet4Address) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestWordCount(net.dempsy.TestWordCount) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Infrastructure(net.dempsy.Infrastructure) Functional.chain(net.dempsy.util.Functional.chain) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) RoutedMessage(net.dempsy.transport.RoutedMessage) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) RoutedMessage(net.dempsy.transport.RoutedMessage) DisruptableRecevier(net.dempsy.transport.DisruptableRecevier) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) Sender(net.dempsy.transport.Sender) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) ThreadingModel(net.dempsy.threading.ThreadingModel) TestInfrastructure(net.dempsy.util.TestInfrastructure) Infrastructure(net.dempsy.Infrastructure) NioSenderFactory(net.dempsy.transport.tcp.nio.NioSenderFactory) SenderFactory(net.dempsy.transport.SenderFactory) Test(org.junit.Test)

Example 4 with TestInfrastructure

use of net.dempsy.util.TestInfrastructure in project Dempsy by Dempsy.

the class TcpTransportTest method testMessage.

@Test
public void testMessage() throws Exception {
    try (ServiceTracker tr = new ServiceTracker()) {
        final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).numHandlers(2).useLocalHost(true);
        final ThreadingModel tm = tr.track(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testMessage"));
        final Infrastructure infra = tr.track(new TestInfrastructure(tm));
        final TcpAddress addr = r.getAddress(infra);
        LOGGER.debug(addr.toString());
        final AtomicReference<RoutedMessage> rm = new AtomicReference<>(null);
        r.start((Listener<RoutedMessage>) msg -> {
            rm.set(msg);
            return true;
        }, infra);
        try (final SenderFactory sf = senderFactory.get()) {
            sf.start(new TestInfrastructure(tm) {

                @Override
                public String getNodeId() {
                    return "test";
                }
            });
            final Sender sender = sf.getSender(addr);
            sender.send(new RoutedMessage(new int[] { 0 }, "Hello", "Hello"));
            assertTrue(poll(o -> rm.get() != null));
            assertEquals("Hello", rm.get().message);
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) NioReceiver(net.dempsy.transport.tcp.nio.NioReceiver) Arrays(java.util.Arrays) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) NioUtils.dontInterrupt(net.dempsy.transport.tcp.nio.internal.NioUtils.dontInterrupt) Listener(net.dempsy.transport.Listener) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) JsonSerializer(net.dempsy.serialization.jackson.JsonSerializer) KryoSerializer(net.dempsy.serialization.kryo.KryoSerializer) Serializer(net.dempsy.serialization.Serializer) Map(java.util.Map) ThreadingModel(net.dempsy.threading.ThreadingModel) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) DisruptableRecevier(net.dempsy.transport.DisruptableRecevier) Receiver(net.dempsy.transport.Receiver) Parameterized(org.junit.runners.Parameterized) NioSenderFactory(net.dempsy.transport.tcp.nio.NioSenderFactory) Sender(net.dempsy.transport.Sender) Logger(org.slf4j.Logger) Collection(java.util.Collection) Functional.uncheck(net.dempsy.util.Functional.uncheck) NetworkInterface(java.net.NetworkInterface) SenderFactory(net.dempsy.transport.SenderFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Inet4Address(java.net.Inet4Address) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestWordCount(net.dempsy.TestWordCount) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Infrastructure(net.dempsy.Infrastructure) Functional.chain(net.dempsy.util.Functional.chain) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) RoutedMessage(net.dempsy.transport.RoutedMessage) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) RoutedMessage(net.dempsy.transport.RoutedMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) Sender(net.dempsy.transport.Sender) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) ThreadingModel(net.dempsy.threading.ThreadingModel) TestInfrastructure(net.dempsy.util.TestInfrastructure) Infrastructure(net.dempsy.Infrastructure) NioSenderFactory(net.dempsy.transport.tcp.nio.NioSenderFactory) SenderFactory(net.dempsy.transport.SenderFactory) Test(org.junit.Test)

Example 5 with TestInfrastructure

use of net.dempsy.util.TestInfrastructure in project Dempsy by Dempsy.

the class TestInstanceManager method setupContainer.

@SuppressWarnings("resource")
public Container setupContainer(final MessageProcessorLifecycle<?> prototype) throws ContainerException {
    dispatcher = new DummyDispatcher();
    statsCollector = new BasicClusterStatsCollector();
    manager = new NonLockingAltContainer().setMessageProcessor(prototype).setClusterId(new ClusterId("test", "test"));
    manager.setDispatcher(dispatcher);
    manager.setInbound(new DummyInbound());
    tm = new DefaultThreadingModel(TestInstanceManager.class.getName());
    tm.start(TestInstanceManager.class.getName());
    manager.start(new TestInfrastructure(tm) {

        BasicNodeStatsCollector nStats = new BasicNodeStatsCollector();

        @Override
        public ClusterStatsCollector getClusterStatsCollector(final ClusterId clusterId) {
            return statsCollector;
        }

        @Override
        public NodeStatsCollector getNodeStatsCollector() {
            return nStats;
        }
    });
    return manager;
}
Also used : DummyInbound(net.dempsy.container.mocks.DummyInbound) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) NodeStatsCollector(net.dempsy.monitoring.NodeStatsCollector) TestInfrastructure(net.dempsy.util.TestInfrastructure) BasicClusterStatsCollector(net.dempsy.monitoring.basic.BasicClusterStatsCollector) ClusterStatsCollector(net.dempsy.monitoring.ClusterStatsCollector) ClusterId(net.dempsy.config.ClusterId) BasicClusterStatsCollector(net.dempsy.monitoring.basic.BasicClusterStatsCollector) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector)

Aggregations

TestInfrastructure (net.dempsy.util.TestInfrastructure)17 Test (org.junit.Test)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 ConditionPoll.poll (net.dempsy.utils.test.ConditionPoll.poll)11 Assert.assertTrue (org.junit.Assert.assertTrue)11 DefaultThreadingModel (net.dempsy.threading.DefaultThreadingModel)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Receiver (net.dempsy.transport.Receiver)7 Sender (net.dempsy.transport.Sender)7 SenderFactory (net.dempsy.transport.SenderFactory)7 Functional.chain (net.dempsy.util.Functional.chain)7 Infrastructure (net.dempsy.Infrastructure)6 ServiceTracker (net.dempsy.ServiceTracker)6 NonLockingAltContainer (net.dempsy.container.altnonlocking.NonLockingAltContainer)6 Inet4Address (java.net.Inet4Address)5 NetworkInterface (java.net.NetworkInterface)5 Arrays (java.util.Arrays)5 Collection (java.util.Collection)5 Collections (java.util.Collections)5