From 870e5fd482a994d11586e819e2b2ae596a741e10 Mon Sep 17 00:00:00 2001 From: Jialin Shao Date: Tue, 27 Oct 2020 20:43:18 -0500 Subject: [PATCH] Fix flaky test caused by MetricRegistryImpl --- .../monitoring/metrics/MetricRegistryImplTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/metrics/src/test/java/com/google/monitoring/metrics/MetricRegistryImplTest.java b/metrics/src/test/java/com/google/monitoring/metrics/MetricRegistryImplTest.java index b4ffb28..d09e1d6 100755 --- a/metrics/src/test/java/com/google/monitoring/metrics/MetricRegistryImplTest.java +++ b/metrics/src/test/java/com/google/monitoring/metrics/MetricRegistryImplTest.java @@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableSet; import com.google.monitoring.metrics.MetricSchema.Kind; import org.junit.After; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -38,6 +39,16 @@ public class MetricRegistryImplTest { private final LabelDescriptor label = LabelDescriptor.create("test_labelname", "test_labeldescription"); + /** + * Add {@code unregisterAllMetrics} before the class + * in case metrics not unregistered at the beginning + * and cause flaky test + */ + @BeforeClass + public static void beforeClassClearMetrics() { + MetricRegistryImpl.getDefault().unregisterAllMetrics(); + } + @After public void clearMetrics() { MetricRegistryImpl.getDefault().unregisterAllMetrics();