Search in sources :

Example 6 with DefaultThreadingModel

use of net.dempsy.threading.DefaultThreadingModel in project Dempsy by Dempsy.

the class TestAlmostSimple method testSimple.

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

            private Dispatcher disp;

            boolean done = false;

            int cur = 0;

            AtomicBoolean exitedLoop = new AtomicBoolean(false);

            @Override
            public void stop() {
                done = true;
                while (!exitedLoop.get()) Thread.yield();
            }

            @Override
            public void start() {
                try {
                    while (!done) {
                        uncheck(() -> disp.dispatchAnnotated(new Message(cur++)));
                        count.incrementAndGet();
                    }
                } finally {
                    exitedLoop.set(true);
                }
            }

            @Override
            public void setDispatcher(final Dispatcher dispatcher) {
                this.disp = dispatcher;
            }
        }).cluster("mpEven").mp(new MessageProcessor<>(mpEven)).evictionFrequency(1, TimeUnit.SECONDS).cluster("mpOdd").mp(new MessageProcessor<>(mpOdd)).build();
        nm.node(n).collaborator(new LocalClusterSessionFactory().createSession()).threadingModel(tm.start(n.application));
        nm.start();
        assertTrue(ConditionPoll.poll(c -> count.get() > 100000));
        // make sure an eviction happened before closing
        // at this point all of the mps should have been checked for eviction
        assertTrue(ConditionPoll.poll(o -> 2 == mpEven.allMps.size()));
        mpEven.allMps.forEach(m -> uncheck(() -> assertTrue(ConditionPoll.poll(o -> m.evictCalled))));
        // now enable them to actually be removed from the container.
        // we need to stop the adaptor or we'll keep recreating them
        adaptor.stop();
        // need to wait for all of the adaptor messages to play through before evicting
        Thread.sleep(2000);
        MpEven.allowEviction.set(true);
        final List<Container> containers = nm.getContainers().stream().filter(c -> "mpEven".equals(c.getClusterId().clusterName)).collect(Collectors.toList());
        assertEquals(1, containers.size());
        final Container container = containers.get(0);
        assertTrue(ConditionPoll.poll(o -> 0 == container.getProcessorCount()));
    }
    Thread.sleep(2000);
    // make sure the messages were distributed correctly.
    assertEquals(2, mpEven.allMps.size());
    assertEquals(2, mpOdd.allMps.size());
    final MpEven mpEvenYes = mpEven.allMps.stream().filter(m -> "yes".equals(m.key)).findAny().get();
    // all messages here should be even numbers
    assertTrue(mpEvenYes.received.stream().allMatch(m -> (m.message & 0x01) == 0));
    final MpEven mpEvenNo = mpEven.allMps.stream().filter(m -> "no".equals(m.key)).findAny().get();
    // all messages here should be odd numbers
    assertTrue(mpEvenNo.received.stream().allMatch(m -> (m.message & 0x01) == 1));
    final MpOdd mpOddYes = mpOdd.allMps.stream().filter(m -> "yes".equals(m.key)).findAny().get();
    // all messages here should be odd numbers
    assertTrue(mpOddYes.received.stream().allMatch(m -> (m.message & 0x01) == 1));
    final MpOdd mpOddNo = mpOdd.allMps.stream().filter(m -> "no".equals(m.key)).findAny().get();
    // all messages here should be even numbers
    assertTrue(mpOddNo.received.stream().allMatch(m -> (m.message & 0x01) == 0));
}
Also used : Adaptor(net.dempsy.messages.Adaptor) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) Arrays(java.util.Arrays) Mp(net.dempsy.lifecycle.annotation.Mp) Node(net.dempsy.config.Node) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) MessageType(net.dempsy.lifecycle.annotation.MessageType) Dispatcher(net.dempsy.messages.Dispatcher) NodeAddress(net.dempsy.transport.NodeAddress) Listener(net.dempsy.transport.Listener) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConditionPoll(net.dempsy.utils.test.ConditionPoll) ArrayList(java.util.ArrayList) NonLockingAltBulkContainer(net.dempsy.container.altnonlockingbulk.NonLockingAltBulkContainer) Receiver(net.dempsy.transport.Receiver) Parameterized(org.junit.runners.Parameterized) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) MessageKey(net.dempsy.lifecycle.annotation.MessageKey) MessageHandler(net.dempsy.lifecycle.annotation.MessageHandler) Collection(java.util.Collection) Functional.uncheck(net.dempsy.util.Functional.uncheck) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Activation(net.dempsy.lifecycle.annotation.Activation) Collectors(java.util.stream.Collectors) LockingContainer(net.dempsy.container.locking.LockingContainer) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Adaptor(net.dempsy.messages.Adaptor) Functional.chain(net.dempsy.util.Functional.chain) Evictable(net.dempsy.lifecycle.annotation.Evictable) Container(net.dempsy.container.Container) MessageTransportException(net.dempsy.transport.MessageTransportException) Assert.assertEquals(org.junit.Assert.assertEquals) Node(net.dempsy.config.Node) MessageProcessor(net.dempsy.lifecycle.annotation.MessageProcessor) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) Dispatcher(net.dempsy.messages.Dispatcher) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) NonLockingAltContainer(net.dempsy.container.altnonlocking.NonLockingAltContainer) NonLockingAltBulkContainer(net.dempsy.container.altnonlockingbulk.NonLockingAltBulkContainer) LockingContainer(net.dempsy.container.locking.LockingContainer) Container(net.dempsy.container.Container) Test(org.junit.Test)

Example 7 with DefaultThreadingModel

use of net.dempsy.threading.DefaultThreadingModel 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 8 with DefaultThreadingModel

use of net.dempsy.threading.DefaultThreadingModel 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 9 with DefaultThreadingModel

use of net.dempsy.threading.DefaultThreadingModel 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)

Example 10 with DefaultThreadingModel

use of net.dempsy.threading.DefaultThreadingModel in project Dempsy by Dempsy.

the class TcpTransportTest method testReceiverStart.

@Test
public void testReceiverStart() throws Exception {
    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(true);
        final Infrastructure infra = tr.track(new TestInfrastructure(new DefaultThreadingModel(TcpTransportTest.class.getSimpleName() + ".testReceiverStart")));
        final TcpAddress addr = r.getAddress(infra);
        LOGGER.debug(addr.toString());
        r.start(null, infra);
        assertTrue(resolverCalled.get());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestInfrastructure(net.dempsy.util.TestInfrastructure) ServiceTracker(net.dempsy.ServiceTracker) TestInfrastructure(net.dempsy.util.TestInfrastructure) Infrastructure(net.dempsy.Infrastructure) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) Test(org.junit.Test)

Aggregations

DefaultThreadingModel (net.dempsy.threading.DefaultThreadingModel)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Receiver (net.dempsy.transport.Receiver)10 TestInfrastructure (net.dempsy.util.TestInfrastructure)10 Test (org.junit.Test)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 Assert.assertTrue (org.junit.Assert.assertTrue)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Functional.chain (net.dempsy.util.Functional.chain)8 Collection (java.util.Collection)7 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 Listener (net.dempsy.transport.Listener)7 Sender (net.dempsy.transport.Sender)7 SenderFactory (net.dempsy.transport.SenderFactory)7 ConditionPoll.poll (net.dempsy.utils.test.ConditionPoll.poll)7 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6