JAMES-2285 Contracts for delayed mail queues
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/afcdc543 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/afcdc543 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/afcdc543 Branch: refs/heads/master Commit: afcdc5438e29e99e046edf0fbcc4208bcaab6296 Parents: ce04905 Author: benwa <[email protected]> Authored: Thu Jan 11 11:42:41 2018 +0700 Committer: benwa <[email protected]> Committed: Thu Jan 18 09:48:49 2018 +0700 ---------------------------------------------------------------------- .../queue/api/DelayedMailQueueContract.java | 117 +++++++++++++++++++ .../api/DelayedManageableMailQueueContract.java | 79 +++++++++++++ .../james/queue/jms/JMSMailQueueTest.java | 21 +++- 3 files changed, 215 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/afcdc543/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedMailQueueContract.java ---------------------------------------------------------------------- diff --git a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedMailQueueContract.java b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedMailQueueContract.java new file mode 100644 index 0000000..6f28311 --- /dev/null +++ b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedMailQueueContract.java @@ -0,0 +1,117 @@ +/**************************************************************** + * 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.queue.api; + +import static org.apache.james.queue.api.Mails.defaultMail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import com.github.fge.lambdas.Throwing; +import com.google.common.base.Stopwatch; + +@ExtendWith(ExecutorExtension.class) +public interface DelayedMailQueueContract { + + MailQueue getMailQueue(); + + @Test + default void enqueueShouldDelayMailsWhenSpecified(ExecutorService executorService) throws Exception { + getMailQueue().enQueue(defaultMail() + .build(), + 2L, + TimeUnit.SECONDS); + + Future<?> future = executorService.submit(Throwing.runnable(() -> getMailQueue().deQueue())); + assertThatThrownBy(() -> future.get(1, TimeUnit.SECONDS)) + .isInstanceOf(TimeoutException.class); + } + + @Test + default void enqueueWithNegativeDelayShouldNotDelayDelivery(ExecutorService executorService) throws Exception { + getMailQueue().enQueue(defaultMail() + .build(), + -30L, + TimeUnit.SECONDS); + + Future<?> future = executorService.submit(Throwing.runnable(() -> getMailQueue().deQueue())); + future.get(1, TimeUnit.SECONDS); + } + + @Test + default void enqueueWithReasonablyLongDelayShouldDelayMail(ExecutorService executorService) throws Exception { + getMailQueue().enQueue(defaultMail() + .build(), + 365*1000, + TimeUnit.DAYS); + + Future<?> future = executorService.submit(Throwing.runnable(() -> getMailQueue().deQueue())); + assertThatThrownBy(() -> future.get(1, TimeUnit.SECONDS)) + .isInstanceOf(TimeoutException.class); + } + + @Test + default void enqueueWithVeryLongDelayShouldDelayMail(ExecutorService executorService) throws Exception { + getMailQueue().enQueue(defaultMail() + .build(), + Long.MAX_VALUE / (3600 * 24), + TimeUnit.DAYS); + + Future<?> future = executorService.submit(Throwing.runnable(() -> getMailQueue().deQueue())); + assertThatThrownBy(() -> future.get(1, TimeUnit.SECONDS)) + .isInstanceOf(TimeoutException.class); + } + + @Test + default void delayedMailCanBeRetrievedFromTheQueue() throws Exception { + getMailQueue().enQueue(defaultMail() + .name("name1") + .build(), + 1L, + TimeUnit.SECONDS); + + MailQueue.MailQueueItem mailQueueItem = getMailQueue().deQueue(); + assertThat(mailQueueItem.getMail().getName()).isEqualTo("name1"); + } + + @Test + default void delayShouldAtLeastBeTheOneSpecified() throws Exception { + long delay = 1L; + TimeUnit unit = TimeUnit.SECONDS; + Stopwatch started = Stopwatch.createStarted(); + + getMailQueue().enQueue(defaultMail() + .build(), + delay, + unit); + + getMailQueue().deQueue(); + assertThat(started.elapsed(TimeUnit.MILLISECONDS)) + .isGreaterThan(unit.toMillis(delay)); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/afcdc543/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedManageableMailQueueContract.java ---------------------------------------------------------------------- diff --git a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedManageableMailQueueContract.java b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedManageableMailQueueContract.java new file mode 100644 index 0000000..1397bbf --- /dev/null +++ b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedManageableMailQueueContract.java @@ -0,0 +1,79 @@ +/**************************************************************** + * 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.queue.api; + +import static org.apache.james.queue.api.Mails.defaultMail; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.mailet.Mail; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +@ExtendWith(ExecutorExtension.class) +public interface DelayedManageableMailQueueContract extends DelayedMailQueueContract, ManageableMailQueueContract { + + ManageableMailQueue getManageableMailQueue(); + + @Test + default void flushShouldRemoveDelays(ExecutorService executorService) throws Exception { + getManageableMailQueue().enQueue(defaultMail() + .name("name1") + .build(), + 30L, + TimeUnit.SECONDS); + + getManageableMailQueue().flush(); + + Future<MailQueue.MailQueueItem> tryDequeue = executorService.submit(() -> getManageableMailQueue().deQueue()); + assertThat(tryDequeue.get(1, TimeUnit.SECONDS).getMail().getName()) + .isEqualTo("name1"); + } + + @Test + default void flushShouldPreserveBrowseOrder() throws Exception { + getManageableMailQueue().enQueue(defaultMail() + .name("name1") + .build()); + + getManageableMailQueue().enQueue(defaultMail() + .name("name2") + .build(), + 30L, + TimeUnit.SECONDS); + + getManageableMailQueue().enQueue(defaultMail() + .name("name3") + .build(), + 2L, + TimeUnit.SECONDS); + + getManageableMailQueue().flush(); + + assertThat(getManageableMailQueue().browse()) + .extracting(ManageableMailQueue.MailQueueItemView::getMail) + .extracting(Mail::getName) + .containsExactly("name1", "name2", "name3"); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/afcdc543/server/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java ---------------------------------------------------------------------- diff --git a/server/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java b/server/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java index 59b616e..8f9e39c 100644 --- a/server/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java +++ b/server/queue/queue-jms/src/test/java/org/apache/james/queue/jms/JMSMailQueueTest.java @@ -19,6 +19,8 @@ package org.apache.james.queue.jms; +import java.util.concurrent.ExecutorService; + import javax.jms.ConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory; @@ -26,9 +28,9 @@ import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.region.policy.PolicyEntry; import org.apache.activemq.broker.region.policy.PolicyMap; import org.apache.james.metrics.api.NoopMetricFactory; +import org.apache.james.queue.api.DelayedManageableMailQueueContract; import org.apache.james.queue.api.MailQueue; import org.apache.james.queue.api.ManageableMailQueue; -import org.apache.james.queue.api.ManageableMailQueueContract; import org.apache.james.queue.api.RawMailQueueItemDecoratorFactory; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -37,7 +39,7 @@ import org.junit.jupiter.api.Test; import com.google.common.collect.ImmutableList; -public class JMSMailQueueTest implements ManageableMailQueueContract { +public class JMSMailQueueTest implements DelayedManageableMailQueueContract { private final static String QUEUE_NAME = "test"; @@ -114,4 +116,19 @@ public class JMSMailQueueTest implements ManageableMailQueueContract { public void removeByRecipientShouldRemoveSpecificEmailWhenMultipleRecipients() { } + + @Test + @Override + @Disabled("JAMES-2308 Flushing JMS mail queue randomly re-order them" + + "Random test failing around 1% of the time") + public void flushShouldPreserveBrowseOrder() { + + } + + @Test + @Override + @Disabled("JAMES-2309 Long overflow in JMS delays") + public void enqueueWithVeryLongDelayShouldDelayMail(ExecutorService executorService) { + + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
