Search in sources :

Example 16 with DummyTopologyView

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

the class TestMinEventDelayHandler method testReactivate.

@Test
public void testReactivate() throws Exception {
    logger.info("testReactivate: start");
    // install a minEventDelayHandler with a longer delay of 2sec
    mgr.installMinEventDelayHandler(sds, scheduler, 2);
    final DummyListener listener = new DummyListener();
    logger.info("testReactivate: calling handleActivated");
    mgr.bind(listener);
    mgr.handleActivated();
    TestHelper.assertNoEvents(listener);
    final DummyTopologyView view1 = new DummyTopologyView().addInstance();
    final DummyTopologyView view2 = DummyTopologyView.clone(view1).addInstance(UUID.randomUUID().toString(), (DefaultClusterView) view1.getLocalInstance().getClusterView(), false, false);
    final DummyTopologyView view3 = DummyTopologyView.clone(view1).addInstance(UUID.randomUUID().toString(), (DefaultClusterView) view1.getLocalInstance().getClusterView(), false, false);
    logger.info("testReactivate: calling handleNewView...");
    mgr.handleNewView(view1);
    logger.info("testReactivate: asserting init event");
    TestHelper.assertEvents(mgr, listener, EventHelper.newInitEvent(view1));
    logger.info("testReactivate: calling handleChanging...");
    mgr.handleChanging();
    TestHelper.assertEvents(mgr, listener, EventHelper.newChangingEvent(view1));
    logger.info("testReactivate: calling handleNewView 2nd time...");
    mgr.handleNewView(view2);
    TestHelper.assertNoEvents(listener);
    // make sure the MinEventDelayHandler finds a topology when coming back from the delaying, so:
    sds.setTopoology(view2);
    logger.info("testReactivate: waiting for async events to have been processed - 4sec");
    Thread.sleep(4000);
    logger.info("testReactivate: waiting for async events to have been processed - max another 2sec");
    assertEquals(0, mgr.waitForAsyncEvents(2000));
    logger.info("testReactivate: asserting CHANGED event");
    TestHelper.assertEvents(mgr, listener, EventHelper.newChangedEvent(view1, view2));
    // now do the above again, but this time do a handleDeactivated before receiving another changed event
    logger.info("testReactivate: calling handleChanging...");
    mgr.handleChanging();
    TestHelper.assertEvents(mgr, listener, EventHelper.newChangingEvent(view2));
    logger.info("testReactivate: calling handleNewView 2nd time...");
    mgr.handleNewView(view3);
    TestHelper.assertNoEvents(listener);
    // make sure the MinEventDelayHandler finds a topology when coming back from the delaying, so:
    sds.setTopoology(view3);
    logger.info("testReactivate: doing handleDeactivated");
    final AsyncEventSender asyncEventSender = mgr.getAsyncEventSender();
    Field field = mgr.getClass().getDeclaredField("minEventDelayHandler");
    field.setAccessible(true);
    MinEventDelayHandler minEventDelayHandler = (MinEventDelayHandler) field.get(mgr);
    assertNotNull(minEventDelayHandler);
    // marking view3 as not current
    view3.setNotCurrent();
    sds.setTopoology(view3);
    mgr.handleDeactivated();
    TestHelper.assertNoEvents(listener);
    logger.info("testReactivate: now waiting 5 sec to make sure the MinEventDelayHandler would be finished");
    TestHelper.assertNoEvents(listener);
    Thread.sleep(5000);
    logger.info("testReactivate: after those 5 sec there should however still not be any new event");
    TestHelper.assertNoEvents(listener);
    int cnt = asyncEventSender.getInFlightEventCnt();
    if (minEventDelayHandler != null && minEventDelayHandler.isDelaying()) {
        cnt++;
    }
    assertEquals(0, cnt);
}
Also used : Field(java.lang.reflect.Field) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) Test(org.junit.Test)

Example 17 with DummyTopologyView

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

the class TestMinEventDelayHandler method testLongMinDelay.

