james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [2/3] james-project git commit: JAMES-2540 adding timer metric contract tests
Date Fri, 07 Sep 2018 06:27:49 GMT
JAMES-2540 adding timer metric contract tests


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

Branch: refs/heads/master
Commit: 93981f0c3a0584c002af19892cf71b6d8aa04e3a
Parents: 2c8c049
Author: duc <dtran@linagora.com>
Authored: Thu Sep 6 11:20:49 2018 +0700
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Fri Sep 7 08:22:31 2018 +0200

----------------------------------------------------------------------
 .../queue/api/MailQueueMetricContract.java      | 54 ++++++++++++++++----
 .../queue/api/MailQueueMetricExtension.java     | 48 ++++++++++++-----
 .../apache/james/queue/jms/JMSMailQueue.java    |  8 +--
 3 files changed, 84 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/93981f0c/server/queue/queue-api/src/test/java/org/apache/james/queue/api/MailQueueMetricContract.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/MailQueueMetricContract.java
b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/MailQueueMetricContract.java
index 9749456..307ac63 100644
--- a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/MailQueueMetricContract.java
+++ b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/MailQueueMetricContract.java
@@ -23,6 +23,7 @@ import static org.apache.james.queue.api.Mails.defaultMail;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 
 import java.util.stream.IntStream;
 
