You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ad...@apache.org on 2017/01/18 07:51:59 UTC

[5/8] james-project git commit: MAILBOX-285 Factorize concurrency testing

MAILBOX-285 Factorize concurrency testing


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

Branch: refs/heads/master
Commit: 04b15802a35bc6710ae952246f74a32f6068923a
Parents: 1bd6c7c
Author: Benoit Tellier <bt...@linagora.com>
Authored: Thu Jan 12 10:18:42 2017 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Tue Jan 17 10:44:41 2017 +0700

----------------------------------------------------------------------
 .../store/mail/model/MessageIdMapperTest.java   | 51 +++----------
 .../store/mail/model/MessageMapperTest.java     | 53 +++-----------
 .../model/concurrency/ConcurrentTestRunner.java | 77 ++++++++++++++++++++
 3 files changed, 96 insertions(+), 85 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/04b15802/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageIdMapperTest.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageIdMapperTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageIdMapperTest.java
index 8983274..7dfa488 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageIdMapperTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageIdMapperTest.java
@@ -24,10 +24,6 @@ import static org.assertj.core.api.Assertions.assertThat;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
 
 import javax.mail.Flags;
 import javax.mail.Flags.Flag;
@@ -46,6 +42,7 @@ import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.MessageIdMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
+import org.apache.james.mailbox.store.mail.model.concurrency.ConcurrentTestRunner;
 import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
@@ -63,35 +60,6 @@ import com.google.common.collect.ImmutableList;
 @Contract(MapperProvider.class)
 public class MessageIdMapperTest<T extends MapperProvider> {
 
-    private class ConcurentSetFlagTestRunnable implements Runnable {
-        private final int threadNumber;
-        private final int updateCount;
-        private final MailboxMessage mailboxMessage;
-        private final CountDownLatch countDownLatch;
-
-        public ConcurentSetFlagTestRunnable(int threadNumber, int updateCount, MailboxMessage mailboxMessage, CountDownLatch countDownLatch) {
-            this.threadNumber = threadNumber;
-            this.updateCount = updateCount;
-            this.mailboxMessage = mailboxMessage;
-            this.countDownLatch = countDownLatch;
-        }
-
-        @Override
-        public void run() {
-            countDownLatch.countDown();
-            for (int i = 0; i < updateCount; i++) {
-                try {
-                    sut.setFlags(mailboxMessage.getMessageId(),
-                        ImmutableList.of(mailboxMessage.getMailboxId()),
-                        new Flags("custom-" + threadNumber + "-" + i),
-                        FlagsUpdateMode.ADD);
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            }
-        }
-    }
-
     @Rule
     public ExpectedException expectedException = ExpectedException.none();
 
@@ -635,14 +603,15 @@ public class MessageIdMapperTest<T extends MapperProvider> {
 
         int threadCount = 2;
         int updateCount = 10;
-        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
-        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
-        for (int i = 0; i < threadCount; i++) {
-            executorService.submit(new ConcurentSetFlagTestRunnable(i, updateCount, message1, countDownLatch));
-        }
-        executorService.shutdown();
-        assertThat(executorService.awaitTermination(1, TimeUnit.MINUTES))
-            .isTrue();
+        new ConcurrentTestRunner(threadCount, updateCount) {
+            @Override
+            protected void performOperation(int threadNumber, int step) throws Exception {
+                sut.setFlags(message1.getMessageId(),
+                    ImmutableList.of(message1.getMailboxId()),
+                    new Flags("custom-" + threadNumber + "-" + step),
+                    FlagsUpdateMode.ADD);
+            }
+        }.run();
 
         List<MailboxMessage> messages = sut.find(ImmutableList.of(message1.getMessageId()), MessageMapper.FetchType.Body);
         assertThat(messages).hasSize(1);

http://git-wip-us.apache.org/repos/asf/james-project/blob/04b15802/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageMapperTest.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageMapperTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageMapperTest.java
index a4e6053..6185b6d 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageMapperTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MessageMapperTest.java
@@ -28,10 +28,6 @@ import java.io.IOException;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
 
 import javax.mail.Flags;
 import javax.mail.util.SharedByteArrayInputStream;
@@ -49,6 +45,7 @@ import org.apache.james.mailbox.store.FlagsUpdateCalculator;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
+import org.apache.james.mailbox.store.mail.model.concurrency.ConcurrentTestRunner;
 import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
@@ -66,37 +63,6 @@ import com.google.common.collect.Lists;
 @Contract(MapperProvider.class)
 public class MessageMapperTest<T extends MapperProvider> {
 
-    private class ConcurrentSetFlagTestRunnable implements Runnable {
-        private final int threadNumber;
-        private final int updateCount;
-        private final Mailbox mailbox;
-        private final MessageUid uid;
-        private final CountDownLatch countDownLatch;
-
-        public ConcurrentSetFlagTestRunnable(int threadNumber, int updateCount, Mailbox mailbox, MessageUid uid, CountDownLatch countDownLatch) {
-            this.threadNumber = threadNumber;
-            this.updateCount = updateCount;
-            this.mailbox = mailbox;
-            this.uid = uid;
-            this.countDownLatch = countDownLatch;
-        }
-
-        @Override
-        public void run() {
-            countDownLatch.countDown();
-            for (int i = 0; i < updateCount; i++) {
-                try {
-                    messageMapper.updateFlags(mailbox,
-                        new FlagsUpdateCalculator(new Flags("custom-" + threadNumber + "-" + i),
-                            FlagsUpdateMode.ADD),
-                        MessageRange.one(uid));
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            }
-        }
-    }
-
     private final static char DELIMITER = '.';
     private static final int LIMIT = 10;
     private static final int BODY_START = 16;
@@ -735,15 +701,14 @@ public class MessageMapperTest<T extends MapperProvider> {
 
         int threadCount = 2;
         int updateCount = 10;
-        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
-        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
-        for (int i = 0; i < threadCount; i++) {
-            executorService.submit(new ConcurrentSetFlagTestRunnable(i, updateCount,
-                benwaInboxMailbox, message1.getUid(), countDownLatch));
-        }
-        executorService.shutdown();
-        assertThat(executorService.awaitTermination(1, TimeUnit.MINUTES))
-            .isTrue();
+        new ConcurrentTestRunner(threadCount, updateCount) {
+            @Override
+            protected void performOperation(int threadNumber, int step) throws Exception {
+                messageMapper.updateFlags(benwaInboxMailbox,
+                    new FlagsUpdateCalculator(new Flags("custom-" + threadNumber + "-" + step), FlagsUpdateMode.ADD),
+                    MessageRange.one(message1.getUid()));
+            }
+        }.run();
 
         Iterator<MailboxMessage> messages = messageMapper.findInMailbox(benwaInboxMailbox, MessageRange.one(message1.getUid()),
             FetchType.Metadata, 1);

http://git-wip-us.apache.org/repos/asf/james-project/blob/04b15802/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/concurrency/ConcurrentTestRunner.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/concurrency/ConcurrentTestRunner.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/concurrency/ConcurrentTestRunner.java
new file mode 100644
index 0000000..81006e5
--- /dev/null
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/concurrency/ConcurrentTestRunner.java
@@ -0,0 +1,77 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.store.mail.model.concurrency;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class ConcurrentTestRunner {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ConcurrentTestRunner.class);
+
+    public class ConcurrentRunnableTask implements Runnable {
+        private final int threadNumber;
+
+        public ConcurrentRunnableTask(int threadNumber) {
+            this.threadNumber = threadNumber;
+        }
+
+        @Override
+        public void run() {
+            countDownLatch.countDown();
+            for (int i = 0; i < operationCount; i++) {
+                try {
+                    performOperation(threadNumber, i);
+                } catch (Exception e) {
+                    LOGGER.error("Error caught during concurrent testing", e);
+                }
+            }
+        }
+    }
+
+    private final int threadCount;
+    private final int operationCount;
+    private final CountDownLatch countDownLatch;
+
+    public ConcurrentTestRunner(int threadCount, int operationCount) {
+        this.threadCount = threadCount;
+        this.operationCount = operationCount;
+        this.countDownLatch = new CountDownLatch(threadCount);
+    }
+
+    public void run() throws Exception {
+        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
+        for (int i = 0; i < threadCount; i++) {
+            executorService.submit(new ConcurrentRunnableTask(i));
+        }
+        executorService.shutdown();
+        assertThat(executorService.awaitTermination(1, TimeUnit.MINUTES))
+            .isTrue();
+    }
+
+    protected abstract void performOperation(int threadNumber, int step) throws Exception;
+}


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