@Test
public void testLongMinDelay() throws Exception {
    mgr.installMinEventDelayHandler(sds, scheduler, 5);
    final DummyListener listener = new DummyListener();
    // first activate
    logger.info("testLongMinDelay: calling handleActivated...");
    mgr.handleActivated();
    // paranoia
    assertNoEvents(listener);
    // then bind
    logger.info("testLongMinDelay: calling bind...");
    mgr.bind(listener);
    // there was no changing or changed yet
    assertNoEvents(listener);
    logger.info("testLongMinDelay: calling handleChanging...");
    mgr.handleChanging();
    assertNoEvents(listener);
    final DummyTopologyView view = new DummyTopologyView().addInstance();
    DummyTopologyView clonedView = view.clone();
    logger.info("testLongMinDelay: calling handleNewView...");
    mgr.handleNewView(view);
    TestHelper.assertEvents(mgr, listener, EventHelper.newInitEvent(view));
    final DummyTopologyView view2 = new DummyTopologyView().addInstance();
    view2.addInstance(UUID.randomUUID().toString(), (DefaultClusterView) view2.getLocalInstance().getClusterView(), false, false);
    logger.info("testLongMinDelay: calling handleNewView...");
    clonedView.setNotCurrent();
    mgr.handleNewView(view2);
    TestHelper.assertEvents(mgr, listener, EventHelper.newChangingEvent(clonedView));
    assertFalse(view.isCurrent());
}
Also used : DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) Test(org.junit.Test)

Example 18 with DummyTopologyView

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

the class TestViewStateManager method testCancelSync.

@Test
public void testCancelSync() throws Exception {
    final List<Runnable> syncCallbacks = new LinkedList<Runnable>();
    mgr = new ViewStateManagerImpl(new ReentrantLock(), new ClusterSyncService() {

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

        @Override
        public void cancelSync() {
            synchronized (syncCallbacks) {
                syncCallbacks.clear();
            }
        }
    });
    mgr.handleActivated();
    final DummyListener listener = new DummyListener();
    mgr.bind(listener);
    mgr.handleChanging();
    final BaseTopologyView view = new DummyTopologyView().addInstance();
    mgr.handleNewView(view);
    assertEquals(0, mgr.waitForAsyncEvents(1000));
    TestHelper.assertNoEvents(listener);
    synchronized (syncCallbacks) {
        assertEquals(1, syncCallbacks.size());
    }
    String id1 = UUID.randomUUID().toString();
    String id2 = UUID.randomUUID().toString();
    final BaseTopologyView view2 = TestHelper.newView(true, id1, id1, id1, id2);
    mgr.handleNewView(view2);
    assertEquals(0, mgr.waitForAsyncEvents(1000));
    TestHelper.assertNoEvents(listener);
    synchronized (syncCallbacks) {
        assertEquals(1, syncCallbacks.size());
        syncCallbacks.get(0).run();
        syncCallbacks.clear();
    }
    assertEquals(0, mgr.waitForAsyncEvents(1000));
    assertEvents(listener, EventHelper.newInitEvent(view2));
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) LinkedList(java.util.LinkedList) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Test(org.junit.Test)

Example 19 with DummyTopologyView

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

the class TestViewStateManager method testBindChangingChangedActivate.

@Test
public void testBindChangingChangedActivate() throws Exception {
    final DummyListener listener = new DummyListener();
    mgr.bind(listener);
    TestHelper.assertNoEvents(listener);
    mgr.handleChanging();
    TestHelper.assertNoEvents(listener);
    final BaseTopologyView view = new DummyTopologyView().addInstance();
    mgr.handleNewView(view);
    TestHelper.assertNoEvents(listener);
    mgr.handleActivated();
    assertEvents(listener, EventHelper.newInitEvent(view));
    randomEventLoop(defaultRandom, listener);
}
Also used : DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Test(org.junit.Test)

Example 20 with DummyTopologyView

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

the class TestViewStateManager method testBindChangingChangedChangingActivate.

@Test
public void testBindChangingChangedChangingActivate() throws Exception {
    final DummyListener listener = new DummyListener();
    mgr.bind(listener);
    TestHelper.assertNoEvents(listener);
    mgr.handleChanging();
    TestHelper.assertNoEvents(listener);
    final BaseTopologyView view = new DummyTopologyView().addInstance();
    mgr.handleNewView(view);
    TestHelper.assertNoEvents(listener);
    mgr.handleChanging();
    TestHelper.assertNoEvents(listener);
    mgr.handleActivated();
    TestHelper.assertNoEvents(listener);
    final BaseTopologyView view2 = new DummyTopologyView().addInstance();
    mgr.handleNewView(view2);
    assertEvents(listener, EventHelper.newInitEvent(view2));
    randomEventLoop(defaultRandom, listener);
}
Also used : DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Test(org.junit.Test)

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