You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/12/19 05:40:35 UTC

[james-project] 01/08: JAMES-3007 Metric.getCounter() and tests suit

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit ef7b644d3c23066bd4f5470bd1520622d5576f74
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Wed Dec 11 18:23:45 2019 +0700

    JAMES-3007 Metric.getCounter() and tests suit
---
 metrics/metrics-api/pom.xml                        |   4 +
 .../java/org/apache/james/metrics/api/Metric.java  |   2 +
 .../apache/james/metrics/api/MetricContract.java   | 121 +++++++++++++++++++++
 metrics/metrics-dropwizard/pom.xml                 |   6 +
 .../james/metrics/dropwizard/DropWizardMetric.java |   5 +
 .../metrics/dropwizard/DropWizardMetricTest.java}  |  35 +++---
 metrics/metrics-logger/pom.xml                     |   6 +
 .../apache/james/metrics/logger/DefaultMetric.java |   5 +
 .../james/metrics/logger/DefaultMetricTest.java}   |  22 ++--
 metrics/metrics-tests/pom.xml                      |   6 +
 .../james/metrics/tests/RecordingMetric.java       |   5 +
 .../james/metrics/tests/RecordingMetricTest.java}  |  38 +++----
 pom.xml                                            |   6 +
 13 files changed, 208 insertions(+), 53 deletions(-)

diff --git a/metrics/metrics-api/pom.xml b/metrics/metrics-api/pom.xml
index 02919f4..3212965 100644
--- a/metrics/metrics-api/pom.xml
+++ b/metrics/metrics-api/pom.xml
@@ -39,6 +39,10 @@
             <groupId>io.projectreactor</groupId>
             <artifactId>reactor-core</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+        </dependency>
     </dependencies>
 
 </project>
diff --git a/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java b/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java
index fdf75b6..1b2e879 100644
--- a/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java
+++ b/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java
@@ -28,4 +28,6 @@ public interface Metric {
     void add(int value);
 
     void remove(int value);
+
+    long getCount();
 }
