You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by ac...@apache.org on 2011/05/06 09:28:46 UTC
svn commit: r1100113 [5/5] - in /hadoop/common/trunk: ./ conf/ ivy/
src/java/org/apache/hadoop/metrics/file/ src/java/org/apache/hadoop/metrics2/
src/java/org/apache/hadoop/metrics2/annotation/
src/java/org/apache/hadoop/metrics2/filter/ src/java/org/a...
Added: hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/lib/TestMutableMetrics.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/lib/TestMutableMetrics.java?rev=1100113&view=auto
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/lib/TestMutableMetrics.java (added)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/lib/TestMutableMetrics.java Fri May 6 07:28:43 2011
@@ -0,0 +1,106 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.metrics2.lib;
+
+import org.junit.Test;
+import static org.mockito.Mockito.*;
+import static org.mockito.AdditionalMatchers.*;
+
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import static org.apache.hadoop.metrics2.lib.Interns.*;
+import static org.apache.hadoop.test.MetricsAsserts.*;
+
+/**
+ * Test metrics record builder interface and mutable metrics
+ */
+public class TestMutableMetrics {
+
+ private final double EPSILON = 1e-42;
+
+ /**
+ * Test the snapshot method
+ */
+ @Test public void testSnapshot() {
+ MetricsRecordBuilder mb = mockMetricsRecordBuilder();
+
+ MetricsRegistry registry = new MetricsRegistry("test");
+ registry.newCounter("c1", "int counter", 1);
+ registry.newCounter("c2", "long counter", 2L);
+ registry.newGauge("g1", "int gauge", 3);
+ registry.newGauge("g2", "long gauge", 4L);
+ registry.newStat("s1", "stat", "Ops", "Time", true).add(0);
+ registry.newRate("s2", "stat", false).add(0);
+
+ registry.snapshot(mb, true);
+
+ MutableStat s2 = (MutableStat) registry.get("s2");
+
+ s2.snapshot(mb, true); // should get the same back.
+ s2.add(1);
+ s2.snapshot(mb, true); // should get new interval values back
+
+ verify(mb).addCounter(info("c1", "int counter"), 1);
+ verify(mb).addCounter(info("c2", "long counter"), 2L);
+ verify(mb).addGauge(info("g1", "int gauge"), 3);
+ verify(mb).addGauge(info("g2", "long gauge"), 4L);
+ verify(mb).addCounter(info("S1NumOps", "Number of ops for stat"), 1L);
+ verify(mb).addGauge(eq(info("S1AvgTime", "Average time for stat")),
+ eq(0.0, EPSILON));
+ verify(mb).addGauge(eq(info("S1StdevTime",
+ "Standard deviation of time for stat")),
+ eq(0.0, EPSILON));
+ verify(mb).addGauge(eq(info("S1IMinTime",
+ "Interval min time for stat")),
+ eq(0.0, EPSILON));
+ verify(mb).addGauge(eq(info("S1IMaxTime",
+ "Interval max time for stat")),
+ eq(0.0, EPSILON));
+ verify(mb).addGauge(eq(info("S1MinTime","Min time for stat")),
+ eq(0.0, EPSILON));
+ verify(mb).addGauge(eq(info("S1MaxTime","Max time for stat")),
+ eq(0.0, EPSILON));
+ verify(mb, times(2))
+ .addCounter(info("S2NumOps", "Number of ops for stat"), 1L);
+ verify(mb, times(2)).addGauge(eq(info("S2AvgTime",
+ "Average time for stat")),
+ eq(0.0, EPSILON));
+ verify(mb).addCounter(info("S2NumOps", "Number of ops for stat"), 2L);
+ verify(mb).addGauge(eq(info("S2AvgTime", "Average time for stat")),
+ eq(1.0, EPSILON));
+ }
+
+ interface TestProtocol {
+ void foo();
+ void bar();
+ }
+
+ @Test public void testMutableRates() {
+ MetricsRecordBuilder rb = mockMetricsRecordBuilder();
+ MetricsRegistry registry = new MetricsRegistry("test");
+ MutableRates rates = new MutableRates(registry);
+
+ rates.init(TestProtocol.class);
+ registry.snapshot(rb, false);
+
+ assertCounter("FooNumOps", 0L, rb);
+ assertGauge("FooAvgTime", 0.0, rb);
+ assertCounter("BarNumOps", 0L, rb);
+ assertGauge("BarAvgTime", 0.0, rb);
+ }
+}
Added: hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/lib/TestUniqNames.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/lib/TestUniqNames.java?rev=1100113&view=auto
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/lib/TestUniqNames.java (added)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/lib/TestUniqNames.java Fri May 6 07:28:43 2011
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.metrics2.lib;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class TestUniqNames {
+
+ @Test public void testCommonCases() {
+ UniqueNames u = new UniqueNames();
+
+ assertEquals("foo", u.uniqueName("foo"));
+ assertEquals("foo-1", u.uniqueName("foo"));
+ }
+
+ @Test public void testCollisions() {
+ UniqueNames u = new UniqueNames();
+ u.uniqueName("foo");
+
+ assertEquals("foo-1", u.uniqueName("foo-1"));
+ assertEquals("foo-2", u.uniqueName("foo"));
+ assertEquals("foo-1-1", u.uniqueName("foo-1"));
+ assertEquals("foo-2-1", u.uniqueName("foo-2"));
+ }
+}
Added: hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/util/TestMetricsCache.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/util/TestMetricsCache.java?rev=1100113&view=auto
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/util/TestMetricsCache.java (added)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/util/TestMetricsCache.java Fri May 6 07:28:43 2011
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.metrics2.util;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.metrics2.AbstractMetric;
+import org.apache.hadoop.metrics2.MetricsRecord;
+import org.apache.hadoop.metrics2.MetricsTag;
+import static org.apache.hadoop.metrics2.lib.Interns.*;
+
+public class TestMetricsCache {
+ private static final Log LOG = LogFactory.getLog(TestMetricsCache.class);
+
+ @Test public void testUpdate() {
+ MetricsCache cache = new MetricsCache();
+ MetricsRecord mr = makeRecord("r",
+ Arrays.asList(makeTag("t", "tv")),
+ Arrays.asList(makeMetric("m", 0), makeMetric("m1", 1)));
+
+ MetricsCache.Record cr = cache.update(mr);
+ verify(mr).name();
+ verify(mr).tags();
+ verify(mr).metrics();
+ assertEquals("same record size", cr.metrics().size(),
+ ((Collection<AbstractMetric>)mr.metrics()).size());
+ assertEquals("same metric value", 0, cr.getMetric("m"));
+
+ MetricsRecord mr2 = makeRecord("r",
+ Arrays.asList(makeTag("t", "tv")),
+ Arrays.asList(makeMetric("m", 2), makeMetric("m2", 42)));
+ cr = cache.update(mr2);
+ assertEquals("contains 3 metric", 3, cr.metrics().size());
+ assertEquals("updated metric value", 2, cr.getMetric("m"));
+ assertEquals("old metric value", 1, cr.getMetric("m1"));
+ assertEquals("new metric value", 42, cr.getMetric("m2"));
+
+ MetricsRecord mr3 = makeRecord("r",
+ Arrays.asList(makeTag("t", "tv3")), // different tag value
+ Arrays.asList(makeMetric("m3", 3)));
+ cr = cache.update(mr3); // should get a new record
+ assertEquals("contains 1 metric", 1, cr.metrics().size());
+ assertEquals("updated metric value", 3, cr.getMetric("m3"));
+ // tags cache should be empty so far
+ assertEquals("no tags", 0, cr.tags().size());
+ // until now
+ cr = cache.update(mr3, true);
+ assertEquals("Got 1 tag", 1, cr.tags().size());
+ assertEquals("Tag value", "tv3", cr.getTag("t"));
+ assertEquals("Metric value", 3, cr.getMetric("m3"));
+ }
+
+ @Test public void testGet() {
+ MetricsCache cache = new MetricsCache();
+ assertNull("empty", cache.get("r", Arrays.asList(makeTag("t", "t"))));
+ MetricsRecord mr = makeRecord("r",
+ Arrays.asList(makeTag("t", "t")),
+ Arrays.asList(makeMetric("m", 1)));
+ cache.update(mr);
+ MetricsCache.Record cr = cache.get("r", mr.tags());
+ LOG.debug("tags="+ mr.tags() +" cr="+ cr);
+
+ assertNotNull("Got record", cr);
+ assertEquals("contains 1 metric", 1, cr.metrics().size());
+ assertEquals("new metric value", 1, cr.getMetric("m"));
+ }
+
+ /**
+ * Make sure metrics tag has a sane hashCode impl
+ */
+ @Test public void testNullTag() {
+ MetricsCache cache = new MetricsCache();
+ MetricsRecord mr = makeRecord("r",
+ Arrays.asList(makeTag("t", null)),
+ Arrays.asList(makeMetric("m", 0), makeMetric("m1", 1)));
+
+ MetricsCache.Record cr = cache.update(mr);
+ assertTrue("t value should be null", null == cr.getTag("t"));
+ }
+
+ @Test public void testOverflow() {
+ MetricsCache cache = new MetricsCache();
+ MetricsCache.Record cr;
+ Collection<MetricsTag> t0 = Arrays.asList(makeTag("t0", "0"));
+ for (int i = 0; i < MetricsCache.MAX_RECS_PER_NAME_DEFAULT + 1; ++i) {
+ cr = cache.update(makeRecord("r",
+ Arrays.asList(makeTag("t"+ i, ""+ i)),
+ Arrays.asList(makeMetric("m", i))));
+ assertEquals("new metrics value", i, cr.getMetric("m"));
+ if (i < MetricsCache.MAX_RECS_PER_NAME_DEFAULT) {
+ assertNotNull("t0 is still there", cache.get("r", t0));
+ }
+ }
+ assertNull("t0 is gone", cache.get("r", t0));
+ }
+
+ private MetricsRecord makeRecord(String name, Collection<MetricsTag> tags,
+ Collection<AbstractMetric> metrics) {
+ MetricsRecord mr = mock(MetricsRecord.class);
+ when(mr.name()).thenReturn(name);
+ when(mr.tags()).thenReturn(tags);
+ when(mr.metrics()).thenReturn(metrics);
+ return mr;
+ }
+
+ private MetricsTag makeTag(String name, String value) {
+ return new MetricsTag(info(name, ""), value);
+ }
+
+ private AbstractMetric makeMetric(String name, Number value) {
+ AbstractMetric metric = mock(AbstractMetric.class);
+ when(metric.name()).thenReturn(name);
+ when(metric.value()).thenReturn(value);
+ return metric;
+ }
+}
Added: hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/util/TestSampleStat.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/util/TestSampleStat.java?rev=1100113&view=auto
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/util/TestSampleStat.java (added)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/metrics2/util/TestSampleStat.java Fri May 6 07:28:43 2011
@@ -0,0 +1,67 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.metrics2.util;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * Test the running sample stat computation
+ */
+public class TestSampleStat {
+ private static final double EPSILON = 1e-42;
+
+ /**
+ * Some simple use cases
+ */
+ @Test public void testSimple() {
+ SampleStat stat = new SampleStat();
+ assertEquals("num samples", 0, stat.numSamples());
+ assertEquals("mean", 0.0, stat.mean(), EPSILON);
+ assertEquals("variance", 0.0, stat.variance(), EPSILON);
+ assertEquals("stddev", 0.0, stat.stddev(), EPSILON);
+ assertEquals("min", Double.MAX_VALUE, stat.min(), EPSILON);
+ assertEquals("max", Double.MIN_VALUE, stat.max(), EPSILON);
+
+ stat.add(3);
+ assertEquals("num samples", 1L, stat.numSamples());
+ assertEquals("mean", 3.0, stat.mean(), EPSILON);
+ assertEquals("variance", 0.0, stat.variance(), EPSILON);
+ assertEquals("stddev", 0.0, stat.stddev(), EPSILON);
+ assertEquals("min", 3.0, stat.min(), EPSILON);
+ assertEquals("max", 3.0, stat.max(), EPSILON);
+
+ stat.add(2).add(1);
+ assertEquals("num samples", 3L, stat.numSamples());
+ assertEquals("mean", 2.0, stat.mean(), EPSILON);
+ assertEquals("variance", 1.0, stat.variance(), EPSILON);
+ assertEquals("stddev", 1.0, stat.stddev(), EPSILON);
+ assertEquals("min", 1.0, stat.min(), EPSILON);
+ assertEquals("max", 3.0, stat.max(), EPSILON);
+
+ stat.reset();
+ assertEquals("num samples", 0, stat.numSamples());
+ assertEquals("mean", 0.0, stat.mean(), EPSILON);
+ assertEquals("variance", 0.0, stat.variance(), EPSILON);
+ assertEquals("stddev", 0.0, stat.stddev(), EPSILON);
+ assertEquals("min", Double.MAX_VALUE, stat.min(), EPSILON);
+ assertEquals("max", Double.MIN_VALUE, stat.max(), EPSILON);
+ }
+
+}
Added: hadoop/common/trunk/src/test/core/org/apache/hadoop/test/MetricsAsserts.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/test/MetricsAsserts.java?rev=1100113&view=auto
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/test/MetricsAsserts.java (added)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/test/MetricsAsserts.java Fri May 6 07:28:43 2011
@@ -0,0 +1,276 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.test;
+
+import static com.google.common.base.Preconditions.*;
+
+import org.hamcrest.Description;
+
+import static org.mockito.Mockito.*;
+import org.mockito.stubbing.Answer;
+import org.mockito.invocation.InvocationOnMock;
+import static org.mockito.AdditionalMatchers.*;
+import org.mockito.ArgumentMatcher;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.metrics2.MetricsInfo;
+import org.apache.hadoop.metrics2.MetricsCollector;
+import org.apache.hadoop.metrics2.MetricsSource;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import org.apache.hadoop.metrics2.MetricsSystem;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import static org.apache.hadoop.metrics2.lib.Interns.*;
+
+/**
+ * Helpers for metrics source tests
+ */
+public class MetricsAsserts {
+
+ final static Log LOG = LogFactory.getLog(MetricsAsserts.class);
+
+ public static MetricsSystem mockMetricsSystem() {
+ MetricsSystem ms = mock(MetricsSystem.class);
+ DefaultMetricsSystem.setInstance(ms);
+ return ms;
+ }
+
+ public static MetricsRecordBuilder mockMetricsRecordBuilder() {
+ final MetricsCollector mc = mock(MetricsCollector.class);
+ MetricsRecordBuilder rb = mock(MetricsRecordBuilder.class,
+ new Answer<Object>() {
+ @Override
+ public Object answer(InvocationOnMock invocation) {
+ Object[] args = invocation.getArguments();
+ StringBuilder sb = new StringBuilder();
+ for (Object o : args) {
+ if (sb.length() > 0) sb.append(", ");
+ sb.append(String.valueOf(o));
+ }
+ String methodName = invocation.getMethod().getName();
+ LOG.debug(methodName +": "+ sb);
+ return methodName.equals("parent") || methodName.equals("endRecord") ?
+ mc : invocation.getMock();
+ }
+ });
+ when(mc.addRecord(anyString())).thenReturn(rb);
+ when(mc.addRecord(anyInfo())).thenReturn(rb);
+ return rb;
+ }
+
+ /**
+ * Call getMetrics on source and get a record builder mock to verify
+ * @param source the metrics source
+ * @param all if true, return all metrics even if not changed
+ * @return the record builder mock to verify
+ */
+ public static MetricsRecordBuilder getMetrics(MetricsSource source,
+ boolean all) {
+ MetricsRecordBuilder rb = mockMetricsRecordBuilder();
+ MetricsCollector mc = rb.parent();
+ source.getMetrics(mc, all);
+ return rb;
+ }
+
+ public static MetricsRecordBuilder getMetrics(String name) {
+ return getMetrics(DefaultMetricsSystem.instance().getSource(name));
+ }
+
+ public static MetricsRecordBuilder getMetrics(MetricsSource source) {
+ return getMetrics(source, true);
+ }
+
+ private static class InfoWithSameName extends ArgumentMatcher<MetricsInfo> {
+ private final String expected;
+
+ InfoWithSameName(MetricsInfo info) {
+ expected = checkNotNull(info.name(), "info name");
+ }
+
+ @Override public boolean matches(Object info) {
+ return expected.equals(((MetricsInfo)info).name());
+ }
+
+ @Override public void describeTo(Description desc) {
+ desc.appendText("Info with name="+ expected);
+ }
+ }
+
+ /**
+ * MetricInfo with the same name
+ * @param info to match
+ * @return <code>null</code>
+ */
+ public static MetricsInfo eqName(MetricsInfo info) {
+ return argThat(new InfoWithSameName(info));
+ }
+
+ private static class AnyInfo extends ArgumentMatcher<MetricsInfo> {
+ @Override public boolean matches(Object info) {
+ return info instanceof MetricsInfo; // not null as well
+ }
+ }
+
+ public static MetricsInfo anyInfo() {
+ return argThat(new AnyInfo());
+ }
+
+ /**
+ * Assert an int gauge metric as expected
+ * @param name of the metric
+ * @param expected value of the metric
+ * @param rb the record builder mock used to getMetrics
+ */
+ public static void assertGauge(String name, int expected,
+ MetricsRecordBuilder rb) {
+ verify(rb).addGauge(eqName(info(name, "")), eq(expected));
+ }
+
+ /**
+ * Assert an int counter metric as expected
+ * @param name of the metric
+ * @param expected value of the metric
+ * @param rb the record builder mock used to getMetrics
+ */
+ public static void assertCounter(String name, int expected,
+ MetricsRecordBuilder rb) {
+ verify(rb).addCounter(eqName(info(name, "")), eq(expected));
+ }
+
+ /**
+ * Assert a long gauge metric as expected
+ * @param name of the metric
+ * @param expected value of the metric
+ * @param rb the record builder mock used to getMetrics
+ */
+ public static void assertGauge(String name, long expected,
+ MetricsRecordBuilder rb) {
+ verify(rb).addGauge(eqName(info(name, "")), eq(expected));
+ }
+
+ /**
+ * Assert a double gauge metric as expected
+ * @param name of the metric
+ * @param expected value of the metric
+ * @param rb the record builder mock used to getMetrics
+ */
+ public static void assertGauge(String name, double expected,
+ MetricsRecordBuilder rb) {
+ verify(rb).addGauge(eqName(info(name, "")), eq(expected));
+ }
+
+ /**
+ * Assert a long counter metric as expected
+ * @param name of the metric
+ * @param expected value of the metric
+ * @param rb the record builder mock used to getMetrics
+ */
+ public static void assertCounter(String name, long expected,
+ MetricsRecordBuilder rb) {
+ verify(rb).addCounter(eqName(info(name, "")), eq(expected));
+ }
+
+ /**
+ * Assert an int gauge metric as expected
+ * @param name of the metric
+ * @param expected value of the metric
+ * @param source to get metrics from
+ */
+ public static void assertGauge(String name, int expected,
+ MetricsSource source) {
+ assertGauge(name, expected, getMetrics(source));
+ }
+
+ /**
+ * Assert an int counter metric as expected
+ * @param name of the metric
+ * @param expected value of the metric
+ * @param source to get metrics from
+ */
+ public static void assertCounter(String name, int expected,
+ MetricsSource source) {
+ assertCounter(name, expected, getMetrics(source));
+ }
+
+ /**
+ * Assert a long gauge metric as expected
+ * @param name of the metric
+ * @param expected value of the metric
+ * @param source to get metrics from
+ */
+ public static void assertGauge(String name, long expected,
+ MetricsSource source) {
+ assertGauge(name, expected, getMetrics(source));
+ }
+
+ /**
+ * Assert a long counter metric as expected
+ * @param name of the metric
+ * @param expected value of the metric
+ * @param source to get metrics from
+ */
+ public static void assertCounter(String name, long expected,
+ MetricsSource source) {
+ assertCounter(name, expected, getMetrics(source));
+ }
+
+ /**
+ * Assert that a long counter metric is greater than a value
+ * @param name of the metric
+ * @param greater value of the metric should be greater than this
+ * @param rb the record builder mock used to getMetrics
+ */
+ public static void assertCounterGt(String name, long greater,
+ MetricsRecordBuilder rb) {
+ verify(rb).addCounter(eqName(info(name, "")), gt(greater));
+ }
+
+ /**
+ * Assert that a long counter metric is greater than a value
+ * @param name of the metric
+ * @param greater value of the metric should be greater than this
+ * @param source the metrics source
+ */
+ public static void assertCounterGt(String name, long greater,
+ MetricsSource source) {
+ assertCounterGt(name, greater, getMetrics(source));
+ }
+
+ /**
+ * Assert that a double gauge metric is greater than a value
+ * @param name of the metric
+ * @param greater value of the metric should be greater than this
+ * @param rb the record builder mock used to getMetrics
+ */
+ public static void assertGaugeGt(String name, double greater,
+ MetricsRecordBuilder rb) {
+ verify(rb).addGauge(eqName(info(name, "")), gt(greater));
+ }
+
+ /**
+ * Assert that a double gauge metric is greater than a value
+ * @param name of the metric
+ * @param greater value of the metric should be greater than this
+ * @param source the metrics source
+ */
+ public static void assertGaugeGt(String name, double greater,
+ MetricsSource source) {
+ assertGaugeGt(name, greater, getMetrics(source));
+ }
+}
Added: hadoop/common/trunk/src/test/core/org/apache/hadoop/test/MoreAsserts.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/test/MoreAsserts.java?rev=1100113&view=auto
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/test/MoreAsserts.java (added)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/test/MoreAsserts.java Fri May 6 07:28:43 2011
@@ -0,0 +1,66 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.test;
+
+import java.util.Iterator;
+import org.junit.Assert;
+
+/**
+ * A few more asserts
+ */
+public class MoreAsserts {
+
+ /**
+ * Assert equivalence for array and iterable
+ * @param <T> the type of the elements
+ * @param s the name/message for the collection
+ * @param expected the expected array of elements
+ * @param actual the actual iterable of elements
+ */
+ public static <T> void assertEquals(String s, T[] expected,
+ Iterable<T> actual) {
+ Iterator<T> it = actual.iterator();
+ int i = 0;
+ for (; i < expected.length && it.hasNext(); ++i) {
+ Assert.assertEquals("Element "+ i +" for "+ s, expected[i], it.next());
+ }
+ Assert.assertTrue("Expected more elements", i == expected.length);
+ Assert.assertTrue("Expected less elements", !it.hasNext());
+ }
+
+ /**
+ * Assert equality for two iterables
+ * @param <T> the type of the elements
+ * @param s
+ * @param expected
+ * @param actual
+ */
+ public static <T> void assertEquals(String s, Iterable<T> expected,
+ Iterable<T> actual) {
+ Iterator<T> ite = expected.iterator();
+ Iterator<T> ita = actual.iterator();
+ int i = 0;
+ while (ite.hasNext() && ita.hasNext()) {
+ Assert.assertEquals("Element "+ i +" for "+s, ite.next(), ita.next());
+ }
+ Assert.assertTrue("Expected more elements", !ite.hasNext());
+ Assert.assertTrue("Expected less elements", !ita.hasNext());
+ }
+
+}