From f74e8b22192c10f33e51ab689cd471960e7c976d Mon Sep 17 00:00:00 2001 From: Todd Lipcon Date: Fri, 7 Oct 2011 21:54:28 +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/branches/branch-0.23@1180258 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 380dc89f1ec..fed940d7dad 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -389,6 +389,9 @@ Release 0.23.0 - Unreleased HADOOP-7724. Fixed hadoop-setup-conf.sh to put proxy user in core-site.xml. (Arpit Gupta via Eric Yang) + 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 47b91e20ab7..f4bcb499e41 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 @@ package org.apache.hadoop.test; 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 @@ import static org.apache.hadoop.metrics2.lib.Interns.*; 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 class MetricsAsserts { */ 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 class MetricsAsserts { */ 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 class MetricsAsserts { */ 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 class MetricsAsserts { */ 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 class MetricsAsserts { */ 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 class MetricsAsserts { */ 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 class MetricsAsserts { */ 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)); } /**