-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
67 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
jmx/src/test/java/com/avast/metrics/dropwizard/MetricsMonitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> 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<Integer> 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); | ||
} | ||
} | ||
|
||
} |