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<Metric>)mr.metrics()).size());
assertEquals("same metric value", 0, cr.getMetric("m"));
assertNotNull("metric not null", cr.getMetricInstance("m"));
assertEquals("new metric value", 0, cr.getMetricInstance("m").value());
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"));
assertNotNull("metric not null", cr.getMetricInstance("m"));
assertEquals("new metric value", 2, cr.getMetricInstance("m").value());
assertEquals("old metric value", 1, cr.getMetric("m1"));
assertNotNull("metric not null", cr.getMetricInstance("m1"));
assertEquals("new metric value", 1, cr.getMetricInstance("m1").value());
assertEquals("new metric value", 42, cr.getMetric("m2"));
assertNotNull("metric not null", cr.getMetricInstance("m2"));
assertEquals("new metric value", 42, cr.getMetricInstance("m2").value());
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"));