Search in sources :

Example 21 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class HealthCheckExecutorImpl method collectResultsFromFutures.

/**
     * Collect the results from all futures
     * @param futuresForResultOfThisCall The list of futures
     * @param results The result collection
     */
void collectResultsFromFutures(final List<HealthCheckFuture> futuresForResultOfThisCall, final Collection<HealthCheckExecutionResult> results) {
    final Set<HealthCheckExecutionResult> resultsFromFutures = new HashSet<HealthCheckExecutionResult>();
    final Iterator<HealthCheckFuture> futuresIt = futuresForResultOfThisCall.iterator();
    while (futuresIt.hasNext()) {
        final HealthCheckFuture future = futuresIt.next();
        final HealthCheckExecutionResult result = this.collectResultFromFuture(future);
        resultsFromFutures.add(result);
        futuresIt.remove();
    }
    logger.debug("Adding {} results from futures", resultsFromFutures.size());
    results.addAll(resultsFromFutures);
}
Also used : HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) HashSet(java.util.HashSet)

Example 22 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class HealthCheckMBeanTest method testBean.

@Test
public void testBean() throws Exception {
    final ServiceReference ref = new ServiceReference() {

        @Override
        public boolean isAssignableTo(Bundle bundle, String className) {
            return false;
        }

        @Override
        public Bundle[] getUsingBundles() {
            return null;
        }

        @Override
        public String[] getPropertyKeys() {
            return null;
        }

        @Override
        public Object getProperty(String key) {
            return null;
        }

        @Override
        public Bundle getBundle() {
            return null;
        }

        @Override
        public int compareTo(Object reference) {
            return 0;
        }
    };
    final HealthCheckMBean mbean = new HealthCheckMBean(ref, new ExtendedHealthCheckExecutor() {

        @SuppressWarnings("deprecation")
        @Override
        public List<HealthCheckExecutionResult> execute(String... tags) {
            return null;
        }

        @Override
        public HealthCheckExecutionResult execute(ServiceReference ref) {
            // TODO Auto-generated method stub
            return new HealthCheckExecutionResult() {

                @Override
                public Result getHealthCheckResult() {
                    // TODO Auto-generated method stub
                    return testHealthCheck.execute();
                }

                @Override
                public HealthCheckMetadata getHealthCheckMetadata() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public Date getFinishedAt() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public long getElapsedTimeInMs() {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public boolean hasTimedOut() {
                    // TODO Auto-generated method stub
                    return false;
                }
            };
        }

        @SuppressWarnings("deprecation")
        @Override
        public List<HealthCheckExecutionResult> execute(HealthCheckExecutionOptions options, String... tags) {
            return null;
        }

        @Override
        public List<HealthCheckExecutionResult> execute(HealthCheckSelector selector) {
            return null;
        }

        @Override
        public List<HealthCheckExecutionResult> execute(HealthCheckSelector selector, HealthCheckExecutionOptions options) {
            return null;
        }
    });
    final ObjectName name = new ObjectName(OBJECT_NAME);
    jmxServer.registerMBean(mbean, name);
    try {
        resultOk = true;
        assertJmxValue(OBJECT_NAME, "ok", "true", true);
        Thread.sleep(1500);
        resultOk = false;
        assertJmxValue(OBJECT_NAME, "ok", "true", false);
        Thread.sleep(1500);
        assertJmxValue(OBJECT_NAME, "log", "contains message=Result is not ok!", true);
    } finally {
        jmxServer.unregisterMBean(name);
    }
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) Bundle(org.osgi.framework.Bundle) ExtendedHealthCheckExecutor(org.apache.sling.hc.core.impl.executor.ExtendedHealthCheckExecutor) Date(java.util.Date) ServiceReference(org.osgi.framework.ServiceReference) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ObjectName(javax.management.ObjectName) HealthCheckSelector(org.apache.sling.hc.api.execution.HealthCheckSelector) HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) List(java.util.List) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 23 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class SampleHealthChecksTest method testAnnotatedHC.

@Test
public void testAnnotatedHC() {
    final List<HealthCheckExecutionResult> results = executor.execute(HealthCheckSelector.tags("annotation", "sample"));
    assertNotNull("Expecting non-null results");
    assertEquals("Expecting a single result", 1, results.size());
    final HealthCheckExecutionResult r = results.get(0);
    assertTrue("Expecting non-empty HC log", r.getHealthCheckResult().iterator().hasNext());
    final String expected = "All good";
    assertTrue("Expecting first log message to contain " + expected, r.getHealthCheckResult().iterator().next().getMessage().contains(expected));
}
Also used : HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 24 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class HealthCheckResultCacheTest method testHealthCheckResultCache.

@Test
public void testHealthCheckResultCache() {
    HealthCheckMetadata hc1 = setupHealthCheckMetadata(1, HC_TIMEOUT_NOT_SET);
    ExecutionResult executionResult1 = spy(new ExecutionResult(hc1, new Result(Result.Status.OK, "result for hc1"), 1));
    doReturn(new Date(new Date().getTime() - DUR_1_MIN)).when(executionResult1).getFinishedAt();
    healthCheckResultCache.updateWith(executionResult1);
    HealthCheckMetadata hc2 = setupHealthCheckMetadata(2, HC_TIMEOUT_NOT_SET);
    ExecutionResult executionResult2 = spy(new ExecutionResult(hc2, new Result(Result.Status.OK, "result for hc2"), 1));
    doReturn(new Date(new Date().getTime() - DUR_3_MIN)).when(executionResult2).getFinishedAt();
    healthCheckResultCache.updateWith(executionResult2);
    HealthCheckMetadata hc3 = setupHealthCheckMetadata(3, DUR_4_MIN);
    ExecutionResult executionResult3 = spy(new ExecutionResult(hc3, new Result(Result.Status.OK, "result for hc3"), 1));
    doReturn(new Date(new Date().getTime() - DUR_3_MIN)).when(executionResult3).getFinishedAt();
    healthCheckResultCache.updateWith(executionResult3);
    HealthCheckMetadata hc4 = setupHealthCheckMetadata(4, HC_TIMEOUT_NOT_SET);
    // no result for this yet
    List<HealthCheckMetadata> hcList = new ArrayList<HealthCheckMetadata>(Arrays.asList(hc1, hc2, hc3, hc4));
    List<HealthCheckExecutionResult> results = new ArrayList<HealthCheckExecutionResult>();
    healthCheckResultCache.useValidCacheResults(hcList, results, DUR_2_MIN);
    // result too old, left in hcList for later execution
    assertTrue(hcList.contains(hc2));
    // no result was added to cache via updateWith()
    assertTrue(hcList.contains(hc4));
    // true <= result one min old, global timeout 2min
    assertTrue(results.contains(executionResult1));
    // false <= result three min old, global timeout 2min
    assertFalse(results.contains(executionResult2));
    // true <= result one three old, HC timeout 4min
    assertTrue(results.contains(executionResult3));
    // values not found in cache are left in hcList
    assertEquals(2, hcList.size());
    assertEquals(2, results.size());
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) ArrayList(java.util.ArrayList) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Date(java.util.Date) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 25 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult 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)

Aggregations

HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)34 Result (org.apache.sling.hc.api.Result)18 Test (org.junit.Test)16 Date (java.util.Date)9 HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)9 HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)8 ExecutionResult (org.apache.sling.hc.core.impl.executor.ExecutionResult)7 ArrayList (java.util.ArrayList)6 TreeSet (java.util.TreeSet)6 LinkedList (java.util.LinkedList)5 Matchers.anyString (org.mockito.Matchers.anyString)5 SimpleDateFormat (java.text.SimpleDateFormat)4 DateFormat (java.text.DateFormat)3 HealthCheckSelector (org.apache.sling.hc.api.execution.HealthCheckSelector)3 ServiceReference (org.osgi.framework.ServiceReference)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 HashSet (java.util.HashSet)2 ResultLog (org.apache.sling.hc.api.ResultLog)2 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)2