From c6fb1eec11f823644ecc4a8921bcf41b65b7a490 Mon Sep 17 00:00:00 2001 From: kolena Date: Thu, 25 May 2017 16:07:24 +0200 Subject: [PATCH] test fixes --- .../dropwizard/MetricsMonitorTest.java | 35 ---------- .../dropwizard/MetricsMonitorTest.java | 67 +++++++++++++++++++ 2 files changed, 67 insertions(+), 35 deletions(-) create mode 100644 jmx/src/test/java/com/avast/metrics/dropwizard/MetricsMonitorTest.java diff --git a/dropwizard-common/src/test/java/com/avast/metrics/dropwizard/MetricsMonitorTest.java b/dropwizard-common/src/test/java/com/avast/metrics/dropwizard/MetricsMonitorTest.java index 371b833..b0fb823 100644 --- a/dropwizard-common/src/test/java/com/avast/metrics/dropwizard/MetricsMonitorTest.java +++ b/dropwizard-common/src/test/java/com/avast/metrics/dropwizard/MetricsMonitorTest.java @@ -53,41 +53,6 @@ public void removal() { } } - @Test - public void jmxClose() { - try (Monitor m1 = new JmxMetricsMonitor("com.avast.metrics.test").named("test-jmx-close")) { - int value1 = 1; - Gauge gauge1 = m1.newGauge("gauge", () -> value1); - assertEquals(value1, (int) gauge1.getValue()); - } - - try (Monitor m2 = new JmxMetricsMonitor("com.avast.metrics.test").named("test-jmx-close")) { - int value2 = 2; - Gauge gauge2 = m2.newGauge("gauge", () -> value2); - assertEquals(value2, (int) gauge2.getValue()); - } - - // watch log output for javax.management.InstanceAlreadyExistsException - } - - @Test - public void getName() { - try (Monitor m1 = new JmxMetricsMonitor("com.avast.metrics.test").named("first", "second", "third", "fourth")) { - final String name = m1.getName(); - - assertEquals("first/second/third/fourth", name); - } - - try (Monitor m1 = new JmxMetricsMonitor("com.avast.metrics.test").named("first/sub").named("second").named("third").named("fourth/fifth")) { - try (Monitor m2 = new JmxMetricsMonitor("com.avast.metrics.test").named("first/sub", "second", "third").named("fourth/fifth")) { - final String name1 = m1.getName(); - final String name2 = m2.getName(); - - assertEquals(name1, name2); - } - } - } - @Test public void timing() throws InterruptedException { long toleranceNanos = 10000000; diff --git a/jmx/src/test/java/com/avast/metrics/dropwizard/MetricsMonitorTest.java b/jmx/src/test/java/com/avast/metrics/dropwizard/MetricsMonitorTest.java new file mode 100644 index 0000000..67513b1 --- /dev/null +++ b/jmx/src/test/java/com/avast/metrics/dropwizard/MetricsMonitorTest.java @@ -0,0 +1,67 @@ +package com.avast.metrics.dropwizard; + +import com.avast.metrics.api.Gauge; +import com.avast.metrics.api.Monitor; +import com.avast.metrics.api.Timer; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class MetricsMonitorTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void jmxClose() { + try (Monitor m1 = new JmxMetricsMonitor("com.avast.metrics.test").named("test-jmx-close")) { + int value1 = 1; + Gauge gauge1 = m1.newGauge("gauge", () -> value1); + assertEquals(value1, (int) gauge1.getValue()); + } + + try (Monitor m2 = new JmxMetricsMonitor("com.avast.metrics.test").named("test-jmx-close")) { + int value2 = 2; + Gauge gauge2 = m2.newGauge("gauge", () -> value2); + assertEquals(value2, (int) gauge2.getValue()); + } + + // watch log output for javax.management.InstanceAlreadyExistsException + } + + @Test + public void getName() { + try (Monitor m1 = new JmxMetricsMonitor("com.avast.metrics.test").named("first", "second", "third", "fourth")) { + final String name = m1.getName(); + + assertEquals("first/second/third/fourth", name); + } + + try (Monitor m1 = new JmxMetricsMonitor("com.avast.metrics.test").named("first/sub").named("second").named("third").named("fourth/fifth")) { + try (Monitor m2 = new JmxMetricsMonitor("com.avast.metrics.test").named("first/sub", "second", "third").named("fourth/fifth")) { + final String name1 = m1.getName(); + final String name2 = m2.getName(); + + assertEquals(name1, name2); + } + } + } + + @Test + public void timing() throws InterruptedException { + long toleranceNanos = 10000000; + try (Monitor monitor = new MetricsMonitor().named("timing")) { + Timer timer = monitor.newTimer("test"); + Timer.TimeContext ctx = timer.start(); + long time1 = System.nanoTime(); + long elapsedTime = ctx.stopAndGetTime(); + long time2 = System.nanoTime(); + long measuredElapsedTime = time2 - time1; + assertTrue(Math.abs(measuredElapsedTime - elapsedTime) < toleranceNanos); + } + } + +}