Search in sources :

Example 1 with RetryLoop

use of org.apache.sling.testing.tools.retry.RetryLoop in project sling by apache.

the class AbstractClusterLoadTest method doDoTest.

private void doDoTest(final int size, final int loopCnt) throws Throwable {
    if (size < 2) {
        fail("can only test 2 or more instances");
    }
    VirtualInstanceBuilder builder = newBuilder().newRepository("/var/discovery/impl/ClusterLoadTest/doTest-" + size + "-" + loopCnt + "/", true).setDebugName("firstInstance-" + size + "_" + loopCnt).setConnectorPingTimeout(3).setConnectorPingInterval(20).setMinEventDelay(0);
    VirtualInstance firstInstance = builder.build();
    firstInstance.startViewChecker(1);
    instances.add(firstInstance);
    for (int i = 1; i < size; i++) {
        VirtualInstanceBuilder builder2 = newBuilder().useRepositoryOf(builder).setDebugName("subsequentInstance-" + i + "-" + size + "_" + loopCnt).setConnectorPingTimeout(3).setMinEventDelay(0).setConnectorPingInterval(20);
        VirtualInstance subsequentInstance = builder2.build();
        instances.add(subsequentInstance);
        subsequentInstance.startViewChecker(1);
    }
    for (int i = 0; i < loopCnt; i++) {
        logger.info("=====================");
        logger.info(" START of LOOP " + i);
        logger.info("=====================");
        // count how many instances had heartbeats running in the first place
        int aliveCnt = 0;
        for (Iterator<VirtualInstance> it = instances.iterator(); it.hasNext(); ) {
            VirtualInstance instance = it.next();
            if (instance.isViewCheckerRunning()) {
                aliveCnt++;
            }
        }
        logger.info("=====================");
        logger.info(" original aliveCnt " + aliveCnt);
        logger.info("=====================");
        if (aliveCnt == 0) {
            // if no one is sending heartbeats, all instances go back to isolated mode
            aliveCnt = 1;
        }
        final int aliveCntFinal = aliveCnt;
        for (Iterator<VirtualInstance> it = instances.iterator(); it.hasNext(); ) {
            VirtualInstance instance = it.next();
            try {
                instance.dumpRepo();
            } catch (Exception e) {
                logger.error("Failed dumping repo for instance " + instance.getSlingId(), e);
            }
        }
        // then verify that each instance sees that many instances
        for (Iterator<VirtualInstance> it = instances.iterator(); it.hasNext(); ) {
            final VirtualInstance instance = it.next();
            if (!instance.isViewCheckerRunning()) {
            // if the heartbeat is not running, this instance is considered dead
            // hence we're not doing any assert here (as the count is only
            // valid if heartbeat/checkView is running and that would void the test)
            } else {
                new RetryLoop(new ConditionImplementation(instance, aliveCntFinal), INSTANCE_VIEW_TIMEOUT_SECONDS, INSTANCE_VIEW_POLL_INTERVAL_MILLIS);
            }
        }
        // start/stop heartbeats accordingly
        logger.info("Starting/Stopping heartbeats with count=" + instances.size());
        for (Iterator<VirtualInstance> it = instances.iterator(); it.hasNext(); ) {
            VirtualInstance instance = it.next();
            if (random.nextBoolean()) {
                logger.info("Starting heartbeats with " + instance.slingId);
                instance.startViewChecker(1);
                logger.info("Started heartbeats with " + instance.slingId);
            } else {
                logger.info("Stopping heartbeats with " + instance.slingId);
                instance.stopViewChecker();
                logger.info("Stopped heartbeats with " + instance.slingId);
            }
        }
    }
}
Also used : VirtualInstanceBuilder(org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder) RetryLoop(org.apache.sling.testing.tools.retry.RetryLoop) VirtualInstance(org.apache.sling.discovery.base.its.setup.VirtualInstance) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException)

Example 2 with RetryLoop

use of org.apache.sling.testing.tools.retry.RetryLoop in project sling by apache.

the class EventsCounterUtil method waitForEvent.

