From e39cc0411046dcb3399f76cb76f91c511b608001 Mon Sep 17 00:00:00 2001 From: jianglai Date: Mon, 22 Oct 2018 12:10:06 -0700 Subject: [PATCH] Test AsyncFlowMetrics without mock Instead of verifying interactions on the mocks, we instead assert on the real test subject directly. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=218209556 --- .../flows/async/AsyncFlowMetrics.java | 10 ++--- javatests/google/registry/flows/BUILD | 1 + .../flows/async/AsyncFlowMetricsTest.java | 45 ++++++------------- 3 files changed, 17 insertions(+), 39 deletions(-) diff --git a/java/google/registry/flows/async/AsyncFlowMetrics.java b/java/google/registry/flows/async/AsyncFlowMetrics.java index c62fa4a8e..3cdda6e62 100644 --- a/java/google/registry/flows/async/AsyncFlowMetrics.java +++ b/java/google/registry/flows/async/AsyncFlowMetrics.java @@ -29,7 +29,6 @@ import com.google.monitoring.metrics.IncrementableMetric; import com.google.monitoring.metrics.LabelDescriptor; import com.google.monitoring.metrics.MetricRegistryImpl; import google.registry.util.Clock; -import google.registry.util.NonFinalForTesting; import javax.inject.Inject; import org.joda.time.DateTime; import org.joda.time.Duration; @@ -67,9 +66,8 @@ public class AsyncFlowMetrics { LabelDescriptor.create("operation_type", "The type of async flow operation."), LabelDescriptor.create("result", "The result of the async flow operation.")); - @NonFinalForTesting @VisibleForTesting - static IncrementableMetric asyncFlowOperationCounts = + static final IncrementableMetric asyncFlowOperationCounts = MetricRegistryImpl.getDefault() .newIncrementableMetric( "/async_flows/operations", @@ -77,9 +75,8 @@ public class AsyncFlowMetrics { "count", LABEL_DESCRIPTORS); - @NonFinalForTesting @VisibleForTesting - static EventMetric asyncFlowOperationProcessingTime = + static final EventMetric asyncFlowOperationProcessingTime = MetricRegistryImpl.getDefault() .newEventMetric( "/async_flows/processing_time", @@ -88,9 +85,8 @@ public class AsyncFlowMetrics { LABEL_DESCRIPTORS, DEFAULT_FITTER); - @NonFinalForTesting @VisibleForTesting - static EventMetric asyncFlowBatchSize = + static final EventMetric asyncFlowBatchSize = MetricRegistryImpl.getDefault() .newEventMetric( "/async_flows/batch_size", diff --git a/javatests/google/registry/flows/BUILD b/javatests/google/registry/flows/BUILD index f23f40b85..083dfd71b 100644 --- a/javatests/google/registry/flows/BUILD +++ b/javatests/google/registry/flows/BUILD @@ -47,6 +47,7 @@ java_library( "@com_google_flogger_system_backend", "@com_google_guava", "@com_google_guava_testlib", + "@com_google_monitoring_client_contrib", "@com_google_monitoring_client_metrics", "@com_google_re2j", "@com_google_truth", diff --git a/javatests/google/registry/flows/async/AsyncFlowMetricsTest.java b/javatests/google/registry/flows/async/AsyncFlowMetricsTest.java index 88f39e2b7..728f6de8d 100644 --- a/javatests/google/registry/flows/async/AsyncFlowMetricsTest.java +++ b/javatests/google/registry/flows/async/AsyncFlowMetricsTest.java @@ -14,19 +14,14 @@ package google.registry.flows.async; +import static com.google.monitoring.metrics.contrib.DistributionMetricSubject.assertThat; +import static com.google.monitoring.metrics.contrib.LongMetricSubject.assertThat; import static google.registry.flows.async.AsyncFlowMetrics.OperationResult.SUCCESS; import static google.registry.flows.async.AsyncFlowMetrics.OperationType.CONTACT_AND_HOST_DELETE; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import com.google.monitoring.metrics.EventMetric; -import com.google.monitoring.metrics.IncrementableMetric; +import com.google.common.collect.ImmutableSet; import google.registry.testing.FakeClock; -import google.registry.testing.InjectRule; import google.registry.testing.ShardableTestCase; -import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -35,26 +30,8 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class AsyncFlowMetricsTest extends ShardableTestCase { - @Rule public final InjectRule inject = new InjectRule(); - - private final IncrementableMetric asyncFlowOperationCounts = mock(IncrementableMetric.class); - private final EventMetric asyncFlowOperationProcessingTime = mock(EventMetric.class); - private final EventMetric asyncFlowBatchSize = mock(EventMetric.class); - private AsyncFlowMetrics asyncFlowMetrics; - private FakeClock clock; - - @Before - public void setUp() { - clock = new FakeClock(); - asyncFlowMetrics = new AsyncFlowMetrics(clock); - inject.setStaticField( - AsyncFlowMetrics.class, "asyncFlowOperationCounts", asyncFlowOperationCounts); - inject.setStaticField( - AsyncFlowMetrics.class, - "asyncFlowOperationProcessingTime", - asyncFlowOperationProcessingTime); - inject.setStaticField(AsyncFlowMetrics.class, "asyncFlowBatchSize", asyncFlowBatchSize); - } + private final FakeClock clock = new FakeClock(); + private final AsyncFlowMetrics asyncFlowMetrics = new AsyncFlowMetrics(clock); @Test public void testRecordAsyncFlowResult_calculatesDurationMillisCorrectly() { @@ -62,9 +39,13 @@ public class AsyncFlowMetricsTest extends ShardableTestCase { CONTACT_AND_HOST_DELETE, SUCCESS, clock.nowUtc().minusMinutes(10).minusSeconds(5).minusMillis(566)); - verify(asyncFlowOperationCounts).increment("contactAndHostDelete", "success"); - verify(asyncFlowOperationProcessingTime).record(605566.0, "contactAndHostDelete", "success"); - verifyNoMoreInteractions(asyncFlowOperationCounts); - verifyNoMoreInteractions(asyncFlowOperationProcessingTime); + assertThat(AsyncFlowMetrics.asyncFlowOperationCounts) + .hasValueForLabels(1, "contactAndHostDelete", "success") + .and() + .hasNoOtherValues(); + assertThat(AsyncFlowMetrics.asyncFlowOperationProcessingTime) + .hasDataSetForLabels(ImmutableSet.of(605566.0), "contactAndHostDelete", "success") + .and() + .hasNoOtherValues(); } }