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

cschneider pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-journal.git

commit b7c5e249a79a6f78954a24c9820e4c9efa663171
Author: Christian Schneider <cschn...@adobe.com>
AuthorDate: Wed Jan 15 10:49:15 2020 +0100

    SLING-8997 - Make sure shutdown of ExponentialBackOff happens quickly
---
 .../journal/impl/shared/ExponentialBackOff.java            |  9 +++++++++
 .../journal/impl/shared/ExponentialBackoffTest.java        | 14 +++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/apache/sling/distribution/journal/impl/shared/ExponentialBackOff.java
 
b/src/main/java/org/apache/sling/distribution/journal/impl/shared/ExponentialBackOff.java
index ea7cc28..1338ef4 100644
--- 
a/src/main/java/org/apache/sling/distribution/journal/impl/shared/ExponentialBackOff.java
+++ 
b/src/main/java/org/apache/sling/distribution/journal/impl/shared/ExponentialBackOff.java
@@ -25,6 +25,7 @@ import java.time.Duration;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.slf4j.Logger;
@@ -63,7 +64,15 @@ public class ExponentialBackOff implements Closeable {
 
     @Override
     public void close() {
+        log.info("Shutting down exponential backoff executor");
         this.executor.shutdown();
+        this.executor.shutdownNow();
+        try {
+            this.executor.awaitTermination(100, TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+        }
+        log.info("Shutdown completed");
     }
     
     public void startChecks() {
diff --git 
a/src/test/java/org/apache/sling/distribution/journal/impl/shared/ExponentialBackoffTest.java
 
b/src/test/java/org/apache/sling/distribution/journal/impl/shared/ExponentialBackoffTest.java
index a69aed6..55e4c01 100644
--- 
a/src/test/java/org/apache/sling/distribution/journal/impl/shared/ExponentialBackoffTest.java
+++ 
b/src/test/java/org/apache/sling/distribution/journal/impl/shared/ExponentialBackoffTest.java
@@ -23,6 +23,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
 import static org.junit.Assert.assertThat;
 
 import java.time.Duration;
+import java.time.temporal.ChronoUnit;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -37,6 +38,7 @@ public class ExponentialBackoffTest {
     private static final int RETRIES = 5;
     private static final Duration INITIAL_DELAY = Duration.of(64, MILLIS);
     private static final Duration MAX_DELAY = Duration.of(256, MILLIS);
+    private static final Duration LONG_DELAY = Duration.of(5, 
ChronoUnit.SECONDS);
     
     private Logger log = LoggerFactory.getLogger(this.getClass());
     
@@ -68,7 +70,17 @@ public class ExponentialBackoffTest {
         assertThat("Should finish quickly as we called startChecks after 
enough delay", finished3, equalTo(true));
 
         backOff.close();
-        
+    }
+    
+    /**
+     * Even with a scheduled check the shutdown is expected to be 
+     * fast and to clean up its thread
+     */
+    @Test(timeout = 500)
+    public void testShutdown() throws Exception {
+        ExponentialBackOff backoff = new ExponentialBackOff(LONG_DELAY, 
LONG_DELAY, false, this::checkCallback);
+        backoff.startChecks();
+        backoff.close(); // We expect this to finish in less than the test 
timeout
     }
     
     private void checkCallback() {

Reply via email to