summaryrefslogtreecommitdiffstats
path: root/simplemetrics
diff options
context:
space:
mode:
authorArne H Juul <arnej@yahoo-inc.com>2016-11-08 11:15:55 +0100
committerArne H Juul <arnej@yahoo-inc.com>2016-11-08 11:15:55 +0100
commit2dc510cbe94cf8e38049d0fdaea668a2c8e5e615 (patch)
tree72be1ea2e3ca06c999c1f03a81032fd9d4b448c9 /simplemetrics
parent34ae85006db23a313150838b94b175c0fcc32f45 (diff)
add unit test exposing bug in conversion, and a minimal fix
Diffstat (limited to 'simplemetrics')
-rw-r--r--simplemetrics/src/main/java/com/yahoo/metrics/simple/jdisc/SnapshotConverter.java3
-rw-r--r--simplemetrics/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java57
2 files changed, 57 insertions, 3 deletions
diff --git a/simplemetrics/src/main/java/com/yahoo/metrics/simple/jdisc/SnapshotConverter.java b/simplemetrics/src/main/java/com/yahoo/metrics/simple/jdisc/SnapshotConverter.java
index 83b84abaded..bb0989ae127 100644
--- a/simplemetrics/src/main/java/com/yahoo/metrics/simple/jdisc/SnapshotConverter.java
+++ b/simplemetrics/src/main/java/com/yahoo/metrics/simple/jdisc/SnapshotConverter.java
@@ -94,6 +94,9 @@ class SnapshotConverter {
}
private Map<String, MetricValue> getMap(Point point) {
+ if (point == null) {
+ point = new Point(new HashMap<>());
+ }
if (! perPointData.containsKey(point)) {
perPointData.put(point, new HashMap<>());
}
diff --git a/simplemetrics/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java b/simplemetrics/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java
index c5be6830fcb..d224f89a947 100644
--- a/simplemetrics/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java
+++ b/simplemetrics/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java
@@ -1,25 +1,76 @@
package com.yahoo.metrics.simple.jdisc;
+import com.yahoo.container.jdisc.state.CountMetric;
+import com.yahoo.container.jdisc.state.GaugeMetric;
import com.yahoo.container.jdisc.state.MetricDimensions;
+import com.yahoo.container.jdisc.state.MetricSet;
+import com.yahoo.container.jdisc.state.MetricSnapshot;
+import com.yahoo.container.jdisc.state.MetricValue;
+import com.yahoo.metrics.simple.Bucket;
+import com.yahoo.metrics.simple.Identifier;
+import com.yahoo.metrics.simple.MetricReceiver;
import com.yahoo.metrics.simple.Point;
+import com.yahoo.metrics.simple.UntypedMetric;
import org.junit.Test;
import java.util.Collections;
import java.util.HashMap;
+import java.util.Map;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
/**
* @author bratseth
*/
public class SnapshotConverterTest {
-
+
@Test
- public void testConversion() {
+ public void testPointConversion() {
MetricDimensions a = SnapshotConverter.convert(new Point(Collections.emptyMap()));
MetricDimensions b = SnapshotConverter.convert(new Point(new HashMap<>(0)));
+ MetricDimensions c = SnapshotConverter.convert((Point)null);
assertEquals(a.hashCode(), b.hashCode());
assertEquals(a, b);
+ assertEquals(a.hashCode(), c.hashCode());
+ assertEquals(a, c);
+ assertEquals(b.hashCode(), c.hashCode());
+ assertEquals(b, c);
+ }
+
+ @Test
+ public void testConversion() {
+ MetricReceiver mock = new MetricReceiver.MockReceiver();
+ mock.declareCounter("foo").add(1);
+ mock.declareGauge("quuux").sample(42.25);
+ mock.declareCounter("bar", new Point(new HashMap<String,String>())).add(4);
+
+ MetricSnapshot snapshot = new SnapshotConverter(mock.getSnapshot()).convert();
+
+ for (Map.Entry<MetricDimensions, MetricSet> entry : snapshot) {
+ for (Map.Entry<String, String> dv : entry.getKey()) {
+ assertTrue(false);
+ }
+
+ int cnt = 0;
+ for (Map.Entry<String, MetricValue> mv : entry.getValue()) {
+ ++cnt;
+ if ("foo".equals(mv.getKey())) {
+ assertTrue(mv.getValue() instanceof CountMetric);
+ assertEquals(1, ((CountMetric)mv.getValue()).getCount());
+ } else if ("bar".equals(mv.getKey())) {
+ assertTrue(mv.getValue() instanceof CountMetric);
+ assertEquals(4, ((CountMetric)mv.getValue()).getCount());
+ } else if ("quuux".equals(mv.getKey())) {
+ assertTrue(mv.getValue() instanceof GaugeMetric);
+ assertEquals(42.25, ((GaugeMetric)mv.getValue()).getLast(), 0.001);
+ assertEquals(1, ((GaugeMetric)mv.getValue()).getCount());
+ } else {
+ assertTrue(false);
+ }
+ }
+ assertEquals(3, cnt);
+ }
}
-
+
}