Search in sources :

Example 6 with RetryLoop

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

the class ITMDCFilter method startContainer.

@Before
public void startContainer() throws Exception {
    if (testContainer == null) {
        ServerConfiguration sc = new ServerConfiguration();
        ExamSystem system = DefaultExamSystem.create(sc.config());
        testContainer = PaxExamRuntime.createContainer(system);
        testContainer.start();
        new RetryLoop(new RetryLoop.Condition() {

            public String getDescription() {
                return "Check if MDCTestServlet is up";
            }

            public boolean isTrue() throws Exception {
                RequestBuilder rb = new RequestBuilder(ServerConfiguration.getServerUrl());
                executor.execute(rb.buildGetRequest("/mdc")).assertStatus(200);
                rb = new RequestBuilder(ServerConfiguration.getServerUrl());
                //Create test config via servlet
                executor.execute(rb.buildGetRequest("/mdc", "createTestConfig", "true"));
                TimeUnit.SECONDS.sleep(1);
                return true;
            }
        }, 5, 100);
    }
}
Also used : ExamSystem(org.ops4j.pax.exam.ExamSystem) DefaultExamSystem(org.ops4j.pax.exam.spi.DefaultExamSystem) RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) RetryLoop(org.apache.sling.testing.tools.retry.RetryLoop) IOException(java.io.IOException) Before(org.junit.Before)

Example 7 with RetryLoop

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

the class ClassloadingTest method testFailedClassloading.

@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testFailedClassloading() throws Exception {
    final AtomicInteger failedJobsCount = new AtomicInteger(0);
    final List<Event> finishedEvents = Collections.synchronizedList(new ArrayList<Event>());
    this.registerJobConsumer(TOPIC + "/failed", new JobConsumer() {

        @Override
        public JobResult process(Job job) {
            failedJobsCount.incrementAndGet();
            return JobResult.OK;
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(Event event) {
            finishedEvents.add(event);
        }
    });
    final JobManager jobManager = this.getJobManager();
    // dao is an invisible class for the dynamic class loader as it is not public
    // therefore scheduling this job should fail!
    final DataObject dao = new DataObject();
    // we start a single job
    final Map<String, Object> props = new HashMap<String, Object>();
    props.put("dao", dao);
    final String id = jobManager.addJob(TOPIC + "/failed", props).getId();
    try {
        // wait until the conditions are met
        new RetryLoop(new RetryLoop.Condition() {

            @Override
            public boolean isTrue() throws Exception {
                return failedJobsCount.get() == 0 && finishedEvents.size() == 0 && jobManager.findJobs(JobManager.QueryType.ALL, TOPIC + "/failed", -1, (Map<String, Object>[]) null).size() == 1 && jobManager.getStatistics().getNumberOfQueuedJobs() == 0 && jobManager.getStatistics().getNumberOfActiveJobs() == 0;
            }

            @Override
            public String getDescription() {
                return "Waiting for job failure to be recorded. Conditions " + "faildJobsCount=" + failedJobsCount.get() + ", finishedEvents=" + finishedEvents.size() + ", findJobs= " + jobManager.findJobs(JobManager.QueryType.ALL, TOPIC + "/failed", -1, (Map<String, Object>[]) null).size() + ", queuedJobs=" + jobManager.getStatistics().getNumberOfQueuedJobs() + ", activeJobs=" + jobManager.getStatistics().getNumberOfActiveJobs();
            }
        }, CONDITION_TIMEOUT_SECONDS, CONDITION_INTERVAL_MILLIS);
        // moves the job to the history section
        jobManager.removeJobById(id);
        assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC + "/failed", -1, (Map<String, Object>[]) null).size());
    } finally {
        // removes the job permanently
        jobManager.removeJobById(id);
    }
}
Also used : HashMap(java.util.HashMap) RetryLoop(org.apache.sling.testing.tools.retry.RetryLoop) EventHandler(org.osgi.service.event.EventHandler) JobManager(org.apache.sling.event.jobs.JobManager) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.osgi.service.event.Event) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) Job(org.apache.sling.event.jobs.Job) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with RetryLoop

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

the class JspScriptingTest method testChangingJsp.

/* Verify that overwriting a JSP script changes the output within a reasonable time 
     * (might not be immediate as there's some observation and caching involved) */
@Test
public void testChangingJsp() throws Exception {
    String toDelete = null;
    try {
        final String[] scripts = { "jsp1.jsp", "jsp2.jsp" };
        for (String script : scripts) {
            toDelete = H.uploadTestScript(unstructuredNode.scriptPath, script, "html.jsp");
            final String expected = "text from " + script + ":" + unstructuredNode.testText;
            final Condition c = new Condition() {

                public String getDescription() {
                    return "Expecting " + expected;
                }

                public boolean isTrue() throws Exception {
                    final String content = H.getContent(unstructuredNode.nodeUrl + ".html", HttpTest.CONTENT_TYPE_HTML);
                    return content.contains(expected);
                }
            };
            new RetryLoop(c, CHECK_CONTENT_TIMEOUT_SECONDS, CHECK_CONTENT_INTERVAL_MSEC);
        }
    } finally {
        if (toDelete != null) {
            H.getTestClient().delete(toDelete);
        }
    }
}
Also used : Condition(org.apache.sling.testing.tools.retry.RetryLoop.Condition) RetryLoop(org.apache.sling.testing.tools.retry.RetryLoop) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

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