Search in sources :

Example 6 with TestInfrastructure

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

the class TestOutputSchedulers method testCronScheduleWithConcurrencySetting.

/**
 * Test cron schedule with concurrency setting
 *
 * @throws Exception the exception
 */
@Test
public void testCronScheduleWithConcurrencySetting() throws Exception {
    try (final CronOutputSchedule cronOutputSchedule = new CronOutputSchedule("0/1 * * * * ?")) {
        cronOutputSchedule.setOutputInvoker(container);
        cronOutputSchedule.start(new TestInfrastructure(null));
        Thread.sleep(1000);
        assertTrue(poll(outputInvoked, oi -> oi.get()));
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) TestInfrastructure(net.dempsy.util.TestInfrastructure) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Container(net.dempsy.container.Container) Test(org.junit.Test) Before(org.junit.Before) TestInfrastructure(net.dempsy.util.TestInfrastructure) Test(org.junit.Test)

Example 7 with TestInfrastructure

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

the class TestGroupRoutingStrategy method testInboundDoubleHappyPathRegister.

@Test
public void testInboundDoubleHappyPathRegister() throws Exception {
    final int numShardsToExpect = Integer.parseInt(Utils.DEFAULT_TOTAL_SHARDS);
    final String groupName = "testInboundDoubleHappyPathRegister";
    try (final RoutingStrategy.Inbound ib1 = new RoutingInboundManager().getAssociatedInstance(ClusterGroupInbound.class.getPackage().getName() + ":" + groupName);
        final RoutingStrategy.Inbound ib2 = new RoutingInboundManager().getAssociatedInstance(ClusterGroupInbound.class.getPackage().getName() + ":" + groupName)) {
        final ClusterId clusterId = new ClusterId("test", "test");
        final NodeAddress node1Addr = new DummyNodeAddress("node1");
        final GroupDetails gd1 = new GroupDetails(groupName, node1Addr);
        final ContainerAddress node1Ca = new ContainerAddress(node1Addr, 0);
        final Utils<GroupDetails> utils = new Utils<>(infra, groupName, gd1);
        ib1.setContainerDetails(clusterId, node1Ca, (l, m) -> {
        });
        ib1.start(infra);
        final NodeAddress node2Addr = new DummyNodeAddress("node2");
        final ContainerAddress node2Ca = new ContainerAddress(node2Addr, 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) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) RoutingInboundManager(net.dempsy.router.RoutingInboundManager) GroupDetails(net.dempsy.router.group.intern.GroupDetails) Utils(net.dempsy.router.shardutils.Utils) RoutingStrategy(net.dempsy.router.RoutingStrategy) NodeAddress(net.dempsy.transport.NodeAddress) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) Test(org.junit.Test)

Example 8 with TestInfrastructure

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

the class TestContainerLoadHandling method setUp.

@Before
public void setUp() throws Exception {
    final ClusterId cid = new ClusterId("TestContainerLoadHandling", "test" + sequence++);
    dispatcher = new MockDispatcher();
    final BasicClusterStatsCollector sc = new BasicClusterStatsCollector();
    clusterStats = sc;
    nodeStats = new BasicNodeStatsCollector();
    container = tr.track(new Manager<Container>(Container.class).getAssociatedInstance(containerId)).setMessageProcessor(new MessageProcessor<TestMessageProcessor>(new TestMessageProcessor())).setClusterId(cid);
    container.setDispatcher(dispatcher);
    container.setInbound(new DummyInbound());
    container.start(new TestInfrastructure(null, null) {

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

        @Override
        public NodeStatsCollector getNodeStatsCollector() {
            return nodeStats;
        }
    });
    forceOutputException = false;
    stillRunning = true;
}
Also used : BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) NodeStatsCollector(net.dempsy.monitoring.NodeStatsCollector) TestInfrastructure(net.dempsy.util.TestInfrastructure) ClusterId(net.dempsy.config.ClusterId) BasicClusterStatsCollector(net.dempsy.monitoring.basic.BasicClusterStatsCollector) DummyInbound(net.dempsy.container.mocks.DummyInbound) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) LockingContainer(net.dempsy.container.locking.LockingContainer) BasicClusterStatsCollector(net.dempsy.monitoring.basic.BasicClusterStatsCollector) ClusterStatsCollector(net.dempsy.monitoring.ClusterStatsCollector) BasicNodeStatsCollector(net.dempsy.monitoring.basic.BasicNodeStatsCollector) Before(org.junit.Before)

Example 9 with TestInfrastructure

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

the class TcpTransportTest method runMultiMessage.

