Search in sources :

Example 6 with HealthCheckExecutionResult

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

the class HealthCheckExecutorServletTest method testDoGetNameAndTagInPath.

@Test
public void testDoGetNameAndTagInPath() throws ServletException, IOException {
    final String testTag = "testTag";
    final String testName = "test name";
    doReturn(testTag + "," + testName).when(request).getPathInfo();
    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[] { testName }), eq(new HealthCheckExecutionOptions()));
    healthCheckExecutorServlet.doGet(request, response);
    verify(request, never()).getParameter(HealthCheckExecutorServlet.PARAM_TAGS.name);
    verify(request, never()).getParameter(HealthCheckExecutorServlet.PARAM_NAMES.name);
    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 7 with HealthCheckExecutionResult

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

the class HealthCheckExecutorServletTest method getExecutionResults.

private List<HealthCheckExecutionResult> getExecutionResults(Result.Status worstStatus) {
    List<HealthCheckExecutionResult> results = new ArrayList<HealthCheckExecutionResult>();
    results.add(new ExecutionResult(new HealthCheckMetadata(hcServiceRef), new Result(worstStatus, worstStatus.name()), 100));
    results.add(new ExecutionResult(new HealthCheckMetadata(hcServiceRef), new Result(Result.Status.OK, "OK"), 100));
    return results;
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) ArrayList(java.util.ArrayList) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) 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)

Example 8 with HealthCheckExecutionResult

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

the class HealthCheckExecutorServletTest method testDoGetJson.

@Test
public void testDoGetJson() throws ServletException, IOException {
    final String testTag = "testTag";
    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);
    doReturn("/" + testTag + ".json").when(request).getPathInfo();
    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(txtSerializer);
    verifyZeroInteractions(verboseTxtSerializer);
    verify(jsonSerializer).serialize(resultEquals(new Result(Result.Status.WARN, "Overall Status WARN")), eq(executionResults), anyString(), 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 9 with HealthCheckExecutionResult

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

the class HealthCheckResultCache method useValidCacheResults.

/**
     * Get the valid cache results
     */
public void useValidCacheResults(final List<HealthCheckMetadata> metadatas, final Collection<HealthCheckExecutionResult> results, final long resultCacheTtlInMs) {
    final Set<HealthCheckExecutionResult> cachedResults = new TreeSet<HealthCheckExecutionResult>();
    final Iterator<HealthCheckMetadata> checksIt = metadatas.iterator();
    while (checksIt.hasNext()) {
        final HealthCheckMetadata md = checksIt.next();
        final HealthCheckExecutionResult result = getValidCacheResult(md, resultCacheTtlInMs);
        if (result != null) {
            cachedResults.add(result);
            checksIt.remove();
        }
    }
    logger.debug("Adding {} results from cache", cachedResults.size());
    results.addAll(cachedResults);
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) TreeSet(java.util.TreeSet) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 10 with HealthCheckExecutionResult

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

the class HealthCheckResultCache method get.

private HealthCheckExecutionResult get(final HealthCheckMetadata metadata, final long globalResultCacheTtlInMs) {
    final Long key = metadata.getServiceId();
    final HealthCheckExecutionResult cachedResult = cache.get(key);
    if (cachedResult != null) {
        Date finishedAt = cachedResult.getFinishedAt();
        if (finishedAt == null) {
            // never cache without proper meta data -> remove it
            cache.remove(key);
            return null;
        }
        long effectiveTtl = getEffectiveTtl(metadata, globalResultCacheTtlInMs);
        long validUntilLong = finishedAt.getTime() + effectiveTtl;
        if (validUntilLong < 0) {
            // if Long.MAX_VALUE is configured, this can become negative
            validUntilLong = Long.MAX_VALUE;
        }
        Date validUntil = new Date(validUntilLong);
        Date now = new Date();
        if (validUntil.after(now)) {
            logger.debug("Cache hit: validUntil={} cachedResult={}", validUntil, cachedResult);
            return cachedResult;
        } else {
            logger.debug("Outdated result: validUntil={} cachedResult={}", validUntil, cachedResult);
        // not removing result for key as out-dated results are shown for timed out checks if available
        }
    }
    // null => no cache hit
    return null;
}
Also used : HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Date(java.util.Date)

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