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);
}
}
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());
}
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);
}
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);
}
Aggregations