@@ -75,16 +76,16 @@ public interface MailQueueMetricContract extends MailQueueContract {
     default void enqueueShouldIncreaseEnQueueMetric(MailQueueMetricExtension.MailQueueMetricTestSystem
testSystem) throws Exception {
         enQueueMail(2);
 
-        verify(testSystem.getMockEnqueuedMailsMetric(), times(2)).increment();
-        Mockito.verifyNoMoreInteractions(testSystem.getMockEnqueuedMailsMetric());
+        verify(testSystem.getSpyEnqueuedMailsMetric(), times(2)).increment();
+        Mockito.verifyNoMoreInteractions(testSystem.getSpyEnqueuedMailsMetric());
     }
 
     @Test
     default void enqueueShouldNotTouchDequeueMetric(MailQueueMetricExtension.MailQueueMetricTestSystem
testSystem) throws Exception {
         enQueueMail(2);
 
-        verify(testSystem.getMockEnqueuedMailsMetric(), times(2)).increment();
-        Mockito.verifyNoMoreInteractions(testSystem.getMockDequeuedMailsMetric());
+        verify(testSystem.getSpyEnqueuedMailsMetric(), times(2)).increment();
+        Mockito.verifyNoMoreInteractions(testSystem.getSpyDequeuedMailsMetric());
     }
 
     @Test
@@ -92,8 +93,8 @@ public interface MailQueueMetricContract extends MailQueueContract {
         enQueueMail(2);
         deQueueMail(2);
 
-        verify(testSystem.getMockDequeuedMailsMetric(), times(2)).increment();
-        Mockito.verifyNoMoreInteractions(testSystem.getMockDequeuedMailsMetric());
+        verify(testSystem.getSpyDequeuedMailsMetric(), times(2)).increment();
+        Mockito.verifyNoMoreInteractions(testSystem.getSpyDequeuedMailsMetric());
     }
 
     @Test
@@ -101,10 +102,43 @@ public interface MailQueueMetricContract extends MailQueueContract {
         enQueueMail(2);
         deQueueMail(2);
 
-        verify(testSystem.getMockDequeuedMailsMetric(), times(2)).increment();
-        Mockito.verifyNoMoreInteractions(testSystem.getMockDequeuedMailsMetric());
+        verify(testSystem.getSpyDequeuedMailsMetric(), times(2)).increment();
+        Mockito.verifyNoMoreInteractions(testSystem.getSpyDequeuedMailsMetric());
 
-        verify(testSystem.getMockEnqueuedMailsMetric(), times(2)).increment();
-        Mockito.verifyNoMoreInteractions(testSystem.getMockEnqueuedMailsMetric());
+        verify(testSystem.getSpyEnqueuedMailsMetric(), times(2)).increment();
+        Mockito.verifyNoMoreInteractions(testSystem.getSpyEnqueuedMailsMetric());
     }
+
+    @Test
+    default void enqueueShouldPublishEnqueueTimeMetric(MailQueueMetricExtension.MailQueueMetricTestSystem
testSystem) throws Exception {
+        enQueueMail(2);
+
+        verify(testSystem.getSpyEnqueuedMailsTimeMetric(), times(2)).stopAndPublish();
+    }
+
+    @Test
+    default void enqueueShouldNotPublishDequeueTimeMetric(MailQueueMetricExtension.MailQueueMetricTestSystem
testSystem) throws Exception {
+        enQueueMail(2);
+
+        verify(testSystem.getSpyEnqueuedMailsTimeMetric(), times(2)).stopAndPublish();
+        verifyNoMoreInteractions(testSystem.getSpyDequeuedMailsTimeMetric());
+    }
+    @Test
+    default void dequeueShouldPublishDequeueTimeMetric(MailQueueMetricExtension.MailQueueMetricTestSystem
testSystem) throws Exception {
+        enQueueMail(2);
+        deQueueMail(2);
+
+        verify(testSystem.getSpyDequeuedMailsTimeMetric(), times(2)).stopAndPublish();
+    }
+
+    @Test
+    default void dequeueShouldNotPublishEnqueueTimeMetric(MailQueueMetricExtension.MailQueueMetricTestSystem
testSystem) throws Exception {
+        enQueueMail(2);
+        verify(testSystem.getSpyEnqueuedMailsTimeMetric(), times(2)).stopAndPublish();
+
+        deQueueMail(2);
+        verify(testSystem.getSpyDequeuedMailsTimeMetric(), times(2)).stopAndPublish();
+        verifyNoMoreInteractions(testSystem.getSpyEnqueuedMailsTimeMetric());
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/93981f0c/server/queue/queue-api/src/test/java/org/apache/james/queue/api/MailQueueMetricExtension.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/MailQueueMetricExtension.java
b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/MailQueueMetricExtension.java
index d4ea362..b214c25 100644
--- a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/MailQueueMetricExtension.java
+++ b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/MailQueueMetricExtension.java
@@ -19,8 +19,12 @@
 
 package org.apache.james.queue.api;
 
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.mock;
+import static org.apache.james.queue.api.MailQueue.DEQUEUED_METRIC_NAME_PREFIX;
+import static org.apache.james.queue.api.MailQueue.DEQUEUED_TIMER_METRIC_NAME_PREFIX;
+import static org.apache.james.queue.api.MailQueue.ENQUEUED_METRIC_NAME_PREFIX;
+import static org.apache.james.queue.api.MailQueue.ENQUEUED_TIMER_METRIC_NAME_PREFIX;
+import static org.mockito.ArgumentMatchers.startsWith;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
 import org.apache.james.metrics.api.GaugeRegistry;
@@ -28,6 +32,7 @@ import org.apache.james.metrics.api.Metric;
 import org.apache.james.metrics.api.MetricFactory;
 import org.apache.james.metrics.api.NoopGaugeRegistry;
 import org.apache.james.metrics.api.NoopMetricFactory;
+import org.apache.james.metrics.api.TimeMetric;
 import org.junit.jupiter.api.extension.BeforeEachCallback;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.junit.jupiter.api.extension.ParameterContext;
@@ -38,24 +43,28 @@ import org.mockito.Mockito;
 public class MailQueueMetricExtension implements BeforeEachCallback, ParameterResolver {
 
     public class MailQueueMetricTestSystem {
-        private final Metric mockEnqueuedMailsMetric;
-        private final Metric mockDequeuedMailsMetric;
+        private final Metric spyEnqueuedMailsMetric;
+        private final Metric spyDequeuedMailsMetric;
+        private final TimeMetric spyEnqueuedMailsTimeMetric;
+        private final TimeMetric spyDequeuedMailsTimeMetric;
         private final GaugeRegistry spyGaugeRegistry;
         private final MetricFactory spyMetricFactory;
 
         public MailQueueMetricTestSystem() {
-            mockEnqueuedMailsMetric = mock(Metric.class);
-            mockDequeuedMailsMetric = mock(Metric.class);
+            spyEnqueuedMailsMetric = spy(new NoopMetricFactory.NoopMetric());
+            spyDequeuedMailsMetric = spy(new NoopMetricFactory.NoopMetric());
+            spyEnqueuedMailsTimeMetric = spy(new NoopMetricFactory.NoopTimeMetric());
+            spyDequeuedMailsTimeMetric = spy(new NoopMetricFactory.NoopTimeMetric());
             spyGaugeRegistry = Mockito.spy(new NoopGaugeRegistry());
             spyMetricFactory = Mockito.spy(new NoopMetricFactory());
         }
 
-        public Metric getMockEnqueuedMailsMetric() {
-            return mockEnqueuedMailsMetric;
+        public Metric getSpyEnqueuedMailsMetric() {
+            return spyEnqueuedMailsMetric;
         }
 
-        public Metric getMockDequeuedMailsMetric() {
-            return mockDequeuedMailsMetric;
+        public Metric getSpyDequeuedMailsMetric() {
+            return spyDequeuedMailsMetric;
         }
 
         public GaugeRegistry getSpyGaugeRegistry() {
@@ -65,6 +74,14 @@ public class MailQueueMetricExtension implements BeforeEachCallback, ParameterRe
         public MetricFactory getSpyMetricFactory() {
             return spyMetricFactory;
         }
+
+        public TimeMetric getSpyEnqueuedMailsTimeMetric() {
+            return spyEnqueuedMailsTimeMetric;
+        }
+
+        public TimeMetric getSpyDequeuedMailsTimeMetric() {
+            return spyDequeuedMailsTimeMetric;
+        }
     }
 
     private MailQueueMetricTestSystem testSystem;
@@ -73,8 +90,15 @@ public class MailQueueMetricExtension implements BeforeEachCallback, ParameterRe
     public void beforeEach(ExtensionContext extensionContext) throws Exception {
         testSystem = new MailQueueMetricTestSystem();
 
-        when(testSystem.spyMetricFactory.generate(anyString()))
-            .thenReturn(testSystem.mockEnqueuedMailsMetric, testSystem.mockDequeuedMailsMetric);
+        when(testSystem.spyMetricFactory.generate(startsWith(ENQUEUED_METRIC_NAME_PREFIX)))
+            .thenReturn(testSystem.spyEnqueuedMailsMetric);
+        when(testSystem.spyMetricFactory.generate(startsWith(DEQUEUED_METRIC_NAME_PREFIX)))
+            .thenReturn(testSystem.spyDequeuedMailsMetric);
+
+        when(testSystem.spyMetricFactory.timer(startsWith(ENQUEUED_TIMER_METRIC_NAME_PREFIX)))
+            .thenReturn(testSystem.spyEnqueuedMailsTimeMetric);
+        when(testSystem.spyMetricFactory.timer(startsWith(DEQUEUED_TIMER_METRIC_NAME_PREFIX)))
+            .thenReturn(testSystem.spyDequeuedMailsTimeMetric);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/93981f0c/server/queue/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
----------------------------------------------------------------------
diff --git a/server/queue/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
b/server/queue/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
index 94092c2..4f0496a 100644
--- a/server/queue/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
+++ b/server/queue/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
@@ -168,8 +168,8 @@ public class JMSMailQueue implements ManageableMailQueue, JMSSupport,
MailPriori
         this.mailQueueItemDecoratorFactory = mailQueueItemDecoratorFactory;
         this.queueName = queueName;
         this.metricFactory = metricFactory;
-        this.enqueuedMailsMetric = metricFactory.generate("enqueuedMail:" + queueName);
-        this.dequeuedMailsMetric = metricFactory.generate("dequeuedMail:" + queueName);
+        this.enqueuedMailsMetric = metricFactory.generate(ENQUEUED_METRIC_NAME_PREFIX + queueName);
+        this.dequeuedMailsMetric = metricFactory.generate(DEQUEUED_METRIC_NAME_PREFIX + queueName);
 
         this.gaugeRegistry = gaugeRegistry;
         this.gaugeRegistry.register(QUEUE_SIZE_METRIC_NAME_PREFIX + queueName, queueSizeGauge());
@@ -210,7 +210,7 @@ public class JMSMailQueue implements ManageableMailQueue, JMSSupport,
MailPriori
         MessageConsumer consumer = null;
 
         while (true) {
-            TimeMetric timeMetric = metricFactory.timer("dequeueTime:" + queueName);
+            TimeMetric timeMetric = metricFactory.timer(DEQUEUED_TIMER_METRIC_NAME_PREFIX
+ queueName);
             try {
                 session = connection.createSession(true, Session.SESSION_TRANSACTED);
                 Queue queue = session.createQueue(queueName);
@@ -240,7 +240,7 @@ public class JMSMailQueue implements ManageableMailQueue, JMSSupport,
MailPriori
 
     @Override
     public void enQueue(Mail mail, long delay, TimeUnit unit) throws MailQueueException {
-        TimeMetric timeMetric = metricFactory.timer("enqueueMailTime:" + queueName);
+        TimeMetric timeMetric = metricFactory.timer(ENQUEUED_TIMER_METRIC_NAME_PREFIX + queueName);
 
         long nextDeliveryTimestamp = computeNextDeliveryTimestamp(delay, unit);
 


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