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 c8c12ae0c40aaee0ca72ec8685918a6b97278a82 Author: datph <[email protected]> AuthorDate: Fri Mar 29 15:20:14 2019 +0700 JAMES-2706 Refactor MetricableBlobStoreContract with non-mock metrics test --- server/blob/blob-api/pom.xml | 5 ++ .../blob/api/MetricableBlobStoreContract.java | 53 ++++++++-------------- server/blob/blob-cassandra/pom.xml | 5 ++ server/blob/blob-memory/pom.xml | 5 ++ server/blob/blob-objectstorage/pom.xml | 5 ++ 5 files changed, 39 insertions(+), 34 deletions(-) diff --git a/server/blob/blob-api/pom.xml b/server/blob/blob-api/pom.xml index ba94bbb..e10c992 100644 --- a/server/blob/blob-api/pom.xml +++ b/server/blob/blob-api/pom.xml @@ -42,6 +42,11 @@ <artifactId>metrics-api</artifactId> </dependency> <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>metrics-tests</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <scope>test</scope> diff --git a/server/blob/blob-api/src/test/java/org/apache/james/blob/api/MetricableBlobStoreContract.java b/server/blob/blob-api/src/test/java/org/apache/james/blob/api/MetricableBlobStoreContract.java index ec495ed..9b8cfb5 100644 --- a/server/blob/blob-api/src/test/java/org/apache/james/blob/api/MetricableBlobStoreContract.java +++ b/server/blob/blob-api/src/test/java/org/apache/james/blob/api/MetricableBlobStoreContract.java @@ -23,54 +23,32 @@ import static org.apache.james.blob.api.MetricableBlobStore.READ_BYTES_TIMER_NAM import static org.apache.james.blob.api.MetricableBlobStore.READ_TIMER_NAME; import static org.apache.james.blob.api.MetricableBlobStore.SAVE_BYTES_TIMER_NAME; import static org.apache.james.blob.api.MetricableBlobStore.SAVE_INPUT_STREAM_TIMER_NAME; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.assertj.core.api.Assertions.assertThat; import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; +import java.time.Duration; -import org.apache.james.metrics.api.MetricFactory; -import org.apache.james.metrics.api.TimeMetric; +import org.apache.james.metrics.tests.RecordingMetricFactory; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.RegisterExtension; + public interface MetricableBlobStoreContract extends BlobStoreContract { class MetricableBlobStoreExtension implements BeforeEachCallback { - private MetricFactory metricFactory; - private TimeMetric saveBytesTimeMetric; - private TimeMetric saveInputStreamTimeMetric; - private TimeMetric readBytesTimeMetric; - private TimeMetric readTimeMetric; + private RecordingMetricFactory metricFactory; @Override public void beforeEach(ExtensionContext extensionContext) { - this.metricFactory = spy(MetricFactory.class); - this.saveBytesTimeMetric = spy(TimeMetric.class); - this.saveInputStreamTimeMetric = spy(TimeMetric.class); - this.readBytesTimeMetric = spy(TimeMetric.class); - this.readTimeMetric = spy(TimeMetric.class); - setupExpectations(); + this.metricFactory = new RecordingMetricFactory(); } - public MetricFactory getMetricFactory() { + public RecordingMetricFactory getMetricFactory() { return metricFactory; } - - private void setupExpectations() { - when(metricFactory.timer(SAVE_BYTES_TIMER_NAME)) - .thenReturn(saveBytesTimeMetric); - when(metricFactory.timer(SAVE_INPUT_STREAM_TIMER_NAME)) - .thenReturn(saveInputStreamTimeMetric); - when(metricFactory.timer(READ_BYTES_TIMER_NAME)) - .thenReturn(readBytesTimeMetric); - when(metricFactory.timer(READ_TIMER_NAME)) - .thenReturn(readTimeMetric); - } } @RegisterExtension @@ -81,15 +59,18 @@ public interface MetricableBlobStoreContract extends BlobStoreContract { default void saveBytesShouldPublishSaveBytesTimerMetrics() { testee().save(BYTES_CONTENT).block(); testee().save(BYTES_CONTENT).block(); - verify(metricsTestExtension.saveBytesTimeMetric, times(2)).stopAndPublish(); + + assertThat(metricsTestExtension.getMetricFactory().executionTimesFor(SAVE_BYTES_TIMER_NAME)) + .hasSize(2); } @Test default void saveInputStreamShouldPublishSaveInputStreamTimerMetrics() { testee().save(new ByteArrayInputStream(BYTES_CONTENT), BYTES_CONTENT.length).block(); testee().save(new ByteArrayInputStream(BYTES_CONTENT), BYTES_CONTENT.length).block(); - testee().save(new ByteArrayInputStream(BYTES_CONTENT), BYTES_CONTENT.length).block(); - verify(metricsTestExtension.saveInputStreamTimeMetric, times(3)).stopAndPublish(); + + assertThat(metricsTestExtension.getMetricFactory().executionTimesFor(SAVE_INPUT_STREAM_TIMER_NAME)) + .hasSize(2); } @Test @@ -97,7 +78,9 @@ public interface MetricableBlobStoreContract extends BlobStoreContract { BlobId blobId = testee().save(BYTES_CONTENT).block(); testee().readBytes(blobId).block(); testee().readBytes(blobId).block(); - verify(metricsTestExtension.readBytesTimeMetric, times(2)).stopAndPublish(); + + assertThat(metricsTestExtension.getMetricFactory().executionTimesFor(READ_BYTES_TIMER_NAME)) + .hasSize(2); } @Test @@ -105,6 +88,8 @@ public interface MetricableBlobStoreContract extends BlobStoreContract { BlobId blobId = testee().save(BYTES_CONTENT).block(); testee().read(blobId); testee().read(blobId); - verify(metricsTestExtension.readTimeMetric, times(2)).stopAndPublish(); + + assertThat(metricsTestExtension.getMetricFactory().executionTimesFor(READ_TIMER_NAME)) + .hasSize(2); } } \ No newline at end of file diff --git a/server/blob/blob-cassandra/pom.xml b/server/blob/blob-cassandra/pom.xml index 8000779..ded0cf1 100644 --- a/server/blob/blob-cassandra/pom.xml +++ b/server/blob/blob-cassandra/pom.xml @@ -55,6 +55,11 @@ </dependency> <dependency> <groupId>${james.groupId}</groupId> + <artifactId>metrics-tests</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> <artifactId>james-server-util</artifactId> </dependency> <dependency> diff --git a/server/blob/blob-memory/pom.xml b/server/blob/blob-memory/pom.xml index d106c2c..99402fc 100644 --- a/server/blob/blob-memory/pom.xml +++ b/server/blob/blob-memory/pom.xml @@ -43,6 +43,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>metrics-tests</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> diff --git a/server/blob/blob-objectstorage/pom.xml b/server/blob/blob-objectstorage/pom.xml index 2cddbd9..fe8481d 100644 --- a/server/blob/blob-objectstorage/pom.xml +++ b/server/blob/blob-objectstorage/pom.xml @@ -49,6 +49,11 @@ </dependency> <dependency> <groupId>${james.groupId}</groupId> + <artifactId>metrics-tests</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> <artifactId>james-server-util</artifactId> </dependency> <dependency> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
