From 0d1fe120c81b80b0cd1bdcfb64c0d590e14d6260 Mon Sep 17 00:00:00 2001 From: Todd Lipcon Date: Fri, 7 Oct 2011 21:54:38 +0000 Subject: [PATCH] HADOOP-7627. Improve MetricsAsserts to give more understandable output on failure. Contributed by Todd Lipcon. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1180259 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 + .../apache/hadoop/test/MetricsAsserts.java | 71 ++++++++++++++++--- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 67875e3d01..ba60ce6132 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -442,6 +442,9 @@ Release 0.23.0 - Unreleased HADOOP-7624. Set things up for a top level hadoop-tools module. (tucu) + HADOOP-7627. Improve MetricsAsserts to give more understandable output + on failure. (todd) + OPTIMIZATIONS HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MetricsAsserts.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MetricsAsserts.java index 47b91e20ab..f4bcb499e4 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MetricsAsserts.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MetricsAsserts.java @@ -21,11 +21,14 @@ import static com.google.common.base.Preconditions.*; import org.hamcrest.Description; +import org.junit.Assert; import static org.mockito.Mockito.*; import org.mockito.stubbing.Answer; +import org.mockito.internal.matchers.GreaterThan; import org.mockito.invocation.InvocationOnMock; -import static org.mockito.AdditionalMatchers.*; + +import org.mockito.ArgumentCaptor; import org.mockito.ArgumentMatcher; import org.apache.commons.logging.Log; @@ -44,6 +47,7 @@ public class MetricsAsserts { final static Log LOG = LogFactory.getLog(MetricsAsserts.class); + private static final double EPSILON = 0.00001; public static MetricsSystem mockMetricsSystem() { MetricsSystem ms = mock(MetricsSystem.class); @@ -139,7 +143,15 @@ public static MetricsInfo anyInfo() { */ public static void assertGauge(String name, int expected, MetricsRecordBuilder rb) { - verify(rb).addGauge(eqName(info(name, "")), eq(expected)); + Assert.assertEquals("Bad value for metric " + name, + expected, getIntGauge(name, rb)); + } + + public static int getIntGauge(String name, MetricsRecordBuilder rb) { + ArgumentCaptor captor = ArgumentCaptor.forClass(Integer.class); + verify(rb, atLeast(0)).addGauge(eqName(info(name, "")), captor.capture()); + checkCaptured(captor, name); + return captor.getValue(); } /** @@ -150,9 +162,18 @@ public static void assertGauge(String name, int expected, */ public static void assertCounter(String name, int expected, MetricsRecordBuilder rb) { - verify(rb).addCounter(eqName(info(name, "")), eq(expected)); + Assert.assertEquals("Bad value for metric " + name, + expected, getIntCounter(name, rb)); } + public static int getIntCounter(String name, MetricsRecordBuilder rb) { + ArgumentCaptor captor = ArgumentCaptor.forClass( + Integer.class); + verify(rb, atLeast(0)).addCounter(eqName(info(name, "")), captor.capture()); + checkCaptured(captor, name); + return captor.getValue(); + } + /** * Assert a long gauge metric as expected * @param name of the metric @@ -161,7 +182,15 @@ public static void assertCounter(String name, int expected, */ public static void assertGauge(String name, long expected, MetricsRecordBuilder rb) { - verify(rb).addGauge(eqName(info(name, "")), eq(expected)); + Assert.assertEquals("Bad value for metric " + name, + expected, getLongGauge(name, rb)); + } + + public static long getLongGauge(String name, MetricsRecordBuilder rb) { + ArgumentCaptor captor = ArgumentCaptor.forClass(Long.class); + verify(rb, atLeast(0)).addGauge(eqName(info(name, "")), captor.capture()); + checkCaptured(captor, name); + return captor.getValue(); } /** @@ -172,7 +201,15 @@ public static void assertGauge(String name, long expected, */ public static void assertGauge(String name, double expected, MetricsRecordBuilder rb) { - verify(rb).addGauge(eqName(info(name, "")), eq(expected)); + Assert.assertEquals("Bad value for metric " + name, + expected, getDoubleGauge(name, rb), EPSILON); + } + + public static double getDoubleGauge(String name, MetricsRecordBuilder rb) { + ArgumentCaptor captor = ArgumentCaptor.forClass(Double.class); + verify(rb, atLeast(0)).addGauge(eqName(info(name, "")), captor.capture()); + checkCaptured(captor, name); + return captor.getValue(); } /** @@ -183,7 +220,23 @@ public static void assertGauge(String name, double expected, */ public static void assertCounter(String name, long expected, MetricsRecordBuilder rb) { - verify(rb).addCounter(eqName(info(name, "")), eq(expected)); + Assert.assertEquals("Bad value for metric " + name, + expected, getLongCounter(name, rb)); + } + + public static long getLongCounter(String name, MetricsRecordBuilder rb) { + ArgumentCaptor captor = ArgumentCaptor.forClass(Long.class); + verify(rb, atLeast(0)).addCounter(eqName(info(name, "")), captor.capture()); + checkCaptured(captor, name); + return captor.getValue(); + } + + /** + * Check that this metric was captured exactly once. + */ + private static void checkCaptured(ArgumentCaptor captor, String name) { + Assert.assertEquals("Expected exactly one metric for name " + name, + 1, captor.getAllValues().size()); } /** @@ -238,7 +291,8 @@ public static void assertCounter(String name, long expected, */ public static void assertCounterGt(String name, long greater, MetricsRecordBuilder rb) { - verify(rb).addCounter(eqName(info(name, "")), gt(greater)); + Assert.assertThat("Bad value for metric " + name, getLongCounter(name, rb), + new GreaterThan(greater)); } /** @@ -260,7 +314,8 @@ public static void assertCounterGt(String name, long greater, */ public static void assertGaugeGt(String name, double greater, MetricsRecordBuilder rb) { - verify(rb).addGauge(eqName(info(name, "")), gt(greater)); + Assert.assertThat("Bad value for metric " + name, getDoubleGauge(name, rb), + new GreaterThan(greater)); } /**