Search in sources :

Example 6 with BaseTopologyView

use of org.apache.sling.discovery.commons.providers.BaseTopologyView in project sling by apache.

the class ClusterTest method testTwoNodes.

@Test
public void testTwoNodes() throws Exception {
    final ViewStateManagerImpl mgr1 = newMgr();
    final String slingId1 = UUID.randomUUID().toString();
    final ViewStateManagerImpl mgr2 = newMgr();
    final String slingId2 = UUID.randomUUID().toString();
    // bind l1
    DummyListener l1 = new DummyListener();
    mgr1.bind(l1);
    assertCountEvents(mgr1, l1);
    // bind l2
    DummyListener l2 = new DummyListener();
    mgr2.bind(l2);
    assertCountEvents(mgr2, l2);
    // fiddle with l1 - without any events expected to be sent
    mgr1.handleChanging();
    assertCountEvents(mgr1, l1);
    mgr1.handleActivated();
    assertCountEvents(mgr1, l1);
    mgr1.handleChanging();
    assertCountEvents(mgr1, l1);
    // fiddle with l2 - without any events expected to be sent
    mgr2.handleChanging();
    assertCountEvents(mgr2, l2);
    mgr2.handleActivated();
    assertCountEvents(mgr2, l2);
    mgr2.handleChanging();
    assertCountEvents(mgr2, l2);
    // call handleNewView with not-current views first...
    BaseTopologyView vA1 = TestHelper.newView(false, slingId1, slingId1, slingId1, slingId2);
    mgr1.handleNewView(vA1);
    assertCountEvents(mgr1, l1);
    assertCountEvents(mgr2, l2);
    BaseTopologyView vB1 = TestHelper.newView(false, slingId1, slingId2, slingId1, slingId2);
    mgr2.handleNewView(vB1);
    assertCountEvents(mgr1, l1);
    assertCountEvents(mgr2, l2);
    // then call handleNewView with a current view - that should now sent the INIT
    BaseTopologyView vA2 = TestHelper.newView(true, slingId1, slingId1, slingId1, slingId2);
    mgr1.handleNewView(vA2);
    assertCountEvents(mgr1, l1, Type.TOPOLOGY_INIT);
    assertCountEvents(mgr2, l2);
    BaseTopologyView vB2 = TestHelper.newView(true, slingId1, slingId2, slingId1, slingId2);
    mgr2.handleNewView(vB2);
    assertCountEvents(mgr1, l1, Type.TOPOLOGY_INIT);
    assertCountEvents(mgr2, l2, Type.TOPOLOGY_INIT);
    // now let instance1 get decoupled from the cluster (pseudo-network-partitioning)
    BaseTopologyView vB3 = TestHelper.newView(true, slingId2, slingId2, slingId2);
    mgr2.handleNewView(vB3);
    assertCountEvents(mgr1, l1, Type.TOPOLOGY_INIT);
    assertCountEvents(mgr2, l2, Type.TOPOLOGY_INIT, Type.TOPOLOGY_CHANGING, Type.TOPOLOGY_CHANGED);
    // now let instance1 take note of this decoupling
    mgr1.handleChanging();
    assertCountEvents(mgr1, l1, Type.TOPOLOGY_INIT, Type.TOPOLOGY_CHANGING);
    assertCountEvents(mgr2, l2, Type.TOPOLOGY_INIT, Type.TOPOLOGY_CHANGING, Type.TOPOLOGY_CHANGED);
    // and now let instance1 rejoin
    BaseTopologyView vA4 = TestHelper.newView(true, slingId2, slingId1, slingId1, slingId2);
    mgr1.handleNewView(vA4);
    assertCountEvents(mgr1, l1, Type.TOPOLOGY_INIT, Type.TOPOLOGY_CHANGING, Type.TOPOLOGY_CHANGED);
    assertCountEvents(mgr2, l2, Type.TOPOLOGY_INIT, Type.TOPOLOGY_CHANGING, Type.TOPOLOGY_CHANGED);
    BaseTopologyView vB4 = TestHelper.newView(true, slingId2, slingId2, slingId1, slingId2);
    mgr2.handleNewView(vA4);
    assertCountEvents(mgr1, l1, Type.TOPOLOGY_INIT, Type.TOPOLOGY_CHANGING, Type.TOPOLOGY_CHANGED);
    assertCountEvents(mgr2, l2, Type.TOPOLOGY_INIT, Type.TOPOLOGY_CHANGING, Type.TOPOLOGY_CHANGED, Type.TOPOLOGY_CHANGING, Type.TOPOLOGY_CHANGED);
}
Also used : ViewStateManagerImpl(org.apache.sling.discovery.commons.providers.base.ViewStateManagerImpl) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Test(org.junit.Test)

Example 7 with BaseTopologyView

use of org.apache.sling.discovery.commons.providers.BaseTopologyView in project sling by apache.

the class TestMinEventDelayHandler method setup.

@Before
public void setup() throws Exception {
    mgr = new ViewStateManagerImpl(new ReentrantLock(), new ClusterSyncService() {

        @Override
        public void sync(BaseTopologyView view, Runnable callback) {
            callback.run();
        }

        @Override
        public void cancelSync() {
        // nothing to cancel, we're auto-run
        }
    });
    // I want randomness yes, but deterministic, for some methods at least
    defaultRandom = new Random(1234123412);
    scheduler = new DummyScheduler();
    sds = new DummyDiscoveryService();
    mgr.installMinEventDelayHandler(sds, scheduler, 1);
    final org.apache.log4j.Logger discoveryLogger = RootLogger.getLogger("org.apache.sling.discovery");
    logLevel = discoveryLogger.getLevel();
    // changed from Level.DEBUG
    discoveryLogger.setLevel(Level.INFO);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) Random(java.util.Random) ViewStateManagerImpl(org.apache.sling.discovery.commons.providers.base.ViewStateManagerImpl) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Before(org.junit.Before)

Example 8 with BaseTopologyView

use of org.apache.sling.discovery.commons.providers.BaseTopologyView in project sling by apache.

the class TestViewStateManager method setup.

@Before
public void setup() throws Exception {
    mgr = new ViewStateManagerImpl(new ReentrantLock(), new ClusterSyncService() {

        @Override
        public void sync(BaseTopologyView view, Runnable callback) {
            callback.run();
        }

        @Override
        public void cancelSync() {
        // nothing to cancel, we're auto-run
        }
    });
    // I want randomness yes, but deterministic, for some methods at least
    defaultRandom = new Random(1234123412);
    final org.apache.log4j.Logger discoveryLogger = RootLogger.getLogger("org.apache.sling.discovery");
    logLevel = discoveryLogger.getLevel();
    discoveryLogger.setLevel(Level.INFO);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) Random(java.util.Random) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Before(org.junit.Before)

Example 9 with BaseTopologyView

use of org.apache.sling.discovery.commons.providers.BaseTopologyView in project sling by apache.

the class TestViewStateManager method testBindActivateChangedDeactivateChangedActivateChanged.

@Test
public void testBindActivateChangedDeactivateChangedActivateChanged() throws Exception {
    final DummyListener listener = new DummyListener();
    mgr.bind(listener);
    TestHelper.assertNoEvents(listener);
    mgr.handleActivated();
    TestHelper.assertNoEvents(listener);
    final BaseTopologyView view1 = new DummyTopologyView().addInstance();
    mgr.handleNewView(view1);
    assertEvents(listener, EventHelper.newInitEvent(view1));
    mgr.handleDeactivated();
    TestHelper.assertNoEvents(listener);
    final BaseTopologyView view2 = new DummyTopologyView().addInstance();
    mgr.handleNewView(view2);
    TestHelper.assertNoEvents(listener);
    // need to bind again after deactivate
    mgr.bind(listener);
    mgr.handleActivated();
    assertEvents(listener, EventHelper.newInitEvent(view2));
    final BaseTopologyView view3 = new DummyTopologyView().addInstance();
    mgr.handleNewView(view3);
    assertEvents(listener, EventHelper.newChangingEvent(view2), EventHelper.newChangedEvent(view2, view3));
}
Also used : DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Test(org.junit.Test)

Example 10 with BaseTopologyView

use of org.apache.sling.discovery.commons.providers.BaseTopologyView in project sling by apache.

the class TestHelper method randomEventLoop.

/** does couple loops randomly calling handleChanging() (or not) and then handleNewView().
     * Note: random is passed to allow customizing and not hardcoding this method to a particular random
     * @throws InterruptedException **/
public static void randomEventLoop(ViewStateManagerImpl mgr, DummyDiscoveryService sds, int loopSize, int delayInMillis, final Random random, DummyListener... listeners) throws InterruptedException {
    for (int i = 0; i < loopSize; i++) {
        final boolean shouldCallChanging = random.nextBoolean();
        if (shouldCallChanging) {
            // dont always do a changing
            logger.debug("randomEventLoop: calling handleChanging...");
            mgr.handleChanging();
            // must first wait for async events to have been processed - as otherwise
            // the 'getLastView()' might not return the correct view
            logger.debug("randomEventLoop: waiting for async events....");
            waitForAsyncEvents(mgr);
            logger.debug("randomEventLoop: asserting CHANGING event was sent...");
            for (int j = 0; j < listeners.length; j++) {
                assertEvents(mgr, listeners[j], EventHelper.newChangingEvent(listeners[j].getLastView()));
            }
        } else {
            logger.debug("randomEventLoop: asserting no events...");
            for (int j = 0; j < listeners.length; j++) {
                assertNoEvents(listeners[j]);
            }
        }
        final DummyTopologyView view = new DummyTopologyView().addInstance();
        BaseTopologyView[] lastViews = new BaseTopologyView[listeners.length];
        for (int j = 0; j < listeners.length; j++) {
            lastViews[j] = listeners[j].getLastView();
        }
        logger.debug("randomEventLoop: calling handleNewView");
        if (sds != null) {
            sds.setTopoology(view);
        }
        DummyTopologyView clonedView = view.clone();
        mgr.handleNewView(view);
        if (delayInMillis > 0) {
            logger.debug("randomEventLoop: waiting " + delayInMillis + "ms ...");
            Thread.sleep(delayInMillis);
            logger.debug("randomEventLoop: waiting " + delayInMillis + "ms done.");
        }
        assertEquals(0, mgr.waitForAsyncEvents(500));
        if (!shouldCallChanging) {
            // in that case I should still get a CHANGING - by contract
            logger.debug("randomEventLoop: asserting CHANGING, CHANGED events were sent");
            for (int j = 0; j < listeners.length; j++) {
                assertEvents(mgr, listeners[j], EventHelper.newChangingEvent(lastViews[j]), EventHelper.newChangedEvent(lastViews[j], view));
            }
        } else {
            logger.debug("randomEventLoop: asserting CHANGED event was sent");
            for (int j = 0; j < listeners.length; j++) {
                assertEvents(mgr, listeners[j], EventHelper.newChangedEvent(lastViews[j], clonedView));
            }
        }
    }
}
Also used : DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView)

Aggregations

BaseTopologyView (org.apache.sling.discovery.commons.providers.BaseTopologyView)23 DummyTopologyView (org.apache.sling.discovery.commons.providers.DummyTopologyView)19 Test (org.junit.Test)19 ReentrantLock (java.util.concurrent.locks.ReentrantLock)4 ClusterSyncService (org.apache.sling.discovery.commons.providers.spi.ClusterSyncService)4 ViewStateManagerImpl (org.apache.sling.discovery.commons.providers.base.ViewStateManagerImpl)3 Random (java.util.Random)2 Before (org.junit.Before)2 Category (org.junit.experimental.categories.Category)2 LinkedList (java.util.LinkedList)1 ClusterView (org.apache.sling.discovery.ClusterView)1 DiscoveryService (org.apache.sling.discovery.DiscoveryService)1 InstanceDescription (org.apache.sling.discovery.InstanceDescription)1 TopologyView (org.apache.sling.discovery.TopologyView)1 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)1 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)1