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 4c44a9ba96b8accb66fe2dc82efbdb840899c718 Author: duc91 <[email protected]> AuthorDate: Thu Jun 18 17:23:31 2020 +0700 JAMES:3170: add test about timerShouldAllowRecordingIndividualAndTotalTiming --- .../dropwizard/DropWizardMetricFactoryTest.java | 35 ++++++++++++++++++++++ metrics/metrics-tests/pom.xml | 5 ++++ .../metrics/tests/RecordingMetricFactoryTest.java | 18 +++++++---- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactoryTest.java b/metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactoryTest.java index 368075c..968095e 100644 --- a/metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactoryTest.java +++ b/metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactoryTest.java @@ -26,11 +26,13 @@ import java.time.temporal.ChronoUnit; import org.apache.james.metrics.api.MetricFactory; import org.apache.james.metrics.api.MetricFactoryContract; +import org.assertj.core.api.SoftAssertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import com.codahale.metrics.MetricRegistry; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; class DropWizardMetricFactoryTest implements MetricFactoryContract { @@ -68,4 +70,37 @@ class DropWizardMetricFactoryTest implements MetricFactoryContract { assertThat(testee.timer("any").getTimer().getSnapshot().get99thPercentile()) .isLessThan(duration.get(ChronoUnit.NANOS) * 2); } + + @Test + void timerShouldAllowRecordingIndividualAndTotalTiming() { + Duration duration = Duration.ofMillis(100); + Flux.from(testee.decoratePublisherWithTimerMetric("anyTotal", + Flux.from(testee.decoratePublisherWithTimerMetric("any", Mono.delay(duration))) + .repeat(5))) + .blockLast(); + + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(testee.timer("any").getTimer().getCount()) + .isEqualTo(6); + softly.assertThat(testee.timer("anyTotal").getTimer().getCount()) + .isEqualTo(1); + }); + } + + @Test + void timerShouldAllowRecordingIndividualAndTotalTimingWithP99() { + Duration duration = Duration.ofMillis(100); + Flux.from(testee.decoratePublisherWithTimerMetric("anyTotal", + Flux.from(testee.decoratePublisherWithTimerMetric("any", Mono.delay(duration))) + .repeat(5))) + .blockLast(); + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(testee.timer("any").getTimer().getSnapshot().get99thPercentile()) + .isLessThan(duration.get(ChronoUnit.NANOS) * 2) + .isGreaterThan(duration.get(ChronoUnit.NANOS)); + softly.assertThat(testee.timer("anyTotal").getTimer().getSnapshot().get99thPercentile()) + .isLessThan(duration.get(ChronoUnit.NANOS) * 7) + .isGreaterThan(duration.get(ChronoUnit.NANOS) * 6); + }); + } } \ No newline at end of file diff --git a/metrics/metrics-tests/pom.xml b/metrics/metrics-tests/pom.xml index c8971d6..de47e55 100644 --- a/metrics/metrics-tests/pom.xml +++ b/metrics/metrics-tests/pom.xml @@ -62,5 +62,10 @@ <groupId>io.projectreactor</groupId> <artifactId>reactor-core</artifactId> </dependency> + <dependency> + <groupId>org.awaitility</groupId> + <artifactId>awaitility</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> \ No newline at end of file diff --git a/metrics/metrics-tests/src/test/java/org/apache/james/metrics/tests/RecordingMetricFactoryTest.java b/metrics/metrics-tests/src/test/java/org/apache/james/metrics/tests/RecordingMetricFactoryTest.java index bbdcf92..b8d001f 100644 --- a/metrics/metrics-tests/src/test/java/org/apache/james/metrics/tests/RecordingMetricFactoryTest.java +++ b/metrics/metrics-tests/src/test/java/org/apache/james/metrics/tests/RecordingMetricFactoryTest.java @@ -22,6 +22,7 @@ package org.apache.james.metrics.tests; import static org.assertj.core.api.Assertions.assertThat; import java.time.Duration; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import org.apache.james.metrics.api.Metric; @@ -29,6 +30,8 @@ import org.apache.james.metrics.api.MetricFactory; import org.apache.james.metrics.api.MetricFactoryContract; import org.apache.james.metrics.api.TimeMetric; import org.apache.james.util.concurrency.ConcurrentTestRunner; +import org.awaitility.Awaitility; +import org.awaitility.core.ConditionFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -40,6 +43,7 @@ class RecordingMetricFactoryTest implements MetricFactoryContract { private static final String METRIC_NAME = "metric"; private static final java.time.Duration ONE_SECOND = java.time.Duration.ofSeconds(1); private static final java.time.Duration FIVE_SECONDS = java.time.Duration.ofSeconds(5); + private static final ConditionFactory WAIT_AT_MOST = Awaitility.waitAtMost(5, TimeUnit.SECONDS); private RecordingMetricFactory testee; @@ -128,9 +132,10 @@ class RecordingMetricFactoryTest implements MetricFactoryContract { .repeat(5) .blockLast(); - assertThat(testee.executionTimesFor("any")) - .hasSize(6) - .allSatisfy(timing -> assertThat(timing).isLessThan(duration.multipliedBy(2))); + WAIT_AT_MOST.untilAsserted(() -> + assertThat(testee.executionTimesFor("any")) + .hasSize(6) + .allSatisfy(timing -> assertThat(timing).isLessThan(duration.multipliedBy(2)))); } @Test @@ -140,8 +145,9 @@ class RecordingMetricFactoryTest implements MetricFactoryContract { .repeat(5) .blockLast(); - assertThat(testee.executionTimesFor("any")) - .hasSize(6) - .allSatisfy(timing -> assertThat(timing).isLessThan(duration.multipliedBy(2))); + WAIT_AT_MOST.untilAsserted(() -> + assertThat(testee.executionTimesFor("any")) + .hasSize(6) + .allSatisfy(timing -> assertThat(timing).isLessThan(duration.multipliedBy(2)))); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
