JAMES-2285 Contracts for prioritized 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/afe41b87
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/afe41b87
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/afe41b87

Branch: refs/heads/master
Commit: afe41b878caf62c5c726f7f46282a6cda5abacb3
Parents: afcdc54
Author: benwa <[email protected]>
Authored: Fri Jan 12 15:10:06 2018 +0700
Committer: benwa <[email protected]>
Committed: Thu Jan 18 09:48:49 2018 +0700

----------------------------------------------------------------------
 .../api/DelayedPriorityMailQueueContract.java   |  84 ++++++++
 .../queue/api/PriorityMailQueueContract.java    | 202 +++++++++++++++++++
 .../PriorityManageableMailQueueContract.java    |  89 ++++++++
 .../james/queue/jms/JMSMailQueueTest.java       |   4 +-
 4 files changed, 378 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/afe41b87/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedPriorityMailQueueContract.java
----------------------------------------------------------------------
diff --git 
a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedPriorityMailQueueContract.java
 
b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedPriorityMailQueueContract.java
new file mode 100644
index 0000000..a3fa18d
--- /dev/null
+++ 
b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedPriorityMailQueueContract.java
@@ -0,0 +1,84 @@
+/****************************************************************
+ * 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.TimeUnit;
+
+import org.junit.jupiter.api.Test;
+
+public interface DelayedPriorityMailQueueContract extends 
DelayedMailQueueContract, PriorityMailQueueContract {
+
+    MailQueue getMailQueue();
+
+    @Test
+    default void 
delayedHighPriorityMailShouldBeDeQueuedBeforeLowPriorityNonDelayedMailAfterDelayExpiracy()
 throws Exception {
+        getMailQueue().enQueue(defaultMail()
+            .name("name1")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 
MailPrioritySupport.LOW_PRIORITY)
+            .build());
+
+        int delay = 1;
+        TimeUnit unit = TimeUnit.SECONDS;
+        getMailQueue().enQueue(defaultMail()
+            .name("name2")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 
MailPrioritySupport.HIGH_PRIORITY)
+            .build(),
+            delay,
+            unit);
+
+        Thread.sleep(unit.toMillis(2 * delay));
+
+        MailQueue.MailQueueItem item1 = getMailQueue().deQueue();
+        item1.done(true);
+        MailQueue.MailQueueItem item2 = getMailQueue().deQueue();
+        item2.done(true);
+
+        assertThat(item1.getMail().getName()).isEqualTo("name2");
+        assertThat(item2.getMail().getName()).isEqualTo("name1");
+    }
+
+    @Test
+    default void delayedHighPriorityMailShouldBeDeQueuedAfterNonDelayedMail() 
throws Exception {
+        getMailQueue().enQueue(defaultMail()
+            .name("name1")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 
MailPrioritySupport.LOW_PRIORITY)
+            .build());
+
+        int delay = 1;
+        TimeUnit unit = TimeUnit.SECONDS;
+        getMailQueue().enQueue(defaultMail()
+            .name("name2")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 
MailPrioritySupport.HIGH_PRIORITY)
+            .build(),
+            delay,
+            unit);
+
+        MailQueue.MailQueueItem item1 = getMailQueue().deQueue();
+        item1.done(true);
+        MailQueue.MailQueueItem item2 = getMailQueue().deQueue();
+        item2.done(true);
+
+        assertThat(item1.getMail().getName()).isEqualTo("name1");
+        assertThat(item2.getMail().getName()).isEqualTo("name2");
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/afe41b87/server/queue/queue-api/src/test/java/org/apache/james/queue/api/PriorityMailQueueContract.java
----------------------------------------------------------------------
diff --git 
a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/PriorityMailQueueContract.java
 
b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/PriorityMailQueueContract.java
new file mode 100644
index 0000000..ffe8fee
--- /dev/null
+++ 
b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/PriorityMailQueueContract.java
@@ -0,0 +1,202 @@
+/****************************************************************
+ * 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.stream.IntStream;
+
+import org.apache.mailet.Mail;
+import org.junit.jupiter.api.Test;
+
+import com.github.fge.lambdas.Throwing;
+import com.github.steveash.guavate.Guavate;
+import com.google.common.collect.ImmutableList;
+
+public interface PriorityMailQueueContract {
+
+    MailQueue getMailQueue();
+
+    @Test
+    default void priorityShouldReorderMailsWhenDequeing() throws Exception {
+        getMailQueue().enQueue(defaultMail()
+            .name("name3")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 3)
+            .build());
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name9")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 9)
+            .build());
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name1")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 1)
+            .build());
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name8")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 8)
+            .build());
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name6")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 6)
+            .build());
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name0")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 0)
+            .build());
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name7")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 7)
+            .build());
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name4")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 4)
+            .build());
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name2")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 2)
+            .build());
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name5")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 5)
+            .build());
+
+        ImmutableList<MailQueue.MailQueueItem> items = IntStream.range(1, 
11).boxed()
+            .map(Throwing.function(i -> {
+                MailQueue.MailQueueItem item = getMailQueue().deQueue();
+                item.done(true);
+                return item;
+            }))
+            .collect(Guavate.toImmutableList());
+
+        assertThat(items)
+            .extracting(MailQueue.MailQueueItem::getMail)
+            .extracting(Mail::getName)
+            .containsExactly("name9", "name8", "name7", "name6", "name5", 
"name4", "name3", "name2", "name1", "name0");
+    }
+
+    @Test
+    default void negativePriorityShouldDefaultToMinimumPriority() throws 
Exception {
+        getMailQueue().enQueue(defaultMail()
+            .name("name0")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, -1)
+            .build());
+        getMailQueue().enQueue(defaultMail()
+            .name("name1")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 1)
+            .build());
+
+        MailQueue.MailQueueItem mailQueueItem1 = getMailQueue().deQueue();
+        mailQueueItem1.done(true);
+        MailQueue.MailQueueItem mailQueueItem2 = getMailQueue().deQueue();
+        mailQueueItem2.done(true);
+        assertThat(mailQueueItem1.getMail().getName()).isEqualTo("name1");
+        assertThat(mailQueueItem2.getMail().getName()).isEqualTo("name0");
+    }
+
+    @Test
+    default void tooBigPriorityShouldDefaultToMaximalPriority() throws 
Exception {
+        getMailQueue().enQueue(defaultMail()
+            .name("name0")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 12)
+            .build());
+        getMailQueue().enQueue(defaultMail()
+            .name("name1")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 8)
+            .build());
+
+        MailQueue.MailQueueItem mailQueueItem1 = getMailQueue().deQueue();
+        mailQueueItem1.done(true);
+        MailQueue.MailQueueItem mailQueueItem2 = getMailQueue().deQueue();
+        mailQueueItem2.done(true);
+        assertThat(mailQueueItem1.getMail().getName()).isEqualTo("name0");
+        assertThat(mailQueueItem2.getMail().getName()).isEqualTo("name1");
+    }
+
+    @Test
+    default void invalidPriorityShouldDefaultToNormalPriority() throws 
Exception {
+        getMailQueue().enQueue(defaultMail()
+            .name("name1")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, "invalid")
+            .build());
+        getMailQueue().enQueue(defaultMail()
+            .name("name2")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 4)
+            .build());
+        getMailQueue().enQueue(defaultMail()
+            .name("name3")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 6)
+            .build());
+
+        MailQueue.MailQueueItem mailQueueItem1 = getMailQueue().deQueue();
+        mailQueueItem1.done(true);
+        MailQueue.MailQueueItem mailQueueItem2 = getMailQueue().deQueue();
+        mailQueueItem2.done(true);
+        MailQueue.MailQueueItem mailQueueItem3 = getMailQueue().deQueue();
+        mailQueueItem3.done(true);
+        assertThat(mailQueueItem1.getMail().getName()).isEqualTo("name3");
+        assertThat(mailQueueItem2.getMail().getName()).isEqualTo("name1");
+        assertThat(mailQueueItem3.getMail().getName()).isEqualTo("name2");
+    }
+
+    @Test
+    default void defaultPriorityShouldDefaultToNormalPriority() throws 
Exception {
+        getMailQueue().enQueue(defaultMail()
+            .name("name1")
+            .build());
+        getMailQueue().enQueue(defaultMail()
+            .name("name2")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 4)
+            .build());
+        getMailQueue().enQueue(defaultMail()
+            .name("name3")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 6)
+            .build());
+
+        MailQueue.MailQueueItem mailQueueItem1 = getMailQueue().deQueue();
+        mailQueueItem1.done(true);
+        MailQueue.MailQueueItem mailQueueItem2 = getMailQueue().deQueue();
+        mailQueueItem2.done(true);
+        MailQueue.MailQueueItem mailQueueItem3 = getMailQueue().deQueue();
+        mailQueueItem3.done(true);
+        assertThat(mailQueueItem1.getMail().getName()).isEqualTo("name3");
+        assertThat(mailQueueItem2.getMail().getName()).isEqualTo("name1");
+        assertThat(mailQueueItem3.getMail().getName()).isEqualTo("name2");
+    }
+
+    @Test
+    default void priorityCanBeOmitted() throws Exception {
+        getMailQueue().enQueue(defaultMail()
+            .name("name1")
+            .build());
+
+        MailQueue.MailQueueItem mailQueueItem = getMailQueue().deQueue();
+        assertThat(mailQueueItem.getMail().getName()).isEqualTo("name1");
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/afe41b87/server/queue/queue-api/src/test/java/org/apache/james/queue/api/PriorityManageableMailQueueContract.java
----------------------------------------------------------------------
diff --git 
a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/PriorityManageableMailQueueContract.java
 
b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/PriorityManageableMailQueueContract.java
new file mode 100644
index 0000000..8351a48
--- /dev/null
+++ 
b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/PriorityManageableMailQueueContract.java
@@ -0,0 +1,89 @@
+/****************************************************************
+ * 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 org.apache.mailet.Mail;
+import org.junit.jupiter.api.Test;
+
+public interface PriorityManageableMailQueueContract extends 
ManageableMailQueueContract, PriorityMailQueueContract {
+
+    ManageableMailQueue getManageableMailQueue();
+
+    @Test
+    default void browseShouldBeOrderedByPriority() throws Exception {
+        getManageableMailQueue().enQueue(defaultMail()
+            .name("name3")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 3)
+            .build());
+
+        getManageableMailQueue().enQueue(defaultMail()
+            .name("name9")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 9)
+            .build());
+
+        getManageableMailQueue().enQueue(defaultMail()
+            .name("name1")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 1)
+            .build());
+
+        getManageableMailQueue().enQueue(defaultMail()
+            .name("name8")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 8)
+            .build());
+
+        getManageableMailQueue().enQueue(defaultMail()
+            .name("name6")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 6)
+            .build());
+
+        getManageableMailQueue().enQueue(defaultMail()
+            .name("name0")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 0)
+            .build());
+
+        getManageableMailQueue().enQueue(defaultMail()
+            .name("name7")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 7)
+            .build());
+
+        getManageableMailQueue().enQueue(defaultMail()
+            .name("name4")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 4)
+            .build());
+
+        getManageableMailQueue().enQueue(defaultMail()
+            .name("name2")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 2)
+            .build());
+
+        getManageableMailQueue().enQueue(defaultMail()
+            .name("name5")
+            .attribute(MailPrioritySupport.MAIL_PRIORITY, 5)
+            .build());
+
+        assertThat(getManageableMailQueue().browse())
+            .extracting(ManageableMailQueue.MailQueueItemView::getMail)
+            .extracting(Mail::getName)
+            .containsExactly("name9", "name8", "name7", "name6", "name5", 
"name4", "name3", "name2", "name1", "name0");
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/afe41b87/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 8f9e39c..d173e6c 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
@@ -29,8 +29,10 @@ 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.DelayedPriorityMailQueueContract;
 import org.apache.james.queue.api.MailQueue;
 import org.apache.james.queue.api.ManageableMailQueue;
+import org.apache.james.queue.api.PriorityManageableMailQueueContract;
 import org.apache.james.queue.api.RawMailQueueItemDecoratorFactory;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
@@ -39,7 +41,7 @@ import org.junit.jupiter.api.Test;
 
 import com.google.common.collect.ImmutableList;
 
-public class JMSMailQueueTest implements DelayedManageableMailQueueContract {
+public class JMSMailQueueTest implements DelayedManageableMailQueueContract, 
PriorityManageableMailQueueContract, DelayedPriorityMailQueueContract {
 
     private final static String QUEUE_NAME = "test";
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to