james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [1/9] james-project git commit: JAMES-2544 Use a manual clock
Date Tue, 02 Oct 2018 02:57:03 GMT
Repository: james-project
Updated Branches:
  refs/heads/master 436e8cde4 -> 88821f8b7


JAMES-2544 Use a manual clock

Allow specifying enqueue instant to change slice while enforcing different timestamps for
each mail.

This allows a smooth ordering.


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d165649a
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d165649a
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d165649a

Branch: refs/heads/master
Commit: d165649ab244ee77f73d80ae5ebcf2f74794fd74
Parents: 436e8cd
Author: Benoit Tellier <btellier@linagora.com>
Authored: Thu Sep 27 15:50:56 2018 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Tue Oct 2 09:19:38 2018 +0700

----------------------------------------------------------------------
 .../queue/rabbitmq/RabbitMQMailQueueTest.java   | 39 +++++--------
 .../queue/rabbitmq/UpdatableTickingClock.java   | 61 ++++++++++++++++++++
 2 files changed, 75 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d165649a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java
b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java
index d9c8156..f4e404f 100644
--- a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java
+++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java
@@ -22,24 +22,16 @@ package org.apache.james.queue.rabbitmq;
 import static java.time.temporal.ChronoUnit.HOURS;
 import static org.apache.james.queue.api.Mails.defaultMail;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.time.Clock;
 import java.time.Duration;
 import java.time.Instant;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ThreadLocalRandom;
-import java.util.concurrent.TimeoutException;
 import java.util.stream.IntStream;
 import java.util.stream.Stream;
 
 import javax.mail.internet.MimeMessage;
 
-import org.apache.http.client.utils.URIBuilder;
 import org.apache.james.backend.rabbitmq.DockerRabbitMQ;
 import org.apache.james.backend.rabbitmq.RabbitChannelPool;
 import org.apache.james.backend.rabbitmq.RabbitMQConfiguration;
@@ -94,15 +86,13 @@ public class RabbitMQMailQueueTest implements ManageableMailQueueContract,
MailQ
         CassandraMailQueueViewModule.MODULE));
 
     private RabbitMQMailQueueFactory mailQueueFactory;
-    private Clock clock;
+    private UpdatableTickingClock clock;
 
     @BeforeEach
-    void setup(DockerRabbitMQ rabbitMQ, CassandraCluster cassandra, MailQueueMetricExtension.MailQueueMetricTestSystem
metricTestSystem) throws IOException, TimeoutException, URISyntaxException {
+    void setup(DockerRabbitMQ rabbitMQ, CassandraCluster cassandra, MailQueueMetricExtension.MailQueueMetricTestSystem
metricTestSystem) throws Exception {
         CassandraBlobsDAO blobsDAO = new CassandraBlobsDAO(cassandra.getConf(), CassandraConfiguration.DEFAULT_CONFIGURATION,
BLOB_ID_FACTORY);
         Store<MimeMessage, MimeMessagePartsId> mimeMessageStore = MimeMessageStore.factory(blobsDAO).mimeMessageStore();
-
-        clock = mock(Clock.class);
-        when(clock.instant()).thenReturn(IN_SLICE_1);
+        clock = new UpdatableTickingClock(IN_SLICE_1);
         ThreadLocalRandom random = ThreadLocalRandom.current();
 
         MailQueueView mailQueueView = CassandraMailQueueViewTestFactory.factory(clock, random,
cassandra.getConf(), cassandra.getTypesProvider(),
@@ -128,7 +118,7 @@ public class RabbitMQMailQueueTest implements ManageableMailQueueContract,
MailQ
             mimeMessageStore,
             BLOB_ID_FACTORY,
             mailQueueView,
-            Clock.systemUTC());
+            clock);
         RabbitMQManagementApi mqManagementApi = new RabbitMQManagementApi(rabbitMQ.managementUri(),
new RabbitMQManagementCredentials("guest", "guest".toCharArray()));
         mailQueueFactory = new RabbitMQMailQueueFactory(rabbitClient, mqManagementApi, factory);
     }