private void runMultiMessage(final String testName, final int numThreads, final int numMessagePerThread, final String message, final Serializer serializer) throws Exception {
    try (final ServiceTracker tr = new ServiceTracker()) {
        final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).numHandlers(2).useLocalHost(true).maxMessageSize(1024 * 1024 * 1024);
        final ThreadingModel tm = tr.track(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + "." + testName));
        final Infrastructure infra = tr.track(new TestInfrastructure(tm));
        final TcpAddress addr = r.getAddress(infra);
        LOGGER.debug(addr.toString());
        final AtomicLong msgCount = new AtomicLong();
        r.start((Listener<RoutedMessage>) msg -> {
            msgCount.incrementAndGet();
            return true;
        }, infra);
        final AtomicBoolean letMeGo = new AtomicBoolean(false);
        final CountDownLatch waitToExit = new CountDownLatch(1);
        final List<Thread> threads = IntStream.range(0, numThreads).mapToObj(threadNum -> new Thread(() -> {
            try (final SenderFactory sf = senderFactory.get()) {
                sf.start(new TestInfrastructure(null, null) {

                    @Override
                    public Map<String, String> getConfiguration() {
                        final Map<String, String> ret = new HashMap<>();
                        ret.put(sf.getClass().getPackage().getName() + "." + NioSenderFactory.CONFIG_KEY_SENDER_THREADS, NUM_SENDER_THREADS);
                        return ret;
                    }
                });
                final Sender sender = sf.getSender(addr);
                while (!letMeGo.get()) Thread.yield();
                try {
                    for (int i = 0; i < numMessagePerThread; i++) sender.send(new RoutedMessage(new int[] { 0 }, "Hello", message));
                } catch (final InterruptedException ie) {
                    LOGGER.error("Interrupted in send.");
                }
                try {
                    waitToExit.await();
                } catch (final InterruptedException ie) {
                }
            }
        }, "testMultiMessage-Sender-" + threadNum)).map(th -> chain(th, t -> t.start())).collect(Collectors.toList());
        Thread.sleep(10);
        // here's we go.
        letMeGo.set(true);
        // the total number of messages sent should be this count.
        assertTrue(poll(Long.valueOf((long) numThreads * (long) numMessagePerThread), v -> v.longValue() == msgCount.get()));
        // let the threads exit
        waitToExit.countDown();
        // all threads should eventually exit.
        assertTrue(poll(threads, o -> o.stream().filter(t -> t.isAlive()).count() == 0));
    }
}
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) HashMap(java.util.HashMap) RoutedMessage(net.dempsy.transport.RoutedMessage) CountDownLatch(java.util.concurrent.CountDownLatch) 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) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestInfrastructure(net.dempsy.util.TestInfrastructure) Infrastructure(net.dempsy.Infrastructure) NioSenderFactory(net.dempsy.transport.tcp.nio.NioSenderFactory) SenderFactory(net.dempsy.transport.SenderFactory)

Example 10 with TestInfrastructure

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

the class TcpTransportTest method testReceiverStartOnSpecifiedIf.

@Test
public void testReceiverStartOnSpecifiedIf() throws Exception {
    final List<NetworkInterface> ifs = Collections.list(TcpUtils.getInterfaces(null)).stream().filter(nif -> !uncheck(() -> nif.isLoopback())).filter(nif -> nif.inetAddresses().filter(ia -> ia instanceof Inet4Address).anyMatch(ia -> uncheck(() -> ia.isReachable(100)))).collect(Collectors.toList());
    final NetworkInterface nif = (ifs.size() > 1) ? ifs.get(1) : ((ifs.size() == 1) ? ifs.get(0) : null);
    if (nif != null) {
        // otherwise we can do no testing.
        final String ifname = nif.getDisplayName();
        if (Collections.list(nif.getInetAddresses()).size() > 0) {
            // otherwise, we still can't really do anything without a lot of work
            final AtomicBoolean resolverCalled = new AtomicBoolean(false);
            try (ServiceTracker tr = new ServiceTracker()) {
                final AbstractTcpReceiver<?, ?> r = tr.track(receiver.get()).resolver(a -> {
                    resolverCalled.set(true);
                    return a;
                }).numHandlers(2).useLocalHost(false);
                final Infrastructure infra = tr.track(new TestInfrastructure(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testReceiverStart")) {

                    @Override
                    public Map<String, String> getConfiguration() {
                        final HashMap<String, String> config = new HashMap<>();
                        config.put(NioReceiver.class.getPackage().getName() + "." + NioReceiver.CONFIG_KEY_RECEIVER_NETWORK_IF_NAME, ifname);
                        return config;
                    }
                });
                final TcpAddress addr = r.getAddress(infra);
                assertTrue(Collections.list(nif.getInetAddresses()).contains(addr.inetAddress));
                LOGGER.debug(addr.toString());
                r.start(null, infra);
                assertTrue(resolverCalled.get());
            }
        }
    }
}
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) Inet4Address(java.net.Inet4Address) NioReceiver(net.dempsy.transport.tcp.nio.NioReceiver) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) HashMap(java.util.HashMap) NetworkInterface(java.net.NetworkInterface) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestInfrastructure(net.dempsy.util.TestInfrastructure) Infrastructure(net.dempsy.Infrastructure) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

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