Search in sources :

Example 36 with Result

use of org.apache.sling.hc.api.Result in project sling by apache.

the class HealthCheckExecutorServletTest method testDoGetTxt.

@Test
public void testDoGetTxt() throws ServletException, IOException {
    final String testTag = "testTag";
    doReturn(testTag).when(request).getParameter(HealthCheckExecutorServlet.PARAM_TAGS.name);
    doReturn(HealthCheckExecutorServlet.FORMAT_TXT).when(request).getParameter(HealthCheckExecutorServlet.PARAM_FORMAT.name);
    doReturn("true").when(request).getParameter(HealthCheckExecutorServlet.PARAM_COMBINE_TAGS_WITH_OR.name);
    int timeout = 5000;
    doReturn(timeout + "").when(request).getParameter(HealthCheckExecutorServlet.PARAM_OVERRIDE_GLOBAL_TIMEOUT.name);
    final List<HealthCheckExecutionResult> executionResults = getExecutionResults(Result.Status.WARN);
    HealthCheckExecutionOptions options = new HealthCheckExecutionOptions();
    options.setCombineTagsWithOr(true);
    options.setOverrideGlobalTimeout(timeout);
    doReturn(executionResults).when(healthCheckExecutor).execute(selector(new String[] { testTag }, new String[0]), eq(options));
    healthCheckExecutorServlet.doGet(request, response);
    verifyZeroInteractions(htmlSerializer);
    verifyZeroInteractions(jsonSerializer);
    verifyZeroInteractions(verboseTxtSerializer);
    verify(txtSerializer).serialize(resultEquals(new Result(Result.Status.WARN, "Overall Status WARN")));
}
Also used : HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) Matchers.anyString(org.mockito.Matchers.anyString) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 37 with Result

use of org.apache.sling.hc.api.Result in project sling by apache.

the class HealthCheckExecutorServletTest method testDoGetHtml.

@Test
public void testDoGetHtml() throws ServletException, IOException {
    final String testTag = "testTag";
    doReturn(testTag).when(request).getParameter(HealthCheckExecutorServlet.PARAM_TAGS.name);
    doReturn("false").when(request).getParameter(HealthCheckExecutorServlet.PARAM_COMBINE_TAGS_WITH_OR.name);
    final List<HealthCheckExecutionResult> executionResults = getExecutionResults(Result.Status.CRITICAL);
    doReturn(executionResults).when(healthCheckExecutor).execute(selector(new String[] { testTag }, new String[0]), eq(new HealthCheckExecutionOptions()));
    healthCheckExecutorServlet.doGet(request, response);
    verifyZeroInteractions(jsonSerializer);
    verifyZeroInteractions(txtSerializer);
    verifyZeroInteractions(verboseTxtSerializer);
    verify(htmlSerializer).serialize(resultEquals(new Result(Result.Status.CRITICAL, "Overall Status CRITICAL")), eq(executionResults), contains("Supported URL parameters"), eq(false));
}
Also used : HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) Matchers.anyString(org.mockito.Matchers.anyString) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 38 with Result

use of org.apache.sling.hc.api.Result in project sling by apache.

the class AsyncHealthCheckIT method testAsyncHealthCheck.

