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