Search in sources :

Example 11 with UndefinedClusterViewException

use of org.apache.sling.discovery.base.commons.UndefinedClusterViewException in project sling by apache.

the class AbstractClusterTest method testAdditionalInstance.

//TODO: this takes env 15sec
@Category(Slow.class)
@Test
public void testAdditionalInstance() throws Throwable {
    logger.info("testAdditionalInstance: start");
    assertNotNull(instance1);
    assertNotNull(instance2);
    assertEquals(instance1.getSlingId(), instance1.getClusterViewService().getSlingId());
    assertEquals(instance2.getSlingId(), instance2.getClusterViewService().getSlingId());
    try {
        instance1.getClusterViewService().getLocalClusterView();
        fail("should complain");
    } catch (UndefinedClusterViewException e) {
    // ok
    }
    try {
        instance2.getClusterViewService().getLocalClusterView();
        fail("should complain");
    } catch (UndefinedClusterViewException e) {
    // ok
    }
    instance1.heartbeatsAndCheckView();
    instance2.heartbeatsAndCheckView();
    instance1.dumpRepo();
    logger.info("testAdditionalInstance: 1st 2s sleep");
    Thread.sleep(2000);
    instance1.heartbeatsAndCheckView();
    instance2.heartbeatsAndCheckView();
    logger.info("testAdditionalInstance: 2nd 2s sleep");
    Thread.sleep(2000);
    instance1.dumpRepo();
    String clusterId1 = instance1.getClusterViewService().getLocalClusterView().getId();
    logger.info("clusterId1=" + clusterId1);
    String clusterId2 = instance2.getClusterViewService().getLocalClusterView().getId();
    logger.info("clusterId2=" + clusterId2);
    assertEquals(clusterId1, clusterId2);
    assertEquals(2, instance1.getClusterViewService().getLocalClusterView().getInstances().size());
    assertEquals(2, instance2.getClusterViewService().getLocalClusterView().getInstances().size());
    AssertingTopologyEventListener assertingTopologyEventListener = new AssertingTopologyEventListener();
    assertingTopologyEventListener.addExpected(Type.TOPOLOGY_INIT);
    assertEquals(1, assertingTopologyEventListener.getRemainingExpectedCount());
    instance1.bindTopologyEventListener(assertingTopologyEventListener);
    // SLING-4755: async event sending requires some minimal wait time nowadays
    Thread.sleep(500);
    assertEquals(0, assertingTopologyEventListener.getRemainingExpectedCount());
    // startup instance 3
    AcceptsMultiple acceptsMultiple = new AcceptsMultiple(Type.TOPOLOGY_CHANGING, Type.TOPOLOGY_CHANGED);
    assertingTopologyEventListener.addExpected(acceptsMultiple);
    assertingTopologyEventListener.addExpected(acceptsMultiple);
    instance3 = newBuilder().setDebugName("thirdInstance").useRepositoryOf(instance1).build();
    for (int i = 0; i < 4; i++) {
        instance1.heartbeatsAndCheckView();
        instance2.heartbeatsAndCheckView();
        instance3.heartbeatsAndCheckView();
        logger.info("testAdditionalInstance: i=" + i + ", 2s sleep");
        Thread.sleep(2000);
    }
    assertEquals(1, acceptsMultiple.getEventCnt(Type.TOPOLOGY_CHANGING));
    assertEquals(1, acceptsMultiple.getEventCnt(Type.TOPOLOGY_CHANGED));
    logger.info("testAdditionalInstance: end");
}
Also used : AssertingTopologyEventListener(org.apache.sling.discovery.base.its.setup.mock.AssertingTopologyEventListener) AcceptsMultiple(org.apache.sling.discovery.base.its.setup.mock.AcceptsMultiple) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 12 with UndefinedClusterViewException

use of org.apache.sling.discovery.base.commons.UndefinedClusterViewException in project sling by apache.

the class AbstractSingleInstanceTest method testGetters.

@Test
public void testGetters() throws UndefinedClusterViewException, InterruptedException {
    logger.info("testGetters: start");
    assertNotNull(instance);
    logger.info("sling id=" + instance.getSlingId());
    try {
        instance.getClusterViewService().getLocalClusterView();
        // SLING-5030
        fail("should complain");
    } catch (UndefinedClusterViewException e) {
    // ok
    }
    instance.heartbeatsAndCheckView();
    // wait 4000ms for the vote to happen
    Thread.sleep(4000);
    assertNotNull(instance.getClusterViewService().getLocalClusterView());
    ClusterView cv = instance.getClusterViewService().getLocalClusterView();
    logger.info("cluster view: id=" + cv.getId());
    assertNotNull(cv.getId());
    assertNotSame(cv.getId(), "");
    List<InstanceDescription> instances = cv.getInstances();
    assertNotNull(instances);
    assertTrue(instances.size() == 1);
    InstanceDescription myInstance = instances.get(0);
    assertNotNull(myInstance);
    assertTrue(myInstance.getClusterView() == cv);
    logger.info("instance id: " + myInstance.getSlingId());
    assertEquals(instance.getSlingId(), myInstance.getSlingId());
    Map<String, String> properties = myInstance.getProperties();
    assertNotNull(properties);
    assertNull(myInstance.getProperty("foo"));
    assertTrue(myInstance.isLeader());
    assertTrue(myInstance.isLocal());
    logger.info("testGetters: end");
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Test(org.junit.Test)

Aggregations

UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)12 InstanceDescription (org.apache.sling.discovery.InstanceDescription)5 Test (org.junit.Test)5 ClusterView (org.apache.sling.discovery.ClusterView)4 LoginException (org.apache.sling.api.resource.LoginException)3 Announcement (org.apache.sling.discovery.base.connectors.announcement.Announcement)3 LocalClusterView (org.apache.sling.discovery.commons.providers.spi.LocalClusterView)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 JsonException (javax.json.JsonException)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 AnnouncementFilter (org.apache.sling.discovery.base.connectors.announcement.AnnouncementFilter)2 AssertingTopologyEventListener (org.apache.sling.discovery.base.its.setup.mock.AssertingTopologyEventListener)2 Category (org.junit.experimental.categories.Category)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1