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

commit d742145cf07315055dfeb6e9b039e725372a8bf4
Author: Andrei Dulvac <adul...@adobe.com>
AuthorDate: Fri Feb 14 11:36:46 2020 +0100

    SLING-9069 Polling format exception in case of TimeoutException
---
 .../sling/testing/clients/util/poller/Polling.java | 13 ++++-
 .../testing/clients/util/poller/PollingTest.java   | 57 +++++++++++-----------
 2 files changed, 39 insertions(+), 31 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 60296de..6de95ef 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
@@ -124,7 +124,8 @@ public class Polling implements Callable<Boolean> {
         } while (System.currentTimeMillis() < start + effectiveTimeout);
 
         waited = System.currentTimeMillis() - start;
-        throw new TimeoutException(String.format(message(), effectiveTimeout, 
delay));
+        throw new TimeoutException(String.format(message(), effectiveTimeout, 
delay) +
+                " Last exception was: " + lastException);
     }
 
     public long getWaited() {
@@ -139,6 +140,14 @@ public class Polling implements Callable<Boolean> {
      * @return the format string
      */
     protected String message() {
-        return "Call failed to return true in %1$d ms. Last exception was: " + 
lastException;
+        return "Call failed to return true in %1$d ms.";
+    }
+
+    /**
+     * Return the last exception while polling or {null}
+     * @return
+     */
+    public Exception getLastException() {
+        return lastException;
     }
 }
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 15994ed..adf75f1 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
@@ -18,11 +18,9 @@ package org.apache.sling.testing.clients.util.poller;
 
 import org.apache.commons.lang3.mutable.MutableBoolean;
 import org.apache.commons.lang3.mutable.MutableInt;
+import org.junit.Assert;
 import org.junit.Test;
-
-import java.util.concurrent.Callable;
 import java.util.concurrent.TimeoutException;
-
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -106,12 +104,9 @@ public class PollingTest {
     public void testCallableOnce() throws Exception {
         final MutableInt callCount = new MutableInt(0);
         final MutableBoolean called = new MutableBoolean(false);
-        Polling p = new Polling(new Callable<Boolean>() {
-            @Override
-            public Boolean call() throws Exception {
-                callCount.increment();
-                return true;
-            }
+        Polling p = new Polling(() -> {
+            callCount.increment();
+            return true;
         });
         p.poll(500, 10);
 
@@ -122,14 +117,11 @@ public class PollingTest {
     public void testCallableTwice() throws Exception {
         final MutableInt callCount = new MutableInt(0);
         final MutableBoolean called = new MutableBoolean(false);
-        Polling p = new Polling(new Callable<Boolean>() {
-            @Override
-            public Boolean call() throws Exception {
-                callCount.increment();
-                boolean b = called.booleanValue();
-                called.setTrue();
-                return b;
-            }
+        Polling p = new Polling(() -> {
+            callCount.increment();
+            boolean b = called.booleanValue();
+            called.setTrue();
+            return b;
         });
         p.poll(500, 10);
 
@@ -139,12 +131,9 @@ public class PollingTest {
     @Test
     public void testCallableTimeout() throws Exception {
         final MutableInt callCount = new MutableInt(0);
-        Polling p = new Polling(new Callable<Boolean>() {
-            @Override
-            public Boolean call() throws Exception {
-                callCount.increment();
-                return false;
-            }
+        Polling p = new Polling(() -> {
+            callCount.increment();
+            return false;
         });
 
         try {
@@ -161,12 +150,7 @@ public class PollingTest {
 
     @Test
     public void testCallPriority() throws Exception {
-        Polling p = new Polling(new Callable<Boolean>() {
-            @Override
-            public Boolean call() throws Exception {
-                return false;
-            }
-        }) {
+        Polling p = new Polling(() -> false) {
             @Override
             public Boolean call() throws Exception {
                 return true;
@@ -176,4 +160,19 @@ public class PollingTest {
         // Should not reach timeout since overridden call() has priority over 
Callable param
         p.poll(100, 10);
     }
+
+    @Test
+    public void testCallThrowException() throws Exception {
+        Polling p = new Polling(() -> {
+          throw new RuntimeException("%Failure");
+        });
+        try {
+            p.poll(100, 10);
+        } catch (TimeoutException e) {
+            Assert.assertTrue("Timeout message should contain original 
message", e.getMessage().contains("%Failure"));
+        }
+
+    }
+
+
 }
\ No newline at end of file

Reply via email to