diff --git a/metrics/metrics-api/src/test/java/org/apache/james/metrics/api/MetricContract.java b/metrics/metrics-api/src/test/java/org/apache/james/metrics/api/MetricContract.java
new file mode 100644
index 0000000..4c6bf07
--- /dev/null
+++ b/metrics/metrics-api/src/test/java/org/apache/james/metrics/api/MetricContract.java
@@ -0,0 +1,121 @@
+/****************************************************************
+ * 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.metrics.api;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.Test;
+
+public interface MetricContract {
+
+    Metric testee();
+
+    @Test
+    default void incrementShouldIncreaseCounter() {
+        testee().increment();
+
+        assertThat(testee().getCount())
+            .isEqualTo(1);
+    }
+
+    @Test
+    default void incrementShouldIncreaseCounterAfterMultipleCalls() {
+        testee().increment();
+        testee().increment();
+        testee().increment();
+
+        assertThat(testee().getCount())
+            .isEqualTo(3);
+    }
+
+    @Test
+    default void decrementShouldDecreaseCounter() {
+        testee().decrement();
+
+        assertThat(testee().getCount())
+            .isEqualTo(-1);
+    }
+
+    @Test
+    default void decrementShouldDecreaseCounterAfterMultipleCalls() {
+        testee().decrement();
+        testee().decrement();
+        testee().decrement();
+
+        assertThat(testee().getCount())
+            .isEqualTo(-3);
+    }
+
+    @Test
+    default void addShouldIncreaseCounter() {
+        testee().add(10);
+
+        assertThat(testee().getCount())
+            .isEqualTo(10);
+    }
+
+    @Test
+    default void addShouldDecreaseCounterWhenNegativeNumber() {
+        testee().add(-9);
+
+        assertThat(testee().getCount())
+            .isEqualTo(-9);
+    }
+
+    @Test
+    default void addShouldKeepCounterBeTheSameWhenZero() {
+        testee().add(10);
+        testee().add(0);
+
+        assertThat(testee().getCount())
+            .isEqualTo(10);
+    }
+
+    @Test
+    default void removeShouldDecreaseCounter() {
+        testee().remove(10);
+
+        assertThat(testee().getCount())
+            .isEqualTo(-10);
+    }
+
+    @Test
+    default void removeShouldIncreaseCounterWhenNegativeNumber() {
+        testee().remove(-9);
+
+        assertThat(testee().getCount())
+            .isEqualTo(9);
+    }
+
+    @Test
+    default void removeShouldKeepCounterBeTheSameWhenZero() {
+        testee().remove(888);
+        testee().remove(0);
+
+        assertThat(testee().getCount())
+            .isEqualTo(-888);
+    }
+
+    @Test
+    default void getCountShouldReturnZeroWhenNoUpdate() {
+        assertThat(testee().getCount())
+            .isEqualTo(0);
+    }
+}
\ No newline at end of file
diff --git a/metrics/metrics-dropwizard/pom.xml b/metrics/metrics-dropwizard/pom.xml
index 9670a16..d0be914 100644
--- a/metrics/metrics-dropwizard/pom.xml
+++ b/metrics/metrics-dropwizard/pom.xml
@@ -36,6 +36,12 @@
         </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
+            <artifactId>metrics-api</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
             <artifactId>james-server-lifecycle-api</artifactId>
         </dependency>
         <dependency>
diff --git a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java b/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
index 3aef262..21a192d 100644
--- a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
+++ b/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
@@ -50,4 +50,9 @@ public class DropWizardMetric implements Metric {
     public void remove(int value) {
         counter.dec(value);
     }
+
+    @Override
+    public long getCount() {
+        return counter.getCount();
+    }
 }
diff --git a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java b/metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricTest.java
similarity index 72%
copy from metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
copy to metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricTest.java
index 3aef262..1183bc4 100644
--- a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
+++ b/metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricTest.java
@@ -20,34 +20,25 @@
 package org.apache.james.metrics.dropwizard;
 
 import org.apache.james.metrics.api.Metric;
+import org.apache.james.metrics.api.MetricContract;
+import org.junit.jupiter.api.BeforeEach;
 
-import com.codahale.metrics.Counter;
+import com.codahale.metrics.MetricRegistry;
 
-public class DropWizardMetric implements Metric {
+class DropWizardMetricTest implements MetricContract {
 
-    private final Counter counter;
+    private static final String METRIC_NAME = "myMetric";
 
-    public DropWizardMetric(Counter counter) {
-        this.counter = counter;
-    }
-
-    @Override
-    public void increment() {
-        counter.inc();
-    }
+    private DropWizardMetric testee;
 
-    @Override
-    public void decrement() {
-        counter.dec();
-    }
-
-    @Override
-    public void add(int value) {
-        counter.inc(value);
+    @BeforeEach
+    void setUp() {
+        MetricRegistry registry = new MetricRegistry();
+        testee = new DropWizardMetric(registry.counter(METRIC_NAME));
     }
 
     @Override
-    public void remove(int value) {
-        counter.dec(value);
+    public Metric testee() {
+        return testee;
     }
-}
+}
\ No newline at end of file
diff --git a/metrics/metrics-logger/pom.xml b/metrics/metrics-logger/pom.xml
index 4354fad..c843ac1 100644
--- a/metrics/metrics-logger/pom.xml
+++ b/metrics/metrics-logger/pom.xml
@@ -36,6 +36,12 @@
         </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
+            <artifactId>metrics-api</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
             <artifactId>testing-base</artifactId>
             <scope>test</scope>
         </dependency>
diff --git a/metrics/metrics-logger/src/main/java/org/apache/james/metrics/logger/DefaultMetric.java b/metrics/metrics-logger/src/main/java/org/apache/james/metrics/logger/DefaultMetric.java
index 90697fa..ed4d7d3 100644
--- a/metrics/metrics-logger/src/main/java/org/apache/james/metrics/logger/DefaultMetric.java
+++ b/metrics/metrics-logger/src/main/java/org/apache/james/metrics/logger/DefaultMetric.java
@@ -49,4 +49,9 @@ public class DefaultMetric implements Metric {
     public void remove(int i) {
         value.addAndGet(-1 * i);
     }
+
+    @Override
+    public long getCount() {
+        return value.get();
+    }
 }
diff --git a/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java b/metrics/metrics-logger/src/test/java/org/apache/james/metrics/logger/DefaultMetricTest.java
similarity index 74%
copy from metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java
copy to metrics/metrics-logger/src/test/java/org/apache/james/metrics/logger/DefaultMetricTest.java
index fdf75b6..3ab0d6d 100644
--- a/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java
+++ b/metrics/metrics-logger/src/test/java/org/apache/james/metrics/logger/DefaultMetricTest.java
@@ -17,15 +17,23 @@
  * under the License.                                           *
  ****************************************************************/
 
