Search in sources :

Example 11 with HealthCheckMetadata

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

the class HealthCheckMBeanTest method testBean.

@Test
public void testBean() throws Exception {
    final ServiceReference ref = new ServiceReference() {

        @Override
        public boolean isAssignableTo(Bundle bundle, String className) {
            return false;
        }

        @Override
        public Bundle[] getUsingBundles() {
            return null;
        }

        @Override
        public String[] getPropertyKeys() {
            return null;
        }

        @Override
        public Object getProperty(String key) {
            return null;
        }

        @Override
        public Bundle getBundle() {
            return null;
        }

        @Override
        public int compareTo(Object reference) {
            return 0;
        }
    };
    final HealthCheckMBean mbean = new HealthCheckMBean(ref, new ExtendedHealthCheckExecutor() {

        @SuppressWarnings("deprecation")
        @Override
        public List<HealthCheckExecutionResult> execute(String... tags) {
            return null;
        }

        @Override
        public HealthCheckExecutionResult execute(ServiceReference ref) {
            // TODO Auto-generated method stub
            return new HealthCheckExecutionResult() {

                @Override
                public Result getHealthCheckResult() {
                    // TODO Auto-generated method stub
                    return testHealthCheck.execute();
                }

                @Override
                public HealthCheckMetadata getHealthCheckMetadata() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public Date getFinishedAt() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public long getElapsedTimeInMs() {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public boolean hasTimedOut() {
                    // TODO Auto-generated method stub
                    return false;
                }
            };
        }

        @SuppressWarnings("deprecation")
        @Override
        public List<HealthCheckExecutionResult> execute(HealthCheckExecutionOptions options, String... tags) {
            return null;
        }

        @Override
        public List<HealthCheckExecutionResult> execute(HealthCheckSelector selector) {
            return null;
        }

        @Override
        public List<HealthCheckExecutionResult> execute(HealthCheckSelector selector, HealthCheckExecutionOptions options) {
            return null;
        }
    });
    final ObjectName name = new ObjectName(OBJECT_NAME);
    jmxServer.registerMBean(mbean, name);
    try {
        resultOk = true;
        assertJmxValue(OBJECT_NAME, "ok", "true", true);
        Thread.sleep(1500);
        resultOk = false;
        assertJmxValue(OBJECT_NAME, "ok", "true", false);
        Thread.sleep(1500);
        assertJmxValue(OBJECT_NAME, "log", "contains message=Result is not ok!", true);
    } finally {
        jmxServer.unregisterMBean(name);
    }
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) Bundle(org.osgi.framework.Bundle) ExtendedHealthCheckExecutor(org.apache.sling.hc.core.impl.executor.ExtendedHealthCheckExecutor) Date(java.util.Date) ServiceReference(org.osgi.framework.ServiceReference) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ObjectName(javax.management.ObjectName) HealthCheckSelector(org.apache.sling.hc.api.execution.HealthCheckSelector) HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) List(java.util.List) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 12 with HealthCheckMetadata

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

the class HealthCheckResultCacheTest method testHealthCheckResultCache.

@Test
public void testHealthCheckResultCache() {
    HealthCheckMetadata hc1 = setupHealthCheckMetadata(1, HC_TIMEOUT_NOT_SET);
    ExecutionResult executionResult1 = spy(new ExecutionResult(hc1, new Result(Result.Status.OK, "result for hc1"), 1));
    doReturn(new Date(new Date().getTime() - DUR_1_MIN)).when(executionResult1).getFinishedAt();
    healthCheckResultCache.updateWith(executionResult1);
    HealthCheckMetadata hc2 = setupHealthCheckMetadata(2, HC_TIMEOUT_NOT_SET);
    ExecutionResult executionResult2 = spy(new ExecutionResult(hc2, new Result(Result.Status.OK, "result for hc2"), 1));
    doReturn(new Date(new Date().getTime() - DUR_3_MIN)).when(executionResult2).getFinishedAt();
    healthCheckResultCache.updateWith(executionResult2);
    HealthCheckMetadata hc3 = setupHealthCheckMetadata(3, DUR_4_MIN);
    ExecutionResult executionResult3 = spy(new ExecutionResult(hc3, new Result(Result.Status.OK, "result for hc3"), 1));
    doReturn(new Date(new Date().getTime() - DUR_3_MIN)).when(executionResult3).getFinishedAt();
    healthCheckResultCache.updateWith(executionResult3);
    HealthCheckMetadata hc4 = setupHealthCheckMetadata(4, HC_TIMEOUT_NOT_SET);
    // no result for this yet
    List<HealthCheckMetadata> hcList = new ArrayList<HealthCheckMetadata>(Arrays.asList(hc1, hc2, hc3, hc4));
    List<HealthCheckExecutionResult> results = new ArrayList<HealthCheckExecutionResult>();
    healthCheckResultCache.useValidCacheResults(hcList, results, DUR_2_MIN);
    // result too old, left in hcList for later execution
    assertTrue(hcList.contains(hc2));
    // no result was added to cache via updateWith()
    assertTrue(hcList.contains(hc4));
    // true <= result one min old, global timeout 2min
    assertTrue(results.contains(executionResult1));
    // false <= result three min old, global timeout 2min
    assertFalse(results.contains(executionResult2));
    // true <= result one three old, HC timeout 4min
    assertTrue(results.contains(executionResult3));
    // values not found in cache are left in hcList
    assertEquals(2, hcList.size());
    assertEquals(2, results.size());
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) ArrayList(java.util.ArrayList) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Date(java.util.Date) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 13 with HealthCheckMetadata

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

the class HealthCheckResultCacheTest method testHealthCheckResultCacheTtl.

@Test
public void testHealthCheckResultCacheTtl() {
    //-- test cache miss due to HC TTL
    HealthCheckMetadata hcWithTtl = setupHealthCheckMetadata(1, DUR_1_MIN);
    ExecutionResult executionResult = spy(new ExecutionResult(hcWithTtl, new Result(Result.Status.OK, "result for hc"), 1));
    doReturn(new Date(new Date().getTime() - DUR_2_MIN)).when(executionResult).getFinishedAt();
    healthCheckResultCache.updateWith(executionResult);
    HealthCheckExecutionResult result = healthCheckResultCache.getValidCacheResult(hcWithTtl, DUR_3_MIN);
    // even though global timeout would be ok (2min<3min, the hc timeout of 1min invalidates the result)
    assertNull(result);
    //-- test cache hit due to HC TTL
    hcWithTtl = setupHealthCheckMetadata(2, DUR_3_MIN);
    executionResult = spy(new ExecutionResult(hcWithTtl, new Result(Result.Status.OK, "result for hc"), 1));
    doReturn(new Date(new Date().getTime() - DUR_2_MIN)).when(executionResult).getFinishedAt();
    healthCheckResultCache.updateWith(executionResult);
    result = healthCheckResultCache.getValidCacheResult(hcWithTtl, DUR_1_MIN);
    // even though global timeout would invalidate this result (1min<2min, the hc timeout of 3min allows the result)
    assertEquals(executionResult, result);
    //-- test Long.MAX_VALUE
    hcWithTtl = setupHealthCheckMetadata(3, Long.MAX_VALUE);
    executionResult = spy(new ExecutionResult(hcWithTtl, new Result(Result.Status.OK, "result for hc"), 1));
    doReturn(new Date(new Date().getTime() - DUR_4_MIN)).when(executionResult).getFinishedAt();
    healthCheckResultCache.updateWith(executionResult);
    result = healthCheckResultCache.getValidCacheResult(hcWithTtl, DUR_1_MIN);
    assertEquals(executionResult, result);
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Date(java.util.Date) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 14 with HealthCheckMetadata

use of org.apache.sling.hc.util.HealthCheckMetadata in project acs-aem-commons by Adobe-Consulting-Services.

the class HealthCheckStatusEmailerTest method setUp.

@Before
public void setUp() throws Exception {
    config = new HashMap<String, Object>();
    config.put(HealthCheckStatusEmailer.PROP_RECIPIENTS_EMAIL_ADDRESSES, "test@example.com");
    // Success
    HealthCheckMetadata successMetadata = mock(HealthCheckMetadata.class);
    Result successResult = mock(Result.class);
    when(successMetadata.getTitle()).thenReturn("hc success");
    when(successResult.isOk()).thenReturn(true);
    when(successResult.getStatus()).thenReturn(Result.Status.OK);
    when(successExecutionResult.getHealthCheckMetadata()).thenReturn(successMetadata);
    when(successExecutionResult.getHealthCheckResult()).thenReturn(successResult);
    // Failure
    HealthCheckMetadata failureMetadata = mock(HealthCheckMetadata.class);
    Result failureResult = mock(Result.class);
    when(failureMetadata.getTitle()).thenReturn("hc failure");
    when(failureResult.isOk()).thenReturn(false);
    when(failureResult.getStatus()).thenReturn(Result.Status.CRITICAL);
    when(failureExecutionResult.getHealthCheckMetadata()).thenReturn(failureMetadata);
    when(failureExecutionResult.getHealthCheckResult()).thenReturn(failureResult);
    results = new ArrayList<>();
    when(healthCheckExecutor.execute(any(HealthCheckExecutionOptions.class), any(String[].class))).thenReturn(results);
    when(productInfoService.getInfos()).thenReturn(new ProductInfo[] { mock(ProductInfo.class) });
    Set<String> runModes = new HashSet<String>();
    runModes.add("author");
    when(slingSettingsService.getRunModes()).thenReturn(runModes);
}
Also used : ProductInfo(com.adobe.granite.license.ProductInfo) HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)14 HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)10 Result (org.apache.sling.hc.api.Result)8 ServiceReference (org.osgi.framework.ServiceReference)5 Date (java.util.Date)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)2 ProductInfo (com.adobe.granite.license.ProductInfo)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 List (java.util.List)1 ObjectName (javax.management.ObjectName)1 StopWatch (org.apache.commons.lang.time.StopWatch)1 Activate (org.apache.felix.scr.annotations.Activate)1 Deactivate (org.apache.felix.scr.annotations.Deactivate)1 HealthCheck (org.apache.sling.hc.api.HealthCheck)1