@Test public void queuePollSyncTracksOnComplete() { //prepare registry with mock clock MockClock clock = new MockClock(); removeRegistry(); registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock); Metrics.globalRegistry.add(registry); AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create(); MicrometerFluxMetricsFuseableSubscriber<Integer> fuseableSubscriber = new MicrometerFluxMetricsFuseableSubscriber<>(testSubscriber, registry, clock, "foo", Collections.emptyList()); Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>(); testQueue.offer(1); fuseableSubscriber.onSubscribe(testQueue); fuseableSubscriber.requestFusion(Fuseable.SYNC); clock.add(Duration.ofMillis(200)); Integer val1 = fuseableSubscriber.poll(); clock.add(Duration.ofMillis(123)); Integer val2 = fuseableSubscriber.poll(); assertThat(val1).isEqualTo(1); assertThat(val2).isNull(); //test meters Timer terminationTimer = registry.find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_COMPLETE) .timer(); assertThat(terminationTimer).isNotNull(); assertThat(terminationTimer.max(TimeUnit.MILLISECONDS)).as("terminate max delay").isEqualTo(323); }
@Test public void queuePollSyncTracksOnComplete() { //prepare registry with mock clock MockClock clock = new MockClock(); removeRegistry(); registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock); Metrics.globalRegistry.add(registry); AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create(); MicrometerMonoMetricsFuseableSubscriber<Integer> fuseableSubscriber = new MicrometerMonoMetricsFuseableSubscriber<>(testSubscriber, registry, clock, "foo", Collections.emptyList()); Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>(); testQueue.offer(1); fuseableSubscriber.onSubscribe(testQueue); fuseableSubscriber.requestFusion(Fuseable.SYNC); clock.add(Duration.ofMillis(200)); Integer val1 = fuseableSubscriber.poll(); clock.add(Duration.ofMillis(123)); Integer val2 = fuseableSubscriber.poll(); assertThat(val1).isEqualTo(1); assertThat(val2).isNull(); //test meters Timer terminationTimer = registry.find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_COMPLETE) .timer(); assertThat(terminationTimer).isNotNull(); assertThat(terminationTimer.max(TimeUnit.MILLISECONDS)).as("terminate max delay").isEqualTo(323); }
@Test public void queuePollTracksOnNext() { //prepare registry with mock clock MockClock clock = new MockClock(); removeRegistry(); registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock); Metrics.globalRegistry.add(registry); AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create(); MicrometerFluxMetricsFuseableSubscriber<Integer> fuseableSubscriber = new MicrometerFluxMetricsFuseableSubscriber<>(testSubscriber, registry, clock, "foo", Collections.emptyList()); Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>(); testQueue.offer(1); fuseableSubscriber.onSubscribe(testQueue); clock.add(Duration.ofMillis(200)); Integer val1 = fuseableSubscriber.poll(); Integer val2 = fuseableSubscriber.poll(); assertThat(val1).isEqualTo(1); assertThat(val2).isNull(); //test meters Timer nextTimer = registry.find(METER_ON_NEXT_DELAY) .timer(); assertThat(nextTimer).isNotNull(); assertThat(nextTimer.max(TimeUnit.MILLISECONDS)).as("onNext max delay").isEqualTo(200); }
@Test public void queuePollError() { //prepare registry with mock clock MockClock clock = new MockClock(); removeRegistry(); registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock); Metrics.globalRegistry.add(registry); AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create(); MicrometerMonoMetricsFuseableSubscriber<Integer> fuseableSubscriber = new MicrometerMonoMetricsFuseableSubscriber<>(testSubscriber, registry, clock, "foo", Collections.emptyList()); FluxPeekFuseableTest.AssertQueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>(); testQueue.setCompleteWithError(true); testQueue.offer(1); fuseableSubscriber.onSubscribe(testQueue); fuseableSubscriber.requestFusion(Fuseable.SYNC); clock.add(Duration.ofMillis(200)); Integer val1 = fuseableSubscriber.poll(); assertThat(val1).isEqualTo(1); clock.add(Duration.ofMillis(123)); assertThatIllegalStateException().isThrownBy(fuseableSubscriber::poll) .withMessage("AssertQueueSubscriber poll error"); //test meters Timer terminationTimer = registry.find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_ERROR) .timer(); assertThat(terminationTimer).isNotNull(); assertThat(terminationTimer.max(TimeUnit.MILLISECONDS)).as("terminate max delay").isEqualTo(323); }
@Test public void queuePollError() { //prepare registry with mock clock MockClock clock = new MockClock(); removeRegistry(); registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock); Metrics.globalRegistry.add(registry); AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create(); MicrometerFluxMetricsFuseableSubscriber<Integer> fuseableSubscriber = new MicrometerFluxMetricsFuseableSubscriber<>(testSubscriber, registry, clock, "foo", Collections.emptyList()); FluxPeekFuseableTest.AssertQueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>(); testQueue.setCompleteWithError(true); testQueue.offer(1); fuseableSubscriber.onSubscribe(testQueue); fuseableSubscriber.requestFusion(Fuseable.SYNC); clock.add(Duration.ofMillis(200)); Integer val1 = fuseableSubscriber.poll(); assertThat(val1).isEqualTo(1); clock.add(Duration.ofMillis(123)); assertThatIllegalStateException().isThrownBy(fuseableSubscriber::poll) .withMessage("AssertQueueSubscriber poll error"); //test meters Timer terminationTimer = registry.find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_ERROR) .timer(); assertThat(terminationTimer).isNotNull(); assertThat(terminationTimer.max(TimeUnit.MILLISECONDS)).as("terminate max delay").isEqualTo(323); }
@Test public void queuePollDoesntTrackOnNext() { //prepare registry with mock clock MockClock clock = new MockClock(); removeRegistry(); registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock); Metrics.globalRegistry.add(registry); AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create(); MicrometerMonoMetricsFuseableSubscriber<Integer> fuseableSubscriber = new MicrometerMonoMetricsFuseableSubscriber<>(testSubscriber, registry, clock, "foo", Collections.emptyList()); Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>(); testQueue.offer(1); fuseableSubscriber.onSubscribe(testQueue); clock.add(Duration.ofMillis(200)); Integer val1 = fuseableSubscriber.poll(); Integer val2 = fuseableSubscriber.poll(); assertThat(val1).isEqualTo(1); assertThat(val2).isNull(); //test meters Timer nextTimer = registry.find(METER_ON_NEXT_DELAY) .timer(); assertThat(nextTimer).as("no onNext delay meter for Mono").isNull(); }
@Test default void recordMax(MeterRegistry registry) { Timer timer = registry.timer("my.timer"); timer.record(10, TimeUnit.MILLISECONDS); timer.record(1, TimeUnit.SECONDS); clock(registry).add(step()); // for Atlas, which is step rather than ring-buffer based assertThat(timer.max(TimeUnit.SECONDS)).isEqualTo(1); assertThat(timer.max(TimeUnit.MILLISECONDS)).isEqualTo(1000); //noinspection ConstantConditions clock(registry).add(Duration.ofMillis(step().toMillis() * DistributionStatisticConfig.DEFAULT.getBufferLength())); assertThat(timer.max(TimeUnit.SECONDS)).isEqualTo(0); }
@Test @DisplayName("function-tracking counter increments by change in a monotonically increasing function when observed") default void functionTrackingCounter(MeterRegistry registry) { AtomicLong n = new AtomicLong(0); registry.more().counter("tracking", emptyList(), n); n.incrementAndGet(); clock(registry).add(step()); registry.forEachMeter(Meter::measure); assertThat(registry.get("tracking").functionCounter().count()).isEqualTo(1.0); } }
@DisplayName("multiple increments are maintained") @Test default void increment(MeterRegistry registry) { Counter c = registry.counter("myCounter"); c.increment(); clock(registry).add(step()); assertThat(c.count()).isEqualTo(1.0, offset(1e-12)); c.increment(); c.increment(); clock(registry).add(step()); // in the case of a step aggregating system will be 2, otherwise 3 assertThat(c.count()).isGreaterThanOrEqualTo(2.0); }
@Test @DisplayName("record a runnable task") default void recordWithRunnable(MeterRegistry registry) { Timer t = registry.timer("myTimer"); try { t.record(() -> clock(registry).add(10, TimeUnit.NANOSECONDS)); clock(registry).add(step()); } finally { assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(10, t.totalTime(TimeUnit.NANOSECONDS), 1.0e-12)); } }
@Test @DisplayName("record with stateful Sample instance") default void recordWithSample(MeterRegistry registry) { Timer timer = registry.timer("myTimer"); Timer.Sample sample = Timer.start(registry); clock(registry).add(10, TimeUnit.NANOSECONDS); sample.stop(timer); clock(registry).add(step()); assertAll(() -> assertEquals(1L, timer.count()), () -> assertEquals(10, timer.totalTime(TimeUnit.NANOSECONDS), 1.0e-12)); }
@Test @DisplayName("scale samples by a fixed factor") default void scale(MeterRegistry registry) { DistributionSummary ds = DistributionSummary.builder("my.summary") .scale(2.0) .register(registry); ds.record(1); clock(registry).add(step()); assertThat(ds.totalAmount()).isEqualTo(2.0); }
@Test @DisplayName("find meters by name and value") void findMetersByValue(MeterRegistry registry) { Counter c = registry.counter("counter"); c.increment(); Timer t = registry.timer("timer"); t.record(10, TimeUnit.NANOSECONDS); clock(registry).add(step()); assertThat(registry.get("counter").counter().count()).isEqualTo(1.0); assertThat(registry.get("timer").timer().count()).isEqualTo(1L); assertThat(registry.get("timer").timer().totalTime(TimeUnit.NANOSECONDS)).isEqualTo(10.0); }
@Test @DisplayName("record durations") default void recordDuration(MeterRegistry registry) { Timer t = registry.timer("myTimer"); t.record(Duration.ofMillis(42)); clock(registry).add(step()); assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(42, t.totalTime(TimeUnit.MILLISECONDS), 1.0e-12)); }
@Test @DisplayName("increment by a non-negative amount") default void incrementAmount(MeterRegistry registry) { Counter c = registry.counter("myCounter"); c.increment(2); c.increment(0); clock(registry).add(step()); assertEquals(2L, c.count()); }
@Test public void timerRecordRunnable() { Timer t = registry.timer("foo"); t.record((Runnable) () -> clock.add(42, TimeUnit.SECONDS)); Assertions.assertEquals(1, t.count()); Assertions.assertEquals(TimeUnit.SECONDS.toNanos(42), t.totalTime()); }
@Test @DisplayName("record zero") default void recordZero(MeterRegistry registry) { DistributionSummary ds = registry.summary("my.summary"); ds.record(0); clock(registry).add(step()); assertAll(() -> assertEquals(1L, ds.count()), () -> assertEquals(0L, ds.totalAmount())); }
@Test public void timerRecordCallable() throws Exception { Timer t = registry.timer("foo"); t.record(() -> clock.add(42, TimeUnit.SECONDS)); Assertions.assertEquals(1, t.count()); Assertions.assertEquals(TimeUnit.SECONDS.toNanos(42), t.totalTime()); }
@Test @DisplayName("zero times contribute to the count of overall events but do not add to total time") default void recordZero(MeterRegistry registry) { Timer t = registry.timer("myTimer"); t.record(0, TimeUnit.MILLISECONDS); clock(registry).add(step()); assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(0L, t.totalTime(TimeUnit.NANOSECONDS))); }
@Test @DisplayName("total time and count are preserved for a single timing") default void record(MeterRegistry registry) { Timer t = registry.timer("myTimer"); t.record(42, TimeUnit.MILLISECONDS); clock(registry).add(step()); assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(42, t.totalTime(TimeUnit.MILLISECONDS), 1.0e-12)); }