Search in sources :

Example 1 with SimulatedLeaseCollection

use of org.apache.sling.discovery.oak.its.setup.SimulatedLeaseCollection in project sling by apache.

the class OakDiscoveryServiceTest method testDescriptorSeqNumChange.

@Test
public void testDescriptorSeqNumChange() throws Exception {
    logger.info("testDescriptorSeqNumChange: start");
    OakVirtualInstanceBuilder builder1 = (OakVirtualInstanceBuilder) new OakVirtualInstanceBuilder().setDebugName("instance1").newRepository("/foo/barry/foo/", true).setConnectorPingInterval(999).setConnectorPingTimeout(999);
    VirtualInstance instance1 = builder1.build();
    OakVirtualInstanceBuilder builder2 = (OakVirtualInstanceBuilder) new OakVirtualInstanceBuilder().setDebugName("instance2").useRepositoryOf(instance1).setConnectorPingInterval(999).setConnectorPingTimeout(999);
    VirtualInstance instance2 = builder2.build();
    logger.info("testDescriptorSeqNumChange: created both instances, binding listener...");
    DummyListener listener = new DummyListener();
    OakDiscoveryService discoveryService = (OakDiscoveryService) instance1.getDiscoveryService();
    discoveryService.bindTopologyEventListener(listener);
    logger.info("testDescriptorSeqNumChange: waiting 2sec, listener should not get anything yet");
    assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
    assertEquals(0, listener.countEvents());
    logger.info("testDescriptorSeqNumChange: issuing 2 heartbeats with each instance should let the topology get established");
    instance1.heartbeatsAndCheckView();
    instance2.heartbeatsAndCheckView();
    instance1.heartbeatsAndCheckView();
    instance2.heartbeatsAndCheckView();
    logger.info("testDescriptorSeqNumChange: listener should get an event within 2sec from now at latest");
    assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
    assertEquals(1, listener.countEvents());
    ResourceResolverFactory factory = instance1.getResourceResolverFactory();
    ResourceResolver resolver = factory.getServiceResourceResolver(null);
    instance1.heartbeatsAndCheckView();
    assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
    assertEquals(1, listener.countEvents());
    // increment the seqNum by 2 - simulating a coming and going instance
    // while we were sleeping
    SimulatedLeaseCollection c = builder1.getSimulatedLeaseCollection();
    c.incSeqNum(2);
    logger.info("testDescriptorSeqNumChange: incremented seqnum by 2 - issuing another heartbeat should trigger a topology change");
    instance1.heartbeatsAndCheckView();
    // due to the nature of the syncService/minEventDelay we now explicitly first sleep 2sec before waiting for async events for another 2sec
    logger.info("testDescriptorSeqNumChange: sleeping 2sec for topology change to happen");
    Thread.sleep(2000);
    logger.info("testDescriptorSeqNumChange: ensuring no async events are still in the pipe - for another 2sec");
    assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
    logger.info("testDescriptorSeqNumChange: now listener should have received 3 events, it got: " + listener.countEvents());
    assertEquals(3, listener.countEvents());
}
Also used : OakVirtualInstanceBuilder(org.apache.sling.discovery.oak.its.setup.OakVirtualInstanceBuilder) SimulatedLeaseCollection(org.apache.sling.discovery.oak.its.setup.SimulatedLeaseCollection) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) VirtualInstance(org.apache.sling.discovery.base.its.setup.VirtualInstance) DummyListener(org.apache.sling.discovery.commons.providers.base.DummyListener) Test(org.junit.Test)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)1 VirtualInstance (org.apache.sling.discovery.base.its.setup.VirtualInstance)1 DummyListener (org.apache.sling.discovery.commons.providers.base.DummyListener)1 OakVirtualInstanceBuilder (org.apache.sling.discovery.oak.its.setup.OakVirtualInstanceBuilder)1 SimulatedLeaseCollection (org.apache.sling.discovery.oak.its.setup.SimulatedLeaseCollection)1 Test (org.junit.Test)1