-package org.apache.james.metrics.api;
+package org.apache.james.metrics.logger;
 
-public interface Metric {
+import org.apache.james.metrics.api.Metric;
+import org.apache.james.metrics.api.MetricContract;
+import org.junit.jupiter.api.BeforeEach;
 
-    void increment();
+class DefaultMetricTest implements MetricContract {
 
-    void decrement();
+    private DefaultMetric testee;
 
-    void add(int value);
+    @BeforeEach
+    void setUp() {
+        testee = new DefaultMetric();
+    }
 
-    void remove(int value);
-}
+    @Override
+    public Metric testee() {
+        return testee;
+    }
+}
\ No newline at end of file
diff --git a/metrics/metrics-tests/pom.xml b/metrics/metrics-tests/pom.xml
index 5408ba9..987b840 100644
--- a/metrics/metrics-tests/pom.xml
+++ b/metrics/metrics-tests/pom.xml
@@ -41,6 +41,12 @@
         </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
+            <artifactId>metrics-api</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
             <artifactId>testing-base</artifactId>
             <scope>test</scope>
         </dependency>
diff --git a/metrics/metrics-tests/src/main/java/org/apache/james/metrics/tests/RecordingMetric.java b/metrics/metrics-tests/src/main/java/org/apache/james/metrics/tests/RecordingMetric.java
index 74b9570..78baea4 100644
--- a/metrics/metrics-tests/src/main/java/org/apache/james/metrics/tests/RecordingMetric.java
+++ b/metrics/metrics-tests/src/main/java/org/apache/james/metrics/tests/RecordingMetric.java
@@ -52,4 +52,9 @@ public class RecordingMetric implements Metric {
     public void remove(int i) {
         publishCallback.accept(value.addAndGet(-1 * i));
     }
+
+    @Override
+    public long getCount() {
+        return value.get();
+    }
 }
diff --git a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java b/metrics/metrics-tests/src/test/java/org/apache/james/metrics/tests/RecordingMetricTest.java
similarity index 70%
copy from metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
copy to metrics/metrics-tests/src/test/java/org/apache/james/metrics/tests/RecordingMetricTest.java
index 3aef262..8b45440 100644
--- a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
+++ b/metrics/metrics-tests/src/test/java/org/apache/james/metrics/tests/RecordingMetricTest.java
@@ -17,37 +17,27 @@
  * under the License.                                           *
  ****************************************************************/
 
-package org.apache.james.metrics.dropwizard;
+package org.apache.james.metrics.tests;
 
-import org.apache.james.metrics.api.Metric;
-
-import com.codahale.metrics.Counter;
-
-public class DropWizardMetric implements Metric {
+import java.util.function.Consumer;
 
-    private final Counter counter;
+import org.apache.james.metrics.api.Metric;
+import org.apache.james.metrics.api.MetricContract;
+import org.junit.jupiter.api.BeforeEach;
 
-    public DropWizardMetric(Counter counter) {
-        this.counter = counter;
-    }
+class RecordingMetricTest implements MetricContract {
 
-    @Override
-    public void increment() {
-        counter.inc();
-    }
+    private static final Consumer<Integer> NO_CALLBACK = counterValue -> {};
 
-    @Override
-    public void decrement() {
-        counter.dec();
-    }
+    private RecordingMetric testee;
 
-    @Override
-    public void add(int value) {
-        counter.inc(value);
+    @BeforeEach
+    void setUp() {
+        testee = new RecordingMetric(NO_CALLBACK);
     }
 
     @Override
-    public void remove(int value) {
-        counter.dec(value);
+    public Metric testee() {
+        return testee;
     }
-}
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 11464b0..e60eb90 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1859,6 +1859,12 @@
             </dependency>
             <dependency>
                 <groupId>${james.groupId}</groupId>
+                <artifactId>metrics-api</artifactId>
+                <version>${project.version}</version>
+                <type>test-jar</type>
+            </dependency>
+            <dependency>
+                <groupId>${james.groupId}</groupId>
                 <artifactId>metrics-dropwizard</artifactId>
                 <version>${project.version}</version>
             </dependency>


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org