use of org.apache.sling.hc.api.HealthCheck 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.HealthCheck in project sling by apache.
the class HealthCheckTestsProviderTest method getMockReferences.
/** Return ServiceReferences that point to our test HealthChecks */
private ServiceReference[] getMockReferences(BundleContext bc, String OSGiFilter) {
final List<ServiceReference> refs = new ArrayList<ServiceReference>();
for (String key : LOG_SETTERS.keySet()) {
if (OSGiFilter.contains(key)) {
final HcLogSetter hls = LOG_SETTERS.get(key);
final ServiceReference ref = Mockito.mock(ServiceReference.class);
Mockito.when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(random.nextLong());
Mockito.when(ref.getProperty(HealthCheck.NAME)).thenReturn("someTest");
final HealthCheck hc = new HealthCheck() {
@Override
public Result execute() {
final FormattingResultLog log = new FormattingResultLog();
return new Result(hls.setLog(log));
}
};
Mockito.when(bc.getService(ref)).thenReturn(hc);
refs.add(ref);
}
}
return refs.toArray(new ServiceReference[] {});
}
use of org.apache.sling.hc.api.HealthCheck in project sling by apache.
the class HealthCheckFilter method getHealthChecks.
public List<HealthCheck> getHealthChecks(final HealthCheckSelector selector) {
final ServiceReference[] refs = this.getHealthCheckServiceReferences(selector);
final List<HealthCheck> result = new ArrayList<HealthCheck>();
if (refs != null) {
final List<ServiceReference> sortedRefs = Arrays.asList(refs);
Collections.sort(sortedRefs);
for (final ServiceReference ref : sortedRefs) {
final HealthCheck hc = (HealthCheck) bundleContext.getService(ref);
log.debug("Selected HealthCheck service {}", hc);
if (hc != null) {
this.usedReferences.add(ref);
result.add(hc);
}
}
}
return result;
}
Aggregations