This is an automated email from the ASF dual-hosted git repository.

dulvac pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-clients.git


The following commit(s) were added to refs/heads/master by this push:
     new 53b7e2f  SLING-9633 Testing Clients Polling does not return all 
exceptions encountered while polling
53b7e2f is described below

commit 53b7e2fdea1b4eb351fe1e729eeef481d74b473b
Author: Andrei Dulvac <[email protected]>
AuthorDate: Wed Aug 5 17:02:06 2020 +0200

    SLING-9633 Testing Clients Polling does not return all exceptions 
encountered while polling
---
 .../sling/testing/clients/util/poller/Polling.java | 27 ++++++++++++++++------
 .../testing/clients/util/poller/package-info.java  |  2 +-
 .../testing/clients/util/poller/PollingTest.java   | 27 +++++++++++++++++++---
 3 files changed, 45 insertions(+), 11 deletions(-)

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 @@ public class Polling implements Callable<Boolean> {
     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 @@ public class Polling implements Callable<Boolean> {
      * 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 class Polling implements Callable<Boolean> {
      */
     public Polling(Callable<Boolean> c) {
         this.c = c;
-        this.lastException = null;
+        lastException = null;
+        this.exceptions = new ArrayList<>();
         this.waited = 0;
     }
 
@@ -118,6 +122,7 @@ public class Polling implements Callable<Boolean> {
             } catch (InterruptedException e) {
                 throw e; // Never inhibit InterruptedException
             } catch (Exception e) {
+                exceptions.add(e);
                 lastException = e;
             }
             Thread.sleep(delay);
@@ -125,7 +130,7 @@ public class Polling implements Callable<Boolean> {
 
         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 class Polling implements Callable<Boolean> {
     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.apache.commons.lang3.mutable.MutableInt;
 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 @@ public class PollingTest {
     }
 
     @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() {

Reply via email to