summaryrefslogtreecommitdiffstats
path: root/jdisc_core
diff options
context:
space:
mode:
authorJon Marius Venstad <venstad@gmail.com>2020-10-28 17:13:32 +0100
committerJon Marius Venstad <venstad@gmail.com>2020-10-28 17:13:32 +0100
commit45e4edf81bc1987d8b8d7e1e806b6d26fd329940 (patch)
treecf83cb8e62488409a049eaf6589580882f33f107 /jdisc_core
parent1f493d32ed2a20eb224db0101be7bad9debdfb83 (diff)
Move MockMetric to jdisc_core test package
Diffstat (limited to 'jdisc_core')
-rw-r--r--jdisc_core/src/main/java/com/yahoo/jdisc/test/MockMetric.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/test/MockMetric.java b/jdisc_core/src/main/java/com/yahoo/jdisc/test/MockMetric.java
new file mode 100644
index 00000000000..cce082aeb12
--- /dev/null
+++ b/jdisc_core/src/main/java/com/yahoo/jdisc/test/MockMetric.java
@@ -0,0 +1,53 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.jdisc.test;
+
+import com.yahoo.jdisc.Metric;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Metric implementation for tests.
+ *
+ * @author jonmv
+ */
+public class MockMetric implements Metric {
+
+ private final Map<String, Map<Map<String, ?>, Double>> metrics = new ConcurrentHashMap<>();
+
+ @Override
+ public void set(String key, Number val, Context ctx) {
+ metrics.computeIfAbsent(key, k -> new ConcurrentHashMap<>())
+ .put(MapContext.emptyIfNull(ctx).dimensions, val.doubleValue());
+ }
+
+ @Override
+ public void add(String key, Number val, Context ctx) {
+ metrics.computeIfAbsent(key, k -> new ConcurrentHashMap<>())
+ .merge(MapContext.emptyIfNull(ctx).dimensions, val.doubleValue(), Double::sum);
+ }
+
+ @Override
+ public Context createContext(Map<String, ?> properties) {
+ return properties == null ? MapContext.empty : new MapContext(properties);
+ }
+
+ public Map<String, Map<Map<String, ?>, Double>> metrics() { return metrics; }
+
+ private static class MapContext implements Context {
+
+ private static final MapContext empty = new MapContext(Map.of());
+
+ private final Map<String, ?> dimensions;
+
+ private MapContext(Map<String, ?> dimensions) {
+ this.dimensions = dimensions;
+ }
+
+ private static MapContext emptyIfNull(Context context) {
+ return context == null ? empty : (MapContext) context;
+ }
+
+ }
+
+}