james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject [james-project] 06/09: JAMES-2704 Fix randomness of RandomStoring mailet
Date Fri, 14 Jun 2019 16:32:57 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 1487aa7c3e3b46bd36a6b003aabcacbcc86a341b
Author: Gautier DI FOLCO <gdifolco@linagora.com>
AuthorDate: Thu Jun 13 14:53:11 2019 +0200

    JAMES-2704 Fix randomness of RandomStoring mailet
---
 .../apache/james/smtp/SmtpRandomStoringTest.java   |  4 ++--
 .../james/transport/mailets/RandomStoring.java     | 22 +++++++++++-----------
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpRandomStoringTest.java
b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpRandomStoringTest.java
index 81f8958..08b6ab5 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpRandomStoringTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpRandomStoringTest.java
@@ -142,7 +142,7 @@ public class SmtpRandomStoringTest {
         SMTPMessageSender authenticatedSmtpConnection = messageSender.connect(LOCALHOST_IP,
jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort())
                 .authenticate(FROM, PASSWORD);
 
-        IntStream.range(1, numberOfMails)
+        IntStream.range(0, numberOfMails)
             .forEach(Throwing.intConsumer(index ->
                 authenticatedSmtpConnection
                     .sendMessage(buildMail("Message " + index))).sneakyThrow());
@@ -155,7 +155,7 @@ public class SmtpRandomStoringTest {
         awaitAtMostTenSeconds
             .untilAsserted(() -> checkMailboxesHaveBeenFilled(connections, numberOfMails));
 
-        connections.forEach(Throwing.consumer(IMAPMessageReader::close));
+        connections.forEach(Throwing.consumer(IMAPMessageReader::close).sneakyThrow());
     }
 
     private IMAPMessageReader createIMAPConnection(String username) {
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 783fb56..eabcf60 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
@@ -21,12 +21,10 @@ package org.apache.james.transport.mailets;
 
 import java.time.Duration;
 import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.ThreadLocalRandom;
-import java.util.stream.Collectors;
+import java.util.function.Supplier;
 import java.util.stream.Stream;
 import javax.inject.Inject;
 import javax.mail.MessagingException;
@@ -59,13 +57,13 @@ public class RandomStoring extends GenericMailet {
     private final Mono<List<ReroutingInfos>> reroutingInfos;
     private final UsersRepository usersRepository;
     private final MailboxManager mailboxManager;
-    private final Iterator<Integer> randomRecipientsNumbers;
+    private final Supplier<Integer> randomRecipientsNumbers;
 
     @Inject
     public RandomStoring(UsersRepository usersRepository, MailboxManager mailboxManager)
{
         this.usersRepository = usersRepository;
         this.mailboxManager = mailboxManager;
-        this.randomRecipientsNumbers = ThreadLocalRandom.current().ints(MIN_NUMBER_OF_RECIPIENTS,
MAX_NUMBER_OF_RECIPIENTS + 1).boxed().iterator();
+        this.randomRecipientsNumbers = () -> ThreadLocalRandom.current().nextInt(MIN_NUMBER_OF_RECIPIENTS,
MAX_NUMBER_OF_RECIPIENTS + 1);
         this.reroutingInfos = Mono.fromCallable(this::retrieveReroutingInfos).cache(CACHE_DURATION);
     }
 
@@ -93,11 +91,14 @@ public class RandomStoring extends GenericMailet {
 
     private Collection<ReroutingInfos> generateRandomMailboxes() {
         List<ReroutingInfos> reroutingInfos = this.reroutingInfos.block();
-        Collections.shuffle(reroutingInfos);
 
-        return reroutingInfos
-            .stream()
-            .limit(randomRecipientsNumbers.next())
+        // Replaces Collections.shuffle() which has a too poor statistical distribution
+        return ThreadLocalRandom
+            .current()
+            .ints(0, reroutingInfos.size())
+            .mapToObj(reroutingInfos::get)
+            .distinct()
+            .limit(randomRecipientsNumbers.get())
             .collect(Guavate.toImmutableSet());
     }
 
@@ -105,8 +106,7 @@ public class RandomStoring extends GenericMailet {
         return Streams.stream(usersRepository.list())
             .map(User::fromUsername)
             .flatMap(this::buildReRoutingInfos)
-            .distinct()
-            .collect(Collectors.toList());
+            .collect(Guavate.toImmutableList());
     }
 
     private Stream<ReroutingInfos> buildReRoutingInfos(User user) {


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