|
| 1 | +package io.tehuti.metrics.stats; |
| 2 | + |
| 3 | +import io.tehuti.Metric; |
| 4 | +import io.tehuti.metrics.JmxReporter; |
| 5 | +import io.tehuti.metrics.MetricConfig; |
| 6 | +import io.tehuti.metrics.MetricsReporter; |
| 7 | +import io.tehuti.metrics.MetricsRepository; |
| 8 | +import io.tehuti.metrics.Sensor; |
| 9 | +import io.tehuti.utils.MockTime; |
| 10 | +import java.util.Arrays; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertNull; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | + |
| 17 | +/** |
| 18 | + * Unit tests for {@link io.tehuti.metrics.NoopSensor}. |
| 19 | + */ |
| 20 | +public class NoopSensorTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testNoopSensorIsNotRegisteredAndHasName() { |
| 24 | + MockTime time = new MockTime(); |
| 25 | + MetricConfig config = new MetricConfig(); |
| 26 | + MetricsRepository repo = new MetricsRepository(config, Arrays.asList((MetricsReporter) new JmxReporter()), time); |
| 27 | + |
| 28 | + Sensor noop = repo.getNoopSensor("noop"); |
| 29 | + |
| 30 | + // Name should be set |
| 31 | + assertEquals("noop", noop.name()); |
| 32 | + // NoopSensor is not tracked/registered in the repository |
| 33 | + assertNull(repo.getSensor("noop")); |
| 34 | + // Repository should have no metrics |
| 35 | + assertTrue(repo.metrics().isEmpty()); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testRecordDoesNothing() { |
| 40 | + MockTime time = new MockTime(); |
| 41 | + MetricConfig config = new MetricConfig(); |
| 42 | + MetricsRepository repo = new MetricsRepository(config, Arrays.asList((MetricsReporter) new JmxReporter()), time); |
| 43 | + |
| 44 | + Sensor noop = repo.getNoopSensor("noop"); |
| 45 | + |
| 46 | + // Invoke all record variants; should be no-ops and not throw |
| 47 | + noop.record(); |
| 48 | + noop.record(1.23); |
| 49 | + noop.record(4.56, time.milliseconds()); |
| 50 | + |
| 51 | + // Still no metrics registered/affected |
| 52 | + assertTrue(repo.metrics().isEmpty()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testAddMethodsReturnNullOrEmptyAndDoNotRegisterMetrics() { |
| 57 | + MockTime time = new MockTime(); |
| 58 | + MetricConfig config = new MetricConfig(); |
| 59 | + MetricsRepository repo = new MetricsRepository(config, Arrays.asList((MetricsReporter) new JmxReporter()), time); |
| 60 | + |
| 61 | + Sensor noop = repo.getNoopSensor("noop"); |
| 62 | + |
| 63 | + // MeasurableStat adds should return null and not register metrics |
| 64 | + Metric m1 = noop.add("test.avg", new Avg()); |
| 65 | + Metric m2 = noop.add("test.avg2", "desc", new Avg()); |
| 66 | + Metric m3 = noop.add("test.avg3", new Avg(), new MetricConfig()); |
| 67 | + assertNull(m1); |
| 68 | + assertNull(m2); |
| 69 | + assertNull(m3); |
| 70 | + |
| 71 | + // CompoundStat adds should return empty map and not register metrics |
| 72 | + Percentiles percs = new Percentiles(10, 0.0, 100.0, Percentiles.BucketSizing.CONSTANT, |
| 73 | + new Percentile("noop.p50", 50.0)); |
| 74 | + assertTrue(noop.add(percs, null).isEmpty()); |
| 75 | + |
| 76 | + // Repository should still have no metrics registered |
| 77 | + assertTrue(repo.metrics().isEmpty()); |
| 78 | + assertNull(repo.getMetric("test.avg")); |
| 79 | + assertNull(repo.getMetric("noop.p50")); |
| 80 | + } |
| 81 | +} |
0 commit comments