MAILBOX-342 Improve data provisioning logic in MailboxManagerTest 117 mailboxes were always provisioned.
Avoid this leads to cool, free test speed improvments: - Cassandra: 6 min 44 -> 1 min 46 - Memory: 8s -> 1,4s - MailDir (x2): 11s -> 2s - JCR: 1 min 45 -> 14s No stats for HBase, JPA Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d6de4025 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d6de4025 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d6de4025 Branch: refs/heads/master Commit: d6de40258077707cfb04615d5a0b221698ad922e Parents: a0cab76 Author: benwa <[email protected]> Authored: Thu Jul 5 15:20:10 2018 +0700 Committer: benwa <[email protected]> Committed: Fri Jul 6 16:25:50 2018 +0700 ---------------------------------------------------------------------- .../apache/james/mailbox/MessageManager.java | 5 + .../james/mailbox/MailboxManagerTest.java | 10 +- .../james/mailbox/mock/DataProvisioner.java | 131 +++++++++++++++ .../james/mailbox/mock/MockMailboxManager.java | 164 ------------------- .../cassandra/CassandraMailboxManagerTest.java | 10 ++ .../james/mailbox/copier/MailboxCopierTest.java | 10 +- 6 files changed, 157 insertions(+), 173 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/d6de4025/mailbox/api/src/main/java/org/apache/james/mailbox/MessageManager.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/MessageManager.java b/mailbox/api/src/main/java/org/apache/james/mailbox/MessageManager.java index 27123b3..41e79c8 100644 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/MessageManager.java +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/MessageManager.java @@ -22,6 +22,7 @@ package org.apache.james.mailbox; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.EnumSet; import java.util.Iterator; @@ -209,6 +210,10 @@ public interface MessageManager { return build(new ByteArrayInputStream(msgIn)); } + public AppendCommand build(String msgIn) { + return build(msgIn.getBytes(StandardCharsets.UTF_8)); + } + public AppendCommand build(Message message) throws IOException { return build(DefaultMessageWriter.asBytes(message)); } http://git-wip-us.apache.org/repos/asf/james-project/blob/d6de4025/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java index b729b64..2f42eec 100644 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java @@ -32,7 +32,7 @@ import org.apache.james.mailbox.MailboxManager.MailboxCapabilities; import org.apache.james.mailbox.MessageManager.AppendCommand; import org.apache.james.mailbox.exception.AnnotationException; import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.mock.MockMailboxManager; +import org.apache.james.mailbox.mock.DataProvisioner; import org.apache.james.mailbox.model.MailboxACL; import org.apache.james.mailbox.model.MailboxAnnotation; import org.apache.james.mailbox.model.MailboxAnnotationKey; @@ -97,7 +97,7 @@ public abstract class MailboxManagerTest { protected abstract MailboxManager provideMailboxManager() throws MailboxException; public void setUp() throws Exception { - this.mailboxManager = new MockMailboxManager(provideMailboxManager()).getMockMailboxManager(); + this.mailboxManager = provideMailboxManager(); this.message = Message.Builder.of() .setSubject("test") @@ -230,8 +230,10 @@ public abstract class MailboxManagerTest { public void listShouldReturnMailboxes() throws MailboxException, UnsupportedEncodingException { session = mailboxManager.createSystemSession("manager"); mailboxManager.startProcessingRequest(session); - - assertThat(mailboxManager.list(session)).hasSize(MockMailboxManager.EXPECTED_MAILBOXES_COUNT); + + DataProvisioner.feedMailboxManager(mailboxManager); + + assertThat(mailboxManager.list(session)).hasSize(DataProvisioner.EXPECTED_MAILBOXES_COUNT); } @Test http://git-wip-us.apache.org/repos/asf/james-project/blob/d6de4025/mailbox/api/src/test/java/org/apache/james/mailbox/mock/DataProvisioner.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/mock/DataProvisioner.java b/mailbox/api/src/test/java/org/apache/james/mailbox/mock/DataProvisioner.java new file mode 100644 index 0000000..8914a6e --- /dev/null +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/mock/DataProvisioner.java @@ -0,0 +1,131 @@ +/**************************************************************** + * 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.mock; + +import java.util.stream.IntStream; + +import javax.mail.Flags; + +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MessageManager; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.model.MailboxConstants; +import org.apache.james.mailbox.model.MailboxPath; + +import com.github.fge.lambdas.Throwing; + +public class DataProvisioner { + + /** + * Number of Domains to be created in the Mailbox Manager. + */ + public static final int DOMAIN_COUNT = 3; + + /** + * Number of Users (with INBOX) to be created in the Mailbox Manager. + */ + public static final int USER_COUNT = 3; + + /** + * Number of Sub Mailboxes (mailbox in INBOX) to be created in the Mailbox Manager. + */ + public static final int SUB_MAILBOXES_COUNT = 3; + + /** + * Number of Sub Sub Mailboxes (mailbox in a mailbox under INBOX) to be created in the Mailbox Manager. + */ + public static final int SUB_SUB_MAILBOXES_COUNT = 3; + + /** + * The expected Mailboxes count calculated based on the feeded mails. + */ + public static final int EXPECTED_MAILBOXES_COUNT = DOMAIN_COUNT * + (USER_COUNT + // INBOX + USER_COUNT * SUB_MAILBOXES_COUNT + // INBOX.SUB_FOLDER + USER_COUNT * SUB_MAILBOXES_COUNT * SUB_SUB_MAILBOXES_COUNT); // INBOX.SUB_FOLDER.SUBSUB_FOLDER + + /** + * Number of Messages per Mailbox to be created in the Mailbox Manager. + */ + public static final int MESSAGE_PER_MAILBOX_COUNT = 3; + + /** + * Utility method to feed the Mailbox Manager with a number of + * mailboxes and messages per mailbox. + */ + public static void feedMailboxManager(MailboxManager mailboxManager) { + IntStream.range(0, DOMAIN_COUNT) + .mapToObj(i -> "localhost" + i) + .forEach(domain -> provisionDomain(mailboxManager, domain)); + } + + public static void provisionDomain(MailboxManager mailboxManager, String domain) { + IntStream.range(0, USER_COUNT) + .mapToObj(i -> "user" + i + "@" + domain) + .forEach(Throwing.consumer(user -> provisionUser(mailboxManager, user))); + } + + private static void provisionUser(MailboxManager mailboxManager, String user) throws MailboxException { + MailboxSession mailboxSession = mailboxManager.createSystemSession(user); + mailboxManager.startProcessingRequest(mailboxSession); + + createMailbox(mailboxManager, mailboxSession, MailboxPath.inbox(mailboxSession)); + + IntStream.range(0, SUB_MAILBOXES_COUNT) + .mapToObj(i -> MailboxConstants.INBOX + ".SUB_FOLDER_" + i) + .peek(name -> createMailbox(mailboxManager, mailboxSession, MailboxPath.forUser(user, name))) + .forEach(name -> createSubSubMailboxes(mailboxManager, mailboxSession, name)); + + mailboxManager.endProcessingRequest(mailboxSession); + mailboxManager.logout(mailboxSession, true); + } + + private static void createSubSubMailboxes(MailboxManager mailboxManager,MailboxSession mailboxSession, String subFolderName) { + IntStream.range(0, SUB_SUB_MAILBOXES_COUNT) + .mapToObj(i -> subFolderName + ".SUBSUB_FOLDER_" + i) + .forEach(name -> createMailbox(mailboxManager, mailboxSession, MailboxPath.forUser(mailboxSession.getUser().getUserName(), name))); + + } + + private static void createMailbox(MailboxManager mailboxManager, MailboxSession mailboxSession, MailboxPath mailboxPath) { + try { + mailboxManager.createMailbox(mailboxPath, mailboxSession); + MessageManager messageManager = mailboxManager.getMailbox(mailboxPath, mailboxSession); + + IntStream.range(0, MESSAGE_PER_MAILBOX_COUNT) + .forEach(i -> appendMessage(messageManager, mailboxSession)); + } catch (MailboxException e) { + throw new RuntimeException(e); + } + } + + private static void appendMessage(MessageManager messageManager, MailboxSession mailboxSession) { + try { + messageManager.appendMessage( + MessageManager.AppendCommand.builder() + .recent() + .withFlags(new Flags(Flags.Flag.RECENT)) + .build(MockMail.MAIL_TEXT_PLAIN), + mailboxSession); + } catch (MailboxException e) { + throw new RuntimeException(e); + } + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/d6de4025/mailbox/api/src/test/java/org/apache/james/mailbox/mock/MockMailboxManager.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/mock/MockMailboxManager.java b/mailbox/api/src/test/java/org/apache/james/mailbox/mock/MockMailboxManager.java deleted file mode 100644 index da8a360..0000000 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/mock/MockMailboxManager.java +++ /dev/null @@ -1,164 +0,0 @@ -/**************************************************************** - * 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.mock; - -import java.io.ByteArrayInputStream; -import java.io.UnsupportedEncodingException; -import java.util.Calendar; - -import javax.mail.Flags; - -import org.apache.james.mailbox.MailboxManager; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.MessageManager; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.model.MailboxPath; - -/** - * A mock mailbox manager. - * - */ -public class MockMailboxManager { - - /** - * The mock mailbox manager constructed based on a provided mailboxmanager. - */ - private final MailboxManager mockMailboxManager; - - /** - * Number of Domains to be created in the Mailbox Manager. - */ - public static final int DOMAIN_COUNT = 3; - - /** - * Number of Users (with INBOX) to be created in the Mailbox Manager. - */ - public static final int USER_COUNT = 3; - - /** - * Number of Sub Mailboxes (mailbox in INBOX) to be created in the Mailbox Manager. - */ - public static final int SUB_MAILBOXES_COUNT = 3; - - /** - * Number of Sub Sub Mailboxes (mailbox in a mailbox under INBOX) to be created in the Mailbox Manager. - */ - public static final int SUB_SUB_MAILBOXES_COUNT = 3; - - /** - * The expected Mailboxes count calculated based on the feeded mails. - */ - public static final int EXPECTED_MAILBOXES_COUNT = DOMAIN_COUNT * - (USER_COUNT + // INBOX - USER_COUNT * SUB_MAILBOXES_COUNT + // INBOX.SUB_FOLDER - USER_COUNT * SUB_MAILBOXES_COUNT * SUB_SUB_MAILBOXES_COUNT); // INBOX.SUB_FOLDER.SUBSUB_FOLDER - - /** - * Number of Messages per Mailbox to be created in the Mailbox Manager. - */ - public static final int MESSAGE_PER_MAILBOX_COUNT = 3; - - /** - * Construct a mock mailboxManager based on a valid mailboxManager. - * The mailboxManager will be feeded with mailboxes and mails. - * - * @param mailboxManager - * @throws UnsupportedEncodingException - * @throws MailboxException - */ - public MockMailboxManager(MailboxManager mailboxManager) throws MailboxException, UnsupportedEncodingException { - this.mockMailboxManager = mailboxManager; - feedMockMailboxManager(); - } - - /** - * @return - */ - public MailboxManager getMockMailboxManager() { - return mockMailboxManager; - } - - /** - * Utility method to feed the Mailbox Manager with a number of - * mailboxes and messages per mailbox. - * - * @throws MailboxException - * @throws UnsupportedEncodingException - */ - private void feedMockMailboxManager() throws MailboxException, UnsupportedEncodingException { - - MailboxPath mailboxPath = null; - - for (int i = 0; i < DOMAIN_COUNT; i++) { - - for (int j = 0; j < USER_COUNT; j++) { - - String user = "user" + j + "@localhost" + i; - - String folderName = "INBOX"; - - MailboxSession mailboxSession = getMockMailboxManager().createSystemSession(user); - mailboxPath = MailboxPath.forUser(user, folderName); - createMailbox(mailboxSession, mailboxPath); - - for (int k = 0; k < SUB_MAILBOXES_COUNT; k++) { - - String subFolderName = folderName + ".SUB_FOLDER_" + k; - mailboxPath = MailboxPath.forUser(user, subFolderName); - createMailbox(mailboxSession, mailboxPath); - - for (int l = 0; l < SUB_SUB_MAILBOXES_COUNT; l++) { - - String subSubfolderName = subFolderName + ".SUBSUB_FOLDER_" + l; - mailboxPath = MailboxPath.forUser(user, subSubfolderName); - createMailbox(mailboxSession, mailboxPath); - - } - - } - - getMockMailboxManager().logout(mailboxSession, true); - - } - - } - - } - - /** - * - * @param mailboxPath - * @throws MailboxException - * @throws UnsupportedEncodingException - */ - private void createMailbox(MailboxSession mailboxSession, MailboxPath mailboxPath) throws MailboxException, UnsupportedEncodingException { - getMockMailboxManager().startProcessingRequest(mailboxSession); - getMockMailboxManager().createMailbox(mailboxPath, mailboxSession); - MessageManager messageManager = getMockMailboxManager().getMailbox(mailboxPath, mailboxSession); - for (int j = 0; j < MESSAGE_PER_MAILBOX_COUNT; j++) { - messageManager.appendMessage(new ByteArrayInputStream(MockMail.MAIL_TEXT_PLAIN.getBytes("UTF-8")), - Calendar.getInstance().getTime(), - mailboxSession, - true, - new Flags(Flags.Flag.RECENT)); - } - getMockMailboxManager().endProcessingRequest(mailboxSession); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/d6de4025/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerTest.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerTest.java b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerTest.java index 4594300..795afec 100644 --- a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerTest.java +++ b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerTest.java @@ -19,6 +19,7 @@ package org.apache.james.mailbox.cassandra; import org.apache.james.backends.cassandra.CassandraCluster; +import org.apache.james.backends.cassandra.ContainerLifecycleConfiguration; import org.apache.james.backends.cassandra.DockerCassandraRule; import org.apache.james.backends.cassandra.init.CassandraModuleComposite; import org.apache.james.blob.cassandra.CassandraBlobModule; @@ -41,10 +42,19 @@ import org.apache.james.mailbox.cassandra.modules.CassandraUidModule; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.rules.TestRule; public class CassandraMailboxManagerTest extends MailboxManagerTest { @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); + + public static ContainerLifecycleConfiguration cassandraLifecycleConfiguration = ContainerLifecycleConfiguration.withDefaultIterationsBetweenRestart() + .container(cassandraServer.getRawContainer()) + .build(); + + @Rule + public TestRule cassandraLifecycleTestRule = cassandraLifecycleConfiguration.asTestRule(); private CassandraCluster cassandra; http://git-wip-us.apache.org/repos/asf/james-project/blob/d6de4025/mailbox/tool/src/test/java/org/apache/james/mailbox/copier/MailboxCopierTest.java ---------------------------------------------------------------------- diff --git a/mailbox/tool/src/test/java/org/apache/james/mailbox/copier/MailboxCopierTest.java b/mailbox/tool/src/test/java/org/apache/james/mailbox/copier/MailboxCopierTest.java index 126bc39..77d4c42 100644 --- a/mailbox/tool/src/test/java/org/apache/james/mailbox/copier/MailboxCopierTest.java +++ b/mailbox/tool/src/test/java/org/apache/james/mailbox/copier/MailboxCopierTest.java @@ -31,7 +31,7 @@ import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver; import org.apache.james.mailbox.exception.BadCredentialsException; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources; -import org.apache.james.mailbox.mock.MockMailboxManager; +import org.apache.james.mailbox.mock.DataProvisioner; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.store.StoreMailboxManager; import org.junit.Before; @@ -97,8 +97,8 @@ public class MailboxCopierTest { if (dstMemMailboxManager instanceof StoreMailboxManager) { ((StoreMailboxManager) dstMemMailboxManager).init(); } - - srcMemMailboxManager = new MockMailboxManager(srcMemMailboxManager).getMockMailboxManager(); + + DataProvisioner.feedMailboxManager(srcMemMailboxManager); assertMailboxManagerSize(srcMemMailboxManager, 1); @@ -124,13 +124,13 @@ public class MailboxCopierTest { List<MailboxPath> mailboxPathList = mailboxManager.list(mailboxSession); - assertThat(mailboxPathList).hasSize(MockMailboxManager.EXPECTED_MAILBOXES_COUNT); + assertThat(mailboxPathList).hasSize(DataProvisioner.EXPECTED_MAILBOXES_COUNT); for (MailboxPath mailboxPath: mailboxPathList) { MailboxSession userSession = mailboxManager.createSystemSession(mailboxPath.getUser()); mailboxManager.startProcessingRequest(mailboxSession); MessageManager messageManager = mailboxManager.getMailbox(mailboxPath, userSession); - assertThat(messageManager.getMetaData(false, userSession, FetchGroup.NO_UNSEEN).getMessageCount()).isEqualTo(MockMailboxManager.MESSAGE_PER_MAILBOX_COUNT * multiplicationFactor); + assertThat(messageManager.getMetaData(false, userSession, FetchGroup.NO_UNSEEN).getMessageCount()).isEqualTo(DataProvisioner.MESSAGE_PER_MAILBOX_COUNT * multiplicationFactor); } mailboxManager.endProcessingRequest(mailboxSession); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