@Test
public void testAsyncHealthCheck() throws InterruptedException {
    final String id = UUID.randomUUID().toString();
    final AtomicInteger counter = new AtomicInteger(Integer.MIN_VALUE);
    final HealthCheck hc = new HealthCheck() {

        @Override
        public Result execute() {
            final int v = counter.incrementAndGet();
            return new Result(Result.Status.OK, "counter is now " + v);
        }
    };
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(HealthCheck.NAME, "name_" + id);
    props.put(HealthCheck.TAGS, id);
    props.put(HealthCheck.ASYNC_CRON_EXPRESSION, "*/1 * * * * ?");
    @SuppressWarnings("rawtypes") final ServiceRegistration reg = bundleContext.registerService(HealthCheck.class.getName(), hc, props);
    try {
        // Wait for HC to be registered
        U.expectHealthChecks(1, executor, id);
        // Now reset the counter and check that HC increments it even if we don't
        // use the executor
        {
            counter.set(0);
            final long timeout = System.currentTimeMillis() + 5000L;
            while (System.currentTimeMillis() < timeout) {
                if (counter.get() > 0) {
                    break;
                }
                Thread.sleep(100L);
            }
            assertTrue("Expecting counter to be incremented", counter.get() > 0);
        }
        // Verify that we get the right log
        final String msg = executor.execute(HealthCheckSelector.tags(id)).get(0).getHealthCheckResult().iterator().next().getMessage();
        assertTrue("Expecting the right message: " + msg, msg.contains("counter is now"));
        // And verify that calling executor lots of times doesn't increment as much
        final int previous = counter.get();
        final int n = 100;
        for (int i = 0; i < n; i++) {
            executor.execute(HealthCheckSelector.tags(id));
        }
        assertTrue("Expecting counter to increment asynchronously", counter.get() < previous + n);
    } finally {
        reg.unregister();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Hashtable(java.util.Hashtable) HealthCheck(org.apache.sling.hc.api.HealthCheck) Result(org.apache.sling.hc.api.Result) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 39 with Result

use of org.apache.sling.hc.api.Result in project sling by apache.

the class CompositeHealthCheckTest method testCyclicRecursion.

@Test
public void testCyclicRecursion() {
    // three checks, cyclic
    final String[] filterTags = new String[] { "check2" };
    final DummyHcServiceReference hcRef1 = new DummyHcServiceReference("Check 1", new String[] { "check1" }, filterTags);
    final DummyHcServiceReference hcRef2 = new DummyHcServiceReference("Check 2", new String[] { "check2" }, new String[] { "check3" });
    final DummyHcServiceReference hcRef3 = new DummyHcServiceReference("Check 3", new String[] { "check3" }, new String[] { "check1" });
    // test check is hcRef1
    doReturn(hcRef1).when(componentContext).getServiceReference();
    compositeHealthCheck.setFilterTags(filterTags);
    compositeHealthCheck.setHealthCheckFilter(new HealthCheckFilter(null) {

        @Override
        public ServiceReference[] getHealthCheckServiceReferences(HealthCheckSelector selector) {
            String[] tags = selector.tags();
            ServiceReference[] result = new ServiceReference[] {};
            if (tags.length > 0) {
                if (tags[0].equals(filterTags[0])) {
                    result = new ServiceReference[] { hcRef2 };
                } else if (tags[0].equals("check3")) {
                    result = new ServiceReference[] { hcRef3 };
                } else if (tags[0].equals("check1")) {
                    result = new ServiceReference[] { hcRef1 };
                }
            }
            return result;
        }
    });
    Result result = compositeHealthCheck.execute();
    verify(healthCheckExecutor, never()).execute(any(HealthCheckSelector.class));
    assertEquals(Result.Status.HEALTH_CHECK_ERROR, result.getStatus());
}
Also used : HealthCheckSelector(org.apache.sling.hc.api.execution.HealthCheckSelector) HealthCheckFilter(org.apache.sling.hc.util.HealthCheckFilter) ServiceReference(org.osgi.framework.ServiceReference) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 40 with Result

use of org.apache.sling.hc.api.Result in project sling by apache.

the class JmxAttributeHealthCheckTest method assertJmxValue.

static void assertJmxValue(String objectName, String attributeName, String constraint, boolean expected) {
    final JmxAttributeHealthCheck hc = new JmxAttributeHealthCheck();
    final Map<String, Object> props = new HashMap<String, Object>();
    props.put(JmxAttributeHealthCheck.PROP_OBJECT_NAME, objectName);
    props.put(JmxAttributeHealthCheck.PROP_ATTRIBUTE_NAME, attributeName);
    props.put(JmxAttributeHealthCheck.PROP_CONSTRAINT, constraint);
    hc.activate(props);
    final Result r = hc.execute();
    assertEquals("Expected result " + expected, expected, r.isOk());
}
Also used : HashMap(java.util.HashMap) Result(org.apache.sling.hc.api.Result)

Aggregations

Result (org.apache.sling.hc.api.Result)50 HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)25 Test (org.junit.Test)19 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)16 HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)9 ExecutionResult (org.apache.sling.hc.core.impl.executor.ExecutionResult)9 ArrayList (java.util.ArrayList)8 HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)8 Matchers.anyString (org.mockito.Matchers.anyString)8 Date (java.util.Date)7 ServiceReference (org.osgi.framework.ServiceReference)6 HealthCheck (org.apache.sling.hc.api.HealthCheck)5 HealthCheckSelector (org.apache.sling.hc.api.execution.HealthCheckSelector)5 Hashtable (java.util.Hashtable)4 DistributionAgent (org.apache.sling.distribution.agent.DistributionAgent)4 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)4 SimpleDateFormat (java.text.SimpleDateFormat)3 HashSet (java.util.HashSet)3 ObjectName (javax.management.ObjectName)3 DistributionQueueEntry (org.apache.sling.distribution.queue.DistributionQueueEntry)3