sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1732845 - in /sling/trunk/bundles/extensions/healthcheck/core/src: main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java test/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImplTest.java
Date Mon, 29 Feb 2016 10:10:05 GMT
Author: bdelacretaz
Date: Mon Feb 29 10:10:05 2016
New Revision: 1732845

URL: http://svn.apache.org/viewvc?rev=1732845&view=rev
Log:
SLING-5415 - additional tests for HealthCheckExecutorImpl, contributed by Georg Henzler, thanks!

Modified:
    sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java
    sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImplTest.java

Modified: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java?rev=1732845&r1=1732844&r2=1732845&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java
(original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImpl.java
Mon Feb 29 10:10:05 2016
@@ -103,7 +103,7 @@ public class HealthCheckExecutorImpl imp
 
     private long resultCacheTtlInMs;
 
-    private final HealthCheckResultCache healthCheckResultCache = new HealthCheckResultCache();
+    private HealthCheckResultCache healthCheckResultCache = new HealthCheckResultCache();
 
     private final Map<HealthCheckMetadata, HealthCheckFuture> stillRunningFutures =
new HashMap<HealthCheckMetadata, HealthCheckFuture>();
 

Modified: sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImplTest.java?rev=1732845&r1=1732844&r2=1732845&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImplTest.java
(original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/executor/HealthCheckExecutorImplTest.java
Mon Feb 29 10:10:05 2016
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertTru
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
 
 import java.util.Collection;
 import java.util.Date;
@@ -31,37 +32,46 @@ import java.util.Set;
 import java.util.TreeSet;
 
 import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.Result.Status;
+import org.apache.sling.hc.api.ResultLog.Entry;
 import org.apache.sling.hc.api.execution.HealthCheckExecutionResult;
 import org.apache.sling.hc.util.HealthCheckMetadata;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.InjectMocks;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.Spy;
 
 public class HealthCheckExecutorImplTest {
 
-    private HealthCheckExecutorImpl healthCheckExecutorImpl;
+	@InjectMocks
+    private HealthCheckExecutorImpl healthCheckExecutorImpl = new HealthCheckExecutorImpl();;
 
     @Mock
     private HealthCheckFuture future;
 
     @Mock
     private HealthCheckMetadata HealthCheckMetadata;
+    
+    @Spy
+    private HealthCheckResultCache healthCheckResultCache = new HealthCheckResultCache();
 
     @Before
     public void setup() {
-        MockitoAnnotations.initMocks(this);
+        initMocks(this);
 
         when(future.getHealthCheckMetadata()).thenReturn(HealthCheckMetadata);
         when(HealthCheckMetadata.getTitle()).thenReturn("Test Check");
 
-        healthCheckExecutorImpl = new HealthCheckExecutorImpl();
+        
         // 2 sec normal timeout
         healthCheckExecutorImpl.setTimeoutInMs(2000L);
         // 10 sec timeout for critical
         healthCheckExecutorImpl.setLongRunningFutureThresholdForRedMs(10000L);
+
     }
 
+
     @Test
     public void testCollectResultsFromFutures() throws Exception {
 
@@ -85,13 +95,16 @@ public class HealthCheckExecutorImplTest
     @Test
     public void testCollectResultsFromFuturesTimeout() throws Exception {
 
+    	// add an earlier result with status ok (that will be shown as part of the log)
+    	addResultToCache(Status.OK);
+    	
         List<HealthCheckFuture> futures = new LinkedList<HealthCheckFuture>();
         futures.add(future);
         Set<HealthCheckExecutionResult> results = new TreeSet<HealthCheckExecutionResult>();
 
         when(future.isDone()).thenReturn(false);
-        when(future.getCreatedTime()).thenReturn(new Date());
-
+        // simulating a future that was created 5sec ago
+        when(future.getCreatedTime()).thenReturn(new Date(new Date().getTime() - 1000 * 5));
 
         healthCheckExecutorImpl.collectResultsFromFutures(futures, results);
 
@@ -101,6 +114,9 @@ public class HealthCheckExecutorImplTest
         HealthCheckExecutionResult result = results.iterator().next();
 
         assertEquals(Result.Status.WARN, result.getHealthCheckResult().getStatus());
+        
+        // 3 because previous result exists and is part of log
+        assertEquals(3, getLogEntryCount(result));
 
     }
 
@@ -113,7 +129,7 @@ public class HealthCheckExecutorImplTest
 
         when(future.isDone()).thenReturn(false);
 
-        // use an old date now (simulating a future that has run for a min)
+        // use an old date now (simulating a future that has run for an hour)
         when(future.getCreatedTime()).thenReturn(new Date(new Date().getTime() - 1000 * 60
* 60));
 
         healthCheckExecutorImpl.collectResultsFromFutures(futures, results);
@@ -123,7 +139,47 @@ public class HealthCheckExecutorImplTest
         verify(future, times(0)).get();
 
         assertEquals(Result.Status.CRITICAL, result.getHealthCheckResult().getStatus());
-
+        assertEquals(1, getLogEntryCount(result));
     }
 
+    @Test
+    public void testCollectResultsFromFuturesWarnTimeoutWithPreviousCritical() throws Exception
{
+
+        // an earlier result with critical
+    	addResultToCache(Status.CRITICAL);
+    	
+        List<HealthCheckFuture> futures = new LinkedList<HealthCheckFuture>();
+        futures.add(future);
+        Set<HealthCheckExecutionResult> results = new TreeSet<HealthCheckExecutionResult>();
+
+        when(future.isDone()).thenReturn(false);
+        // simulating a future that was created 5sec ago
+        when(future.getCreatedTime()).thenReturn(new Date(new Date().getTime() - 1000 * 5));
+
+
+        healthCheckExecutorImpl.collectResultsFromFutures(futures, results);
+        assertEquals(1, results.size());
+        HealthCheckExecutionResult result = results.iterator().next();
+
+        verify(future, times(0)).get();
+
+        // expect CRITICAL because previous result (before timeout) was CRITICAL (and not
only WARN)
+        assertEquals(Result.Status.CRITICAL, result.getHealthCheckResult().getStatus());
+        assertEquals(3, getLogEntryCount(result));
+    }
+    
+    
+	private int getLogEntryCount(HealthCheckExecutionResult result) {
+		int logEntryCount = 0;
+        for (Entry entry : result.getHealthCheckResult()) {
+        	logEntryCount++;
+		}
+        return logEntryCount;
+	}
+	
+
+	private void addResultToCache(Status status) {
+		healthCheckResultCache.updateWith(new ExecutionResult(HealthCheckMetadata, new Result(status,
"Status "+status), 1000));
+	}
+    
 }



Mime
View raw message