Search in sources :

Example 11 with ClusterInfoSession

use of net.dempsy.cluster.ClusterInfoSession 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 12 with ClusterInfoSession

use of net.dempsy.cluster.ClusterInfoSession in project Dempsy by Dempsy.

the class TestLeaderAndSubscriber method testMultiLeader.

@Test
public void testMultiLeader() throws Exception {
    final int NUM_THREADS = 10;
    final ClusterId cid = setTestName("testMultiLeader");
    final AtomicBoolean isRunning = new AtomicBoolean(true);
    final AtomicBoolean go = new AtomicBoolean(false);
    final List<ListenerHolder> allHolders = new ArrayList<>();
    try {
        final List<ListenerHolder> holders = new ArrayList<>();
        for (int i = 0; i < NUM_THREADS; i++) {
            final ClusterInfoSession session = sessFact.createSession();
            final Utils<ContainerAddress> utils = new Utils<>(makeInfra(session, sched), cid.clusterName, new ContainerAddress(new DummyNodeAddress(), new int[] { 0 }));
            final Leader<ContainerAddress> l = new Leader<>(utils, 256, 1, infra, isRunning, ContainerAddress[]::new);
            final Thread t = new Thread(() -> {
                // wait for it.
                while (!go.get()) {
                    if (!isRunning.get())
                        return;
                    Thread.yield();
                }
                l.process();
            }, "testMultiLeader-" + i);
            holders.add(new ListenerHolder(session, l, t));
            t.start();
        }
        // all threads running.
        Thread.sleep(50);
        // we're going to modify holders
        allHolders.addAll(holders);
        // let 'em go.
        go.set(true);
        // once they're all ready, one should be 'it'
        assertTrue(poll(o -> holders.stream().map(h -> h.l).filter(l -> l.isReady()).count() == NUM_THREADS));
        assertEquals(1, holders.stream().map(h -> h.l).filter(l -> l.imIt()).count());
        // find the leader and kill it.
        final ListenerHolder h = holders.stream().filter(l -> l.l.imIt()).findFirst().get();
        disruptor.accept(h.session);
        holders.remove(h);
        assertTrue(poll(o -> holders.stream().filter(l -> l.l.imIt()).count() == 1L));
        Thread.sleep(50);
        assertEquals(1, holders.stream().filter(l -> l.l.imIt()).count());
        isRunning.set(false);
        assertTrue(poll(o -> holders.stream().filter(l -> l.t.isAlive()).count() == 0));
    } finally {
        allHolders.forEach(h -> h.session.close());
        isRunning.set(false);
    }
}
Also used : Logger(org.slf4j.Logger) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) ClusterInfoSessionFactory(net.dempsy.cluster.ClusterInfoSessionFactory) NodeAddress(net.dempsy.transport.NodeAddress) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) BaseRouterTestWithSession(net.dempsy.router.BaseRouterTestWithSession) Assert.assertEquals(org.junit.Assert.assertEquals) ClusterId(net.dempsy.config.ClusterId) ClusterId(net.dempsy.config.ClusterId) ArrayList(java.util.ArrayList) ContainerAddress(net.dempsy.router.RoutingStrategy.ContainerAddress) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterInfoSession(net.dempsy.cluster.ClusterInfoSession) Test(org.junit.Test)

Aggregations

ClusterInfoSession (net.dempsy.cluster.ClusterInfoSession)12 Test (org.junit.Test)10 ClusterId (net.dempsy.config.ClusterId)8 NodeAddress (net.dempsy.transport.NodeAddress)7 Set (java.util.Set)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ContainerAddress (net.dempsy.router.RoutingStrategy.ContainerAddress)6 ConditionPoll.poll (net.dempsy.utils.test.ConditionPoll.poll)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertTrue (org.junit.Assert.assertTrue)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ClusterInfoException (net.dempsy.cluster.ClusterInfoException)5 Functional.chain (net.dempsy.util.Functional.chain)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 Dispatcher (net.dempsy.messages.Dispatcher)4