public static void waitForEvent(final HttpTestBase b, final String topic, int timeoutSeconds, final int previousCount) {
    final Condition c = new Condition() {

        public String getDescription() {
            return "Wait for OSGi event on topic " + topic;
        }

        public boolean isTrue() throws Exception {
            return getEventsCount(b, topic) > previousCount;
        }
    };
    new RetryLoop(c, timeoutSeconds, 500);
}
Also used : Condition(org.apache.sling.testing.tools.retry.RetryLoop.Condition) RetryLoop(org.apache.sling.testing.tools.retry.RetryLoop)

Example 3 with RetryLoop

use of org.apache.sling.testing.tools.retry.RetryLoop in project sling by apache.

the class HtmlDefaultServletTest method tearDown.

@Override
protected void tearDown() throws Exception {
    // disable the HtmlDefaultServlet after testing
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("apply", "true");
    properties.put("delete", "true");
    assertEquals(200, testClient.post(CONFIG_SERVLET, properties));
    // Verify that our test servlet is gone
    final String url = testNodeNORT.nodeUrl + ".html";
    final Condition c = new Condition() {

        public String getDescription() {
            return url + " is not served by a test servlet";
        }

        public boolean isTrue() throws Exception {
            assertNotTestServlet(getContent(url, CONTENT_TYPE_HTML));
            return true;
        }
    };
    new RetryLoop(c, 10, 100);
    // forces an async reregistration of the resource resolver factory
    try {
        Thread.sleep(3000);
    } catch (final InterruptedException ie) {
    // ignore
    }
    super.tearDown();
}
Also used : Condition(org.apache.sling.testing.tools.retry.RetryLoop.Condition) HashMap(java.util.HashMap) RetryLoop(org.apache.sling.testing.tools.retry.RetryLoop)

Example 4 with RetryLoop

use of org.apache.sling.testing.tools.retry.RetryLoop in project sling by apache.

the class HtmlDefaultServletTest method testHtmlExtension.

public void testHtmlExtension() throws IOException {
    final String url = testNodeNORT.nodeUrl + ".html";
    final Condition c = new Condition() {

        public String getDescription() {
            return url + " returns plain text";
        }

        public boolean isTrue() throws Exception {
            assertServlet(getContent(url, CONTENT_TYPE_PLAIN), HTML_DEFAULT_SERVLET_SUFFIX);
            return true;
        }
    };
    new RetryLoop(c, 10, 100);
}
Also used : Condition(org.apache.sling.testing.tools.retry.RetryLoop.Condition) RetryLoop(org.apache.sling.testing.tools.retry.RetryLoop)

Example 5 with RetryLoop

use of org.apache.sling.testing.tools.retry.RetryLoop in project sling by apache.

the class HtmlDefaultServletTest method testJsonExtension.

public void testJsonExtension() throws IOException {
    final String url = testNodeNORT.nodeUrl + ".json";
    final Condition c = new Condition() {

        public String getDescription() {
            return url + " returns JSON";
        }

        public boolean isTrue() throws Exception {
            assertNotTestServlet(getContent(url, CONTENT_TYPE_DONTCARE));
            return true;
        }
    };
    new RetryLoop(c, 10, 100);
}
Also used : Condition(org.apache.sling.testing.tools.retry.RetryLoop.Condition) RetryLoop(org.apache.sling.testing.tools.retry.RetryLoop)

Aggregations

RetryLoop (org.apache.sling.testing.tools.retry.RetryLoop)8 Condition (org.apache.sling.testing.tools.retry.RetryLoop.Condition)5 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)1 UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)1 VirtualInstance (org.apache.sling.discovery.base.its.setup.VirtualInstance)1 VirtualInstanceBuilder (org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder)1 Job (org.apache.sling.event.jobs.Job)1 JobManager (org.apache.sling.event.jobs.JobManager)1 JobConsumer (org.apache.sling.event.jobs.consumer.JobConsumer)1 RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)1 Before (org.junit.Before)1 ExamSystem (org.ops4j.pax.exam.ExamSystem)1 DefaultExamSystem (org.ops4j.pax.exam.spi.DefaultExamSystem)1 Event (org.osgi.service.event.Event)1 EventHandler (org.osgi.service.event.EventHandler)1