Search in sources :

Example 11 with DummyTopologyView

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

the class TestOakSyncTokenService method testOneNode.

@Test
public void testOneNode() throws Exception {
    logger.info("testOneNode: start");
    DummyTopologyView one = TestHelper.newView(true, slingId1, slingId1, slingId1);
    Lock lock = new ReentrantLock();
    OakBacklogClusterSyncService cs = OakBacklogClusterSyncService.testConstructorAndActivate(new SimpleCommonsConfig(), idMapService1, new DummySlingSettingsService(slingId1), factory1);
    ViewStateManager vsm = ViewStateManagerFactory.newViewStateManager(lock, cs);
    DummyListener l = new DummyListener();
    assertEquals(0, l.countEvents());
    vsm.bind(l);
    cs.triggerBackgroundCheck();
    assertEquals(0, l.countEvents());
    vsm.handleActivated();
    cs.triggerBackgroundCheck();
    assertEquals(0, l.countEvents());
    vsm.handleNewView(one);
    cs.triggerBackgroundCheck();
    assertEquals(0, l.countEvents());
    cs.triggerBackgroundCheck();
    DescriptorHelper.setDiscoveryLiteDescriptor(factory1, new DiscoveryLiteDescriptorBuilder().me(1).seq(1).activeIds(1).setFinal(true));
    assertTrue(idMapService1.waitForInit(5000));
    cs.triggerBackgroundCheck();
    assertEquals(0, vsm.waitForAsyncEvents(1000));
    assertEquals(1, l.countEvents());
    logger.info("testOneNode: end");
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) ViewStateManager(org.apache.sling.discovery.commons.providers.ViewStateManager) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) DummyListener(org.apache.sling.discovery.commons.providers.base.DummyListener) Test(org.junit.Test)

Example 12 with DummyTopologyView

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

the class TestViewStateManager method testPropertiesChanged.

@Test
public void testPropertiesChanged() throws Exception {
    final DummyListener listener = new DummyListener();
    mgr.handleActivated();
    mgr.bind(listener);
    mgr.handleChanging();
    DummyTopologyView oldView = new DummyTopologyView().addInstance();
    DefaultInstanceDescription localInstance = (DefaultInstanceDescription) oldView.getLocalInstance();
    localInstance.setProperty("foo", "bar1");
    mgr.handleNewView(oldView);
    TopologyEvent initEvent = EventHelper.newInitEvent(oldView.clone());
    assertEvents(listener, initEvent);
    DummyTopologyView newView = oldView.clone();
    oldView.setNotCurrent();
    localInstance = (DefaultInstanceDescription) newView.getLocalInstance();
    localInstance.setProperty("foo", "bar2");
    mgr.handleNewView(newView);
    Thread.sleep(2000);
    TopologyEvent propertiesChangedEvent = EventHelper.newPropertiesChangedEvent(oldView.clone(), newView.clone());
    assertEvents(listener, propertiesChangedEvent);
}
Also used : DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) TopologyEvent(org.apache.sling.discovery.TopologyEvent) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) Test(org.junit.Test)

Example 13 with DummyTopologyView

use of org.apache.sling.discovery.commons.providers.DummyTopologyView 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 14 with DummyTopologyView

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

the class TestViewStateManager method testClusterSyncService_noConcurrency.

@Test
public void testClusterSyncService_noConcurrency() throws Exception {
    final org.apache.log4j.Logger commonsLogger = RootLogger.getLogger("org.apache.sling.discovery.commons.providers");
    //        final org.apache.log4j.Level logLevel = commonsLogger.getLevel();
    // change here to DEBUG in case of issues with this test
    commonsLogger.setLevel(Level.INFO);
    final Semaphore serviceSemaphore = new Semaphore(0);
    final ReentrantLock lock = new ReentrantLock();
    final ClusterSyncServiceWithSemaphore cs = new ClusterSyncServiceWithSemaphore(lock, serviceSemaphore);
    mgr = new ViewStateManagerImpl(lock, cs);
    final DummyListener listener = new DummyListener();
    mgr.bind(listener);
    TestHelper.assertNoEvents(listener);
    mgr.handleActivated();
    TestHelper.assertNoEvents(listener);
    final String slingId1 = UUID.randomUUID().toString();
    final String slingId2 = UUID.randomUUID().toString();
    final String clusterId = UUID.randomUUID().toString();
    final DefaultClusterView cluster = new DefaultClusterView(clusterId);
    final DummyTopologyView view1 = new DummyTopologyView().addInstance(slingId1, cluster, true, true).addInstance(slingId2, cluster, false, false);
    async(new Runnable() {

        @Override
        public void run() {
            mgr.handleNewView(view1);
        }
    });
    Thread.sleep(1000);
    TestHelper.assertNoEvents(listener);
    serviceSemaphore.release(1);
    Thread.sleep(1000);
    assertEvents(listener, EventHelper.newInitEvent(view1));
    final DummyTopologyView view2 = view1.clone();
    mgr.handleChanging();
    assertEvents(listener, EventHelper.newChangingEvent(view1));
    view2.removeInstance(slingId2);
    async(new Runnable() {

        @Override
        public void run() {
            mgr.handleNewView(view2);
        }
    });
    logger.debug("run: waiting for 1sec");
    Thread.sleep(1000);
    logger.debug("run: asserting no events");
    TestHelper.assertNoEvents(listener);
    logger.debug("run: releasing consistencyService");
    serviceSemaphore.release(1);
    logger.debug("run: waiting 1sec");
    Thread.sleep(1000);
    logger.debug("run: asserting 1 event");
    assertEvents(listener, EventHelper.newChangedEvent(view1, view2));
    // back to default
    commonsLogger.setLevel(Level.INFO);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) Semaphore(java.util.concurrent.Semaphore) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Test(org.junit.Test)

Example 15 with DummyTopologyView

use of org.apache.sling.discovery.commons.providers.DummyTopologyView 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

DummyTopologyView (org.apache.sling.discovery.commons.providers.DummyTopologyView)31 Test (org.junit.Test)29 BaseTopologyView (org.apache.sling.discovery.commons.providers.BaseTopologyView)19 ReentrantLock (java.util.concurrent.locks.ReentrantLock)5 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)5 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)4 Category (org.junit.experimental.categories.Category)3 Semaphore (java.util.concurrent.Semaphore)2 Lock (java.util.concurrent.locks.Lock)2 TopologyEvent (org.apache.sling.discovery.TopologyEvent)2 ViewStateManager (org.apache.sling.discovery.commons.providers.ViewStateManager)2 DummyListener (org.apache.sling.discovery.commons.providers.base.DummyListener)2 Field (java.lang.reflect.Field)1 LinkedList (java.util.LinkedList)1 Level (org.apache.log4j.Level)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 ClusterSyncService (org.apache.sling.discovery.commons.providers.spi.ClusterSyncService)1