SLING-9633 Testing Clients Polling does not return all exceptions encountered while polling
diff --git a/src/main/java/org/apache/sling/testing/clients/util/poller/Polling.java b/src/main/java/org/apache/sling/testing/clients/util/poller/Polling.java
index 6de95ef..d6f0f05 100644
--- a/src/main/java/org/apache/sling/testing/clients/util/poller/Polling.java
+++ b/src/main/java/org/apache/sling/testing/clients/util/poller/Polling.java
@@ -15,9 +15,9 @@
* the License.
*/
package org.apache.sling.testing.clients.util.poller;
-
import org.apache.sling.testing.timeouts.TimeoutsProvider;
-
+import java.util.ArrayList;
+import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeoutException;
@@ -41,6 +41,11 @@
protected Exception lastException;
/**
+ * List of all the exceptions thrown by call(), to be used for logging
+ */
+ protected List<Exception> exceptions;
+
+ /**
* Counter for total waiting time
*/
protected long waited;
@@ -52,9 +57,7 @@
* will be equivalent to {@code Thread.sleep(timeout)}
*/
public Polling() {
- this.c = null;
- this.lastException = null;
- this.waited = 0;
+ this(null);
}
/**
@@ -64,7 +67,8 @@
*/
public Polling(Callable<Boolean> c) {
this.c = c;
- this.lastException = null;
+ lastException = null;
+ this.exceptions = new ArrayList<>();
this.waited = 0;
}
@@ -118,6 +122,7 @@
} catch (InterruptedException e) {
throw e; // Never inhibit InterruptedException
} catch (Exception e) {
+ exceptions.add(e);
lastException = e;
}
Thread.sleep(delay);
@@ -125,7 +130,7 @@
waited = System.currentTimeMillis() - start;
throw new TimeoutException(String.format(message(), effectiveTimeout, delay) +
- " Last exception was: " + lastException);
+ " Last exception was: " + this.getLastException());
}
public long getWaited() {
@@ -150,4 +155,12 @@
public Exception getLastException() {
return lastException;
}
+
+ /**
+ * Return the list of all exceptions while polling
+ * @return
+ */
+ public List<Exception> getExceptions() {
+ return exceptions;
+ }
}
diff --git a/src/main/java/org/apache/sling/testing/clients/util/poller/package-info.java b/src/main/java/org/apache/sling/testing/clients/util/poller/package-info.java
index c7a81fa..4c22cac 100644
--- a/src/main/java/org/apache/sling/testing/clients/util/poller/package-info.java
+++ b/src/main/java/org/apache/sling/testing/clients/util/poller/package-info.java
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("1.3.0")
+@Version("1.4.0")
package org.apache.sling.testing.clients.util.poller;
import org.osgi.annotation.versioning.Version;
diff --git a/src/test/java/org/apache/sling/testing/clients/util/poller/PollingTest.java b/src/test/java/org/apache/sling/testing/clients/util/poller/PollingTest.java
index adf75f1..bcb5f6d 100644
--- a/src/test/java/org/apache/sling/testing/clients/util/poller/PollingTest.java
+++ b/src/test/java/org/apache/sling/testing/clients/util/poller/PollingTest.java
@@ -21,9 +21,8 @@
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.TimeoutException;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+
+import static org.junit.Assert.*;
public class PollingTest {
@Test
@@ -60,6 +59,28 @@
}
@Test
+ public void testGetExceptionsFromThreeCalls() throws Exception {
+ final MutableInt callCount = new MutableInt(0);
+ Polling p = new Polling() {
+ @Override
+ public Boolean call() throws Exception {
+ callCount.increment();
+ if (callCount.getValue() < 3) {
+ throw new Exception(callCount.getValue().toString());
+ }
+ return true;
+ }
+ };
+ p.poll(500, 10);
+ assertEquals(3, callCount.intValue());
+ assertTrue("Exceptions list should not be null", p.getExceptions() != null);
+ assertEquals("Wrong number of exceptions ", 2, p.getExceptions().size());
+ assertEquals("Wrong message for first exception ", "1", p.getExceptions().get(0).getMessage());
+ assertEquals("Wrong message for second exception ", "2", p.getExceptions().get(1).getMessage());
+ assertEquals("Wrong message for the last exception", "2", p.getLastException().getMessage());
+ }
+
+ @Test
public void testCallTimeout() throws Exception {
final MutableInt callCount = new MutableInt(0);
Polling p = new Polling() {