use of org.apache.sling.hc.api.Result in project sling by apache.
the class CompositeHealthCheckTest method testExecution.
@Test
public void testExecution() {
doReturn((Result) null).when(compositeHealthCheck).checkForRecursion(Matchers.<ServiceReference>any(), Matchers.<Set<String>>any());
String[] testTags = new String[] { "tag1" };
compositeHealthCheck.setFilterTags(testTags);
List<HealthCheckExecutionResult> executionResults = new LinkedList<HealthCheckExecutionResult>();
executionResults.add(createExecutionResult("Check 1", testTags, new Result(Result.Status.INFO, "Good")));
executionResults.add(createExecutionResult("Check 2", testTags, new Result(Result.Status.CRITICAL, "Bad")));
when(healthCheckExecutor.execute(any(HealthCheckSelector.class))).thenReturn(executionResults);
Result result = compositeHealthCheck.execute();
verify(healthCheckExecutor, times(1)).execute(argThat(selectorWithTags(testTags)));
assertEquals(Result.Status.CRITICAL, result.getStatus());
}
use of org.apache.sling.hc.api.Result in project sling by apache.
the class CompositeHealthCheckTest method testSimpleRecursion.
@Test
public void testSimpleRecursion() {
// composite check referencing itself
final String[] filterTags = new String[] { "check1" };
final DummyHcServiceReference hcRef = new DummyHcServiceReference("Check 1", new String[] { "check1" }, filterTags);
// test check is hcRef
doReturn(hcRef).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[] { hcRef };
}
}
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 AnnotatedHealthCheckSample method execute.
@Override
public Result execute() {
final FormattingResultLog resultLog = new FormattingResultLog();
resultLog.info("All good at {}", new Date());
return new Result(resultLog);
}
use of org.apache.sling.hc.api.Result in project sling by apache.
the class SlowHealthCheckSample method execute.
@Override
public Result execute() {
final FormattingResultLog resultLog = new FormattingResultLog();
try {
final long randomDelay = (long) (Math.random() * (maxExecutionTime - minExecutionTime));
final long toSleep = minExecutionTime + randomDelay;
resultLog.debug("Executing {} will last {} msec", this, toSleep);
Thread.sleep(toSleep);
} catch (InterruptedException iex) {
resultLog.warn("{} during execution", iex.getClass().getSimpleName());
}
final String execMsg = "Done executing, execution counter=" + counter.incrementAndGet();
resultLog.debug("{}:{}", this, execMsg);
log.debug("{}:{}", this, execMsg);
return new Result(resultLog);
}
use of org.apache.sling.hc.api.Result in project sling by apache.
the class DefaultLoginsHealthCheck method execute.
@Override
public Result execute() {
final FormattingResultLog resultLog = new FormattingResultLog();
int checked = 0;
int failures = 0;
for (String login : logins) {
final String[] parts = login.split(":");
if (parts.length != 2) {
resultLog.warn("Expected login in the form username:password, got [{}]", login);
continue;
}
checked++;
final String username = parts[0].trim();
final String password = parts[1].trim();
final Credentials creds = new SimpleCredentials(username, password.toCharArray());
Session s = null;
try {
s = repository.login(creds);
if (s != null) {
failures++;
resultLog.warn("Login as [{}] succeeded, was expecting it to fail", username);
} else {
resultLog.debug("Login as [{}] didn't throw an Exception but returned null Session", username);
}
} catch (RepositoryException re) {
resultLog.debug("Login as [{}] failed, as expected", username);
} finally {
if (s != null) {
s.logout();
}
}
}
if (checked == 0) {
resultLog.warn("Did not check any logins, configured logins={}", logins);
} else if (failures != 0) {
resultLog.warn("Checked {} logins, {} failures", checked, failures);
} else {
resultLog.debug("Checked {} logins, all successful", checked, failures);
}
return new Result(resultLog);
}
Aggregations