james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [james-project] 02/04: JAMES-2784 Replaces usage of Random by ThreadLocalRandom
Date Thu, 06 Jun 2019 09:33:44 GMT
This is an automated email from the ASF dual-hosted git repository.

rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit e1eaa3c0b74d77fb10c93a0005d5e1763c37bc52
Author: Raphael Ouazana <raphael.ouazana@linagora.com>
AuthorDate: Mon Jun 3 11:22:33 2019 +0200

    JAMES-2784 Replaces usage of Random by ThreadLocalRandom
---
 .../src/test/java/org/apache/james/mailbox/MailboxSessionUtil.java | 6 ++----
 .../java/org/apache/james/mailbox/jpa/mail/JPAMapperProvider.java  | 4 ++--
 .../apache/james/mailbox/inmemory/mail/InMemoryMapperProvider.java | 6 ++----
 .../container/util/src/main/java/org/apache/james/util/Port.java   | 4 ++--
 .../java/org/apache/james/transport/mailets/RandomStoring.java     | 4 ++--
 .../org/apache/james/mailrepository/MailRepositoryContract.java    | 7 +++----
 .../java/org/apache/james/jmap/memory/MemorySendMDNMethodTest.java | 6 ++----
 .../org/apache/james/jmap/memory/MemorySetMessagesMethodTest.java  | 6 ++----
 .../src/test/java/org/apache/james/smtpserver/Util.java            | 6 ++----
 9 files changed, 19 insertions(+), 30 deletions(-)

diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxSessionUtil.java b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxSessionUtil.java
index a307020..82e3013 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxSessionUtil.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxSessionUtil.java
@@ -21,17 +21,15 @@ package org.apache.james.mailbox;
 
 import java.util.ArrayList;
 import java.util.Locale;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 import org.apache.james.mailbox.model.MailboxConstants;
 
 import com.google.common.annotations.VisibleForTesting;
 
 public class MailboxSessionUtil {
-    private static final Random RANDOM = new Random();
-
     public static MailboxSession create(String username) {
-        return create(username, MailboxSession.SessionId.of(RANDOM.nextLong()));
+        return create(username, MailboxSession.SessionId.of(ThreadLocalRandom.current().nextLong()));
     }
 
     @VisibleForTesting
diff --git a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/mail/JPAMapperProvider.java
b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/mail/JPAMapperProvider.java
index 17b55da..0d4b58f 100644
--- a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/mail/JPAMapperProvider.java
+++ b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/mail/JPAMapperProvider.java
@@ -20,7 +20,7 @@
 package org.apache.james.mailbox.jpa.mail;
 
 import java.util.List;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 import javax.persistence.EntityManagerFactory;
 
@@ -82,7 +82,7 @@ public class JPAMapperProvider implements MapperProvider {
 
     @Override
     public MailboxId generateId() {
-        return JPAId.of(Math.abs(new Random().nextInt()));
+        return JPAId.of(Math.abs(ThreadLocalRandom.current().nextInt()));
     }
 
     @Override
diff --git a/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/mail/InMemoryMapperProvider.java
b/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/mail/InMemoryMapperProvider.java
index 35d3432..3162d91 100644
--- a/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/mail/InMemoryMapperProvider.java
+++ b/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/mail/InMemoryMapperProvider.java
@@ -20,7 +20,7 @@
 package org.apache.james.mailbox.inmemory.mail;
 
 import java.util.List;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MailboxSessionUtil;
@@ -45,14 +45,12 @@ public class InMemoryMapperProvider implements MapperProvider {
 
     private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create("user");
 
-    private final Random random;
     private final MessageId.Factory messageIdFactory;
     private final MessageUidProvider messageUidProvider;
     private final InMemoryMailboxSessionMapperFactory inMemoryMailboxSessionMapperFactory;
 
 
     public InMemoryMapperProvider() {
-        random = new Random();
         messageIdFactory = new InMemoryMessageId.Factory();
         messageUidProvider = new MessageUidProvider();
         inMemoryMailboxSessionMapperFactory = new InMemoryMailboxSessionMapperFactory();
@@ -82,7 +80,7 @@ public class InMemoryMapperProvider implements MapperProvider {
 
     @Override
     public InMemoryId generateId() {
-        return InMemoryId.of(random.nextInt());
+        return InMemoryId.of(ThreadLocalRandom.current().nextInt());
     }
 
     @Override
diff --git a/server/container/util/src/main/java/org/apache/james/util/Port.java b/server/container/util/src/main/java/org/apache/james/util/Port.java
index 5c90aa7..7f84202 100644
--- a/server/container/util/src/main/java/org/apache/james/util/Port.java
+++ b/server/container/util/src/main/java/org/apache/james/util/Port.java
@@ -19,7 +19,7 @@
 
 package org.apache.james.util;
 
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Range;
@@ -34,7 +34,7 @@ public class Port {
     }
 
     public static int generateValidUnprivilegedPort() {
-        return new Random().nextInt(Port.MAX_PORT_VALUE - PRIVILEGED_PORT_BOUND) + PRIVILEGED_PORT_BOUND;
+        return ThreadLocalRandom.current().nextInt(Port.MAX_PORT_VALUE - PRIVILEGED_PORT_BOUND)
+ PRIVILEGED_PORT_BOUND;
     }
 
     public static void assertValid(int port) {
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RandomStoring.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RandomStoring.java
index 8423b37..783fb56 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RandomStoring.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RandomStoring.java
@@ -25,7 +25,7 @@ import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Objects;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import javax.inject.Inject;
@@ -65,7 +65,7 @@ public class RandomStoring extends GenericMailet {
     public RandomStoring(UsersRepository usersRepository, MailboxManager mailboxManager)
{
         this.usersRepository = usersRepository;
         this.mailboxManager = mailboxManager;
-        this.randomRecipientsNumbers = new Random().ints(MIN_NUMBER_OF_RECIPIENTS, MAX_NUMBER_OF_RECIPIENTS
+ 1).boxed().iterator();
+        this.randomRecipientsNumbers = ThreadLocalRandom.current().ints(MIN_NUMBER_OF_RECIPIENTS,
MAX_NUMBER_OF_RECIPIENTS + 1).boxed().iterator();
         this.reroutingInfos = Mono.fromCallable(this::retrieveReroutingInfos).cache(CACHE_DURATION);
     }
 
diff --git a/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/MailRepositoryContract.java
b/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/MailRepositoryContract.java
index 7dab1fa..c445f49 100644
--- a/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/MailRepositoryContract.java
+++ b/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/MailRepositoryContract.java
@@ -26,8 +26,8 @@ import java.nio.charset.StandardCharsets;
 import java.time.Duration;
 import java.util.Date;
 import java.util.List;
-import java.util.Random;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
 import java.util.stream.IntStream;
 
 import javax.mail.MessagingException;
@@ -444,9 +444,8 @@ public interface MailRepositoryContract {
             .boxed()
             .collect(Guavate.toImmutableList());
 
-        Random random = new Random();
         ThrowingRunnable add = () -> {
-            int keyIndex = computeKeyIndex(nbKeys, Math.abs(random.nextInt()));
+            int keyIndex = computeKeyIndex(nbKeys, Math.abs(ThreadLocalRandom.current().nextInt()));
             MailKey key =  computeKey(keyIndex);
             synchronized (locks.get(keyIndex)) {
                 testee.store(createMail(key));
@@ -455,7 +454,7 @@ public interface MailRepositoryContract {
         };
 
         ThrowingRunnable remove = () -> {
-            int keyIndex = computeKeyIndex(nbKeys, Math.abs(random.nextInt()));
+            int keyIndex = computeKeyIndex(nbKeys, Math.abs(ThreadLocalRandom.current().nextInt()));
             MailKey key =  computeKey(keyIndex);
             synchronized (locks.get(keyIndex)) {
                 testee.remove(key);
diff --git a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemorySendMDNMethodTest.java
b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemorySendMDNMethodTest.java
index 7728735..8cde87e 100644
--- a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemorySendMDNMethodTest.java
+++ b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemorySendMDNMethodTest.java
@@ -19,7 +19,7 @@
 
 package org.apache.james.jmap.memory;
 
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 import org.apache.activemq.store.PersistenceAdapter;
 import org.apache.activemq.store.memory.MemoryPersistenceAdapter;
@@ -51,10 +51,8 @@ public class MemorySendMDNMethodTest extends SendMDNMethodTest {
             .overrideWith(binder -> binder.bind(MessageSearchIndex.class).to(SimpleMessageSearchIndex.class)))
         .build();
 
-    private Random random = new Random();
-
     @Override
     protected MessageId randomMessageId() {
-        return new InMemoryMessageId.Factory().fromString(String.valueOf(random.nextInt(100000)
+ 100));
+        return new InMemoryMessageId.Factory().fromString(String.valueOf(ThreadLocalRandom.current().nextInt(100000)
+ 100));
     }
 }
diff --git a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemorySetMessagesMethodTest.java
b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemorySetMessagesMethodTest.java
index 15c5bde..7519fc9 100644
--- a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemorySetMessagesMethodTest.java
+++ b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemorySetMessagesMethodTest.java
@@ -20,7 +20,7 @@
 package org.apache.james.jmap.memory;
 
 import java.io.IOException;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 import org.apache.james.GuiceJamesServer;
 import org.apache.james.MemoryJmapTestRule;
@@ -34,8 +34,6 @@ public class MemorySetMessagesMethodTest extends SetMessagesMethodTest {
     @Rule
     public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule();
 
-    private Random random = new Random();
-    
     @Override
     protected GuiceJamesServer createJmapServer() throws IOException {
         return memoryJmap.jmapServer();
@@ -47,7 +45,7 @@ public class MemorySetMessagesMethodTest extends SetMessagesMethodTest {
     
     @Override
     protected MessageId randomMessageId() {
-        return new InMemoryMessageId.Factory().fromString(String.valueOf(random.nextInt(100000)
+ 100));
+        return new InMemoryMessageId.Factory().fromString(String.valueOf(ThreadLocalRandom.current().nextInt(100000)
+ 100));
     }
 
 }
diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/Util.java
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/Util.java
index dc16c40..91c6f93 100644
--- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/Util.java
+++ b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/Util.java
@@ -18,7 +18,7 @@
  ****************************************************************/
 package org.apache.james.smtpserver;
 
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
@@ -32,11 +32,9 @@ import org.apache.mailet.base.test.FakeMail;
  */
 public class Util {
 
-    private static final Random RANDOM = new Random();
-
     public static Mail createMockMail2Recipients(MimeMessage m) throws MessagingException
{
         return FakeMail.builder()
-            .name("ID=" + RANDOM.nextLong())
+            .name("ID=" + ThreadLocalRandom.current().nextLong())
             .mimeMessage(m)
             .recipients("test@james.apache.org", "test2@james.apache.org")
             .build();


---------------------------------------------------------------------
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