Search in sources :

Example 16 with FormattingResultLog

use of org.apache.sling.hc.util.FormattingResultLog 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 17 with FormattingResultLog

use of org.apache.sling.hc.util.FormattingResultLog in project sling by apache.

the class AsyncHealthCheckSample method execute.

@Override
public Result execute() {
    final long toWait = (long) (Math.random() * 2 * PERIOD_SECONDS);
    log.info("{} - Waiting {} seconds to simulate an expensive operation...", this, toWait);
    try {
        Thread.sleep(toWait * 1000L);
    } catch (InterruptedException iex) {
        log.warn("Sleep interrupted", iex);
    }
    final int value = counter.incrementAndGet();
    log.info("{} - counter set to {}", this, value);
    final FormattingResultLog resultLog = new FormattingResultLog();
    resultLog.info("{} - counter value set to {} at {}", this, value, new Date());
    if (value % 2 != 0) {
        resultLog.warn("Counter value ({}) is not even", value);
    }
    return new Result(resultLog);
}
Also used : FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Date(java.util.Date) Result(org.apache.sling.hc.api.Result)

Aggregations

FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)17 Result (org.apache.sling.hc.api.Result)15 Date (java.util.Date)3 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)2 Test (org.junit.Test)2 ServiceReference (org.osgi.framework.ServiceReference)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Credentials (javax.jcr.Credentials)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1