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, hc, props);
try {
{
// Wait for HC to be registered
final long timeout = System.currentTimeMillis() + 10000L;
boolean hcFound = false;
while(System.currentTimeMillis() < timeout) {
final List<HealthCheckExecutionResult> results = executor.execute(id);
if(!results.isEmpty()) {
hcFound = true;
break;
}
Thread.sleep(100L);
}
assertTrue("Expecting HC to become active", hcFound);
}
// 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(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(id);
}
assertTrue("Expecting counter to increment asynchronously", counter.get() < previous + n);
} finally {
reg.unregister();
}
}