Search in sources :

Example 6 with HealthCheck

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();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Hashtable(java.util.Hashtable) HealthCheck(org.apache.sling.hc.api.HealthCheck) Result(org.apache.sling.hc.api.Result) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 7 with HealthCheck

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[] {});
}
Also used : FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) ArrayList(java.util.ArrayList) HealthCheck(org.apache.sling.hc.api.HealthCheck) ServiceReference(org.osgi.framework.ServiceReference) Result(org.apache.sling.hc.api.Result)

Example 8 with HealthCheck

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;
}
Also used : ArrayList(java.util.ArrayList) HealthCheck(org.apache.sling.hc.api.HealthCheck) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

HealthCheck (org.apache.sling.hc.api.HealthCheck)8 Hashtable (java.util.Hashtable)5 Result (org.apache.sling.hc.api.Result)5 ArrayList (java.util.ArrayList)2 HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)2 ServiceReference (org.osgi.framework.ServiceReference)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ComponentDescription (org.apache.felix.scrplugin.description.ComponentDescription)1 ServiceDescription (org.apache.felix.scrplugin.description.ServiceDescription)1 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)1 Test (org.junit.Test)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1