@@ -148,19 +138,19 @@ public class RabbitMQMailQueueTest implements ManageableMailQueueContract,
MailQ
         ManageableMailQueue mailQueue = getManageableMailQueue();
         int emailCount = 5;
 
-        when(clock.instant()).thenReturn(IN_SLICE_1);
+        clock.setInstant(IN_SLICE_1);
         enqueueMailsInSlice(1, emailCount);
 
-        when(clock.instant()).thenReturn(IN_SLICE_2);
+        clock.setInstant(IN_SLICE_2);
         enqueueMailsInSlice(2, emailCount);
 
-        when(clock.instant()).thenReturn(IN_SLICE_3);
+        clock.setInstant(IN_SLICE_3);
         enqueueMailsInSlice(3, emailCount);
 
-        when(clock.instant()).thenReturn(IN_SLICE_5);
+        clock.setInstant(IN_SLICE_5);
         enqueueMailsInSlice(5, emailCount);
 
-        when(clock.instant()).thenReturn(IN_SLICE_6);
+        clock.setInstant(IN_SLICE_6);
         Stream<String> names = Iterators.toStream(mailQueue.browse())
             .map(ManageableMailQueue.MailQueueItemView::getMail)
             .map(Mail::getName);
@@ -177,20 +167,19 @@ public class RabbitMQMailQueueTest implements ManageableMailQueueContract,
MailQ
         ManageableMailQueue mailQueue = getManageableMailQueue();
         int emailCount = 5;
 
-        when(clock.instant()).thenReturn(IN_SLICE_1);
+        clock.setInstant(IN_SLICE_1);
         enqueueMailsInSlice(1, emailCount);
 
-        when(clock.instant()).thenReturn(IN_SLICE_2);
+        clock.setInstant(IN_SLICE_2);
         enqueueMailsInSlice(2, emailCount);
 
-        when(clock.instant()).thenReturn(IN_SLICE_3);
+        clock.setInstant(IN_SLICE_3);
         enqueueMailsInSlice(3, emailCount);
 
-        when(clock.instant()).thenReturn(IN_SLICE_5);
+        clock.setInstant(IN_SLICE_5);
         enqueueMailsInSlice(5, emailCount);
 
-        when(clock.instant()).thenReturn(IN_SLICE_6);
-
+        clock.setInstant(IN_SLICE_6);
         dequeueMails(5);
         dequeueMails(5);
         dequeueMails(3);

http://git-wip-us.apache.org/repos/asf/james-project/blob/d165649a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/UpdatableTickingClock.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/UpdatableTickingClock.java
b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/UpdatableTickingClock.java
new file mode 100644
index 0000000..cd22449
--- /dev/null
+++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/UpdatableTickingClock.java
@@ -0,0 +1,61 @@
+/****************************************************************
+ * 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.rabbitmq;
+
+import java.time.Clock;
+import java.time.Instant;
+import java.time.ZoneId;
+
+import org.apache.commons.lang3.NotImplementedException;
+
+public class UpdatableTickingClock extends Clock {
+    private Instant currentInstant;
+
+    public UpdatableTickingClock(Instant currentInstant) {
+        this.currentInstant = currentInstant;
+    }
+
+    public void setInstant(Instant instant) {
+        currentInstant = instant;
+    }
+
+    @Override
+    public ZoneId getZone() {
+        throw new NotImplementedException("No timezone attached to this clock");
+    }
+
+    @Override
+    public Clock withZone(ZoneId zone) {
+        throw new NotImplementedException("No timezone attached to this clock");
+    }
+
+    @Override
+    public synchronized Instant instant() {
+        try {
+            return currentInstant;
+        } finally {
+            tick();
+        }
+    }
+
+    private void tick() {
+        currentInstant = currentInstant.plusMillis(1);
+    }
+}


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