james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [18/21] james-project git commit: JAMES-2047 Add error management to ConcurrentTestRunner
Date Wed, 07 Jun 2017 10:46:28 GMT
JAMES-2047 Add error management to ConcurrentTestRunner


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/04566013
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/04566013
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/04566013

Branch: refs/heads/master
Commit: 045660137ff53a360f8d91fe9b155332342dac8d
Parents: 2e1d41d
Author: benwa <btellier@linagora.com>
Authored: Tue Jun 6 11:19:32 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Wed Jun 7 17:39:06 2017 +0700

----------------------------------------------------------------------
 .../util/concurrency/ConcurrentTestRunner.java  | 22 ++++-
 .../concurrency/ConcurrentTestRunnerTest.java   | 98 ++++++++++++++++++++
 2 files changed, 119 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/04566013/server/container/util/src/main/java/org/apache/james/util/concurrency/ConcurrentTestRunner.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/main/java/org/apache/james/util/concurrency/ConcurrentTestRunner.java
b/server/container/util/src/main/java/org/apache/james/util/concurrency/ConcurrentTestRunner.java
index 0a4fa40..cc06686 100644
--- a/server/container/util/src/main/java/org/apache/james/util/concurrency/ConcurrentTestRunner.java
+++ b/server/container/util/src/main/java/org/apache/james/util/concurrency/ConcurrentTestRunner.java
@@ -19,15 +19,20 @@
 
 package org.apache.james.util.concurrency;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Preconditions;
+import com.google.common.base.Throwables;
 
 public class ConcurrentTestRunner {
 
@@ -40,6 +45,7 @@ public class ConcurrentTestRunner {
     private class ConcurrentRunnableTask implements Runnable {
         private final int threadNumber;
         private final BiConsumer biConsumer;
+        private Exception exception;
 
         public ConcurrentRunnableTask(int threadNumber, BiConsumer biConsumer) {
             this.threadNumber = threadNumber;
@@ -48,14 +54,19 @@ public class ConcurrentTestRunner {
 
         @Override
         public void run() {
+            exception = null;
             countDownLatch.countDown();
             for (int i = 0; i < operationCount; i++) {
                 try {
                     biConsumer.consume(threadNumber, i);
                 } catch (Exception e) {
                     LOGGER.error("Error caught during concurrent testing", e);
+                    exception = e;
                 }
             }
+            if (exception != null) {
+                throw Throwables.propagate(exception);
+            }
         }
     }
 
@@ -64,6 +75,7 @@ public class ConcurrentTestRunner {
     private final CountDownLatch countDownLatch;
     private final BiConsumer biConsumer;
     private final ExecutorService executorService;
+    private final List<Future> futures;
 
     public ConcurrentTestRunner(int threadCount, int operationCount, BiConsumer biConsumer)
{
         Preconditions.checkArgument(threadCount > 0, "Thread count should be strictly
positive");
@@ -74,11 +86,19 @@ public class ConcurrentTestRunner {
         this.countDownLatch = new CountDownLatch(threadCount);
         this.biConsumer = biConsumer;
         this.executorService = Executors.newFixedThreadPool(threadCount);
+        this.futures = new ArrayList<Future>();
     }
 
     public ConcurrentTestRunner run() {
         for (int i = 0; i < threadCount; i++) {
-            executorService.submit(new ConcurrentRunnableTask(i, biConsumer));
+            futures.add(executorService.submit(new ConcurrentRunnableTask(i, biConsumer)));
+        }
+        return this;
+    }
+
+    public ConcurrentTestRunner assertNoException() throws ExecutionException, InterruptedException
{
+        for (Future future: futures) {
+            future.get();
         }
         return this;
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/04566013/server/container/util/src/test/java/org/apache/james/util/concurrency/ConcurrentTestRunnerTest.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/test/java/org/apache/james/util/concurrency/ConcurrentTestRunnerTest.java
b/server/container/util/src/test/java/org/apache/james/util/concurrency/ConcurrentTestRunnerTest.java
index 98bde34..89396e0 100644
--- a/server/container/util/src/test/java/org/apache/james/util/concurrency/ConcurrentTestRunnerTest.java
+++ b/server/container/util/src/test/java/org/apache/james/util/concurrency/ConcurrentTestRunnerTest.java
@@ -22,6 +22,7 @@ package org.apache.james.util.concurrency;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 
 import org.junit.Rule;
@@ -132,4 +133,101 @@ public class ConcurrentTestRunnerTest {
         assertThat(concurrentTestRunner.awaitTermination(DEFAULT_AWAIT_TIME, TimeUnit.MILLISECONDS)).isTrue();
         assertThat(queue).containsOnly("0:0", "0:1", "1:0", "1:1");
     }
+
+    @Test
+    public void runShouldNotThrowOnExceptions() throws Exception {
+        int operationCount = 2;
+        int threadCount = 2;
+
+        ConcurrentTestRunner concurrentTestRunner = new ConcurrentTestRunner(threadCount,
operationCount,
+            new ConcurrentTestRunner.BiConsumer() {
+                @Override
+                public void consume(int threadNumber, int step) throws Exception {
+                    throw new RuntimeException();
+                }
+            })
+            .run();
+
+        assertThat(concurrentTestRunner.awaitTermination(DEFAULT_AWAIT_TIME, TimeUnit.MILLISECONDS)).isTrue();
+    }
+
+    @Test
+    public void noExceptionsShouldNotThrowWhenNoExceptionGenerated() throws Exception {
+        int operationCount = 2;
+        int threadCount = 2;
+
+        ConcurrentTestRunner concurrentTestRunner = new ConcurrentTestRunner(threadCount,
operationCount,
+            new ConcurrentTestRunner.BiConsumer() {
+                @Override
+                public void consume(int threadNumber, int step) throws Exception {
+
+                }
+            })
+            .run();
+
+        concurrentTestRunner.awaitTermination(DEFAULT_AWAIT_TIME, TimeUnit.MILLISECONDS);
+
+        concurrentTestRunner.assertNoException();
+    }
+
+    @Test
+    public void assertNoExceptionShouldThrowOnExceptions() throws Exception {
+        int operationCount = 2;
+        int threadCount = 2;
+
+        ConcurrentTestRunner concurrentTestRunner = new ConcurrentTestRunner(threadCount,
operationCount,
+            new ConcurrentTestRunner.BiConsumer() {
+                @Override
+                public void consume(int threadNumber, int step) throws Exception {
+                    throw new RuntimeException();
+                }
+            })
+            .run();
+        concurrentTestRunner.awaitTermination(DEFAULT_AWAIT_TIME, TimeUnit.MILLISECONDS);
+
+        expectedException.expect(ExecutionException.class);
+        concurrentTestRunner.assertNoException();
+    }
+
+    @Test
+    public void runShouldPerformAllOperationsEvenOnExceptions() throws Exception {
+        int operationCount = 2;
+        int threadCount = 2;
+        final ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
+
+        ConcurrentTestRunner concurrentTestRunner = new ConcurrentTestRunner(threadCount,
operationCount,
+            new ConcurrentTestRunner.BiConsumer() {
+                @Override
+                public void consume(int threadNumber, int step) throws Exception {
+                    queue.add(threadNumber + ":" + step);
+                    throw new RuntimeException();
+                }
+            })
+            .run();
+
+        assertThat(concurrentTestRunner.awaitTermination(DEFAULT_AWAIT_TIME, TimeUnit.MILLISECONDS)).isTrue();
+        assertThat(queue).containsOnly("0:0", "0:1", "1:0", "1:1");
+    }
+
+    @Test
+    public void runShouldPerformAllOperationsEvenOnOccasionalExceptions() throws Exception
{
+        int operationCount = 2;
+        int threadCount = 2;
+        final ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
+
+        ConcurrentTestRunner concurrentTestRunner = new ConcurrentTestRunner(threadCount,
operationCount,
+            new ConcurrentTestRunner.BiConsumer() {
+                @Override
+                public void consume(int threadNumber, int step) throws Exception {
+                    queue.add(threadNumber + ":" + step);
+                    if ((threadNumber + step) % 2 == 0) {
+                        throw new RuntimeException();
+                    }
+                }
+            })
+            .run();
+
+        assertThat(concurrentTestRunner.awaitTermination(DEFAULT_AWAIT_TIME, TimeUnit.MILLISECONDS)).isTrue();
+        assertThat(queue).containsOnly("0:0", "0:1", "1:0", "1:1");
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message