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);
}
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);
}
}
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));
}
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());
}
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")));
}
Aggregations