From 4f6fc573089318070ce120e2a06aa0df5fd5ea31 Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Mon, 21 Oct 2024 23:43:58 +0200 Subject: [PATCH] fix: NoopTimer#time(Runnable) should run the Runnable (#4540) --- .../java/com/codahale/metrics/NoopMetricRegistry.java | 2 +- .../com/codahale/metrics/NoopMetricRegistryTest.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/metrics-core/src/main/java/com/codahale/metrics/NoopMetricRegistry.java b/metrics-core/src/main/java/com/codahale/metrics/NoopMetricRegistry.java index db65193074..5af6e94d81 100644 --- a/metrics-core/src/main/java/com/codahale/metrics/NoopMetricRegistry.java +++ b/metrics-core/src/main/java/com/codahale/metrics/NoopMetricRegistry.java @@ -432,7 +432,7 @@ public T timeSupplier(Supplier event) { */ @Override public void time(Runnable event) { - // NOP + event.run(); } /** diff --git a/metrics-core/src/test/java/com/codahale/metrics/NoopMetricRegistryTest.java b/metrics-core/src/test/java/com/codahale/metrics/NoopMetricRegistryTest.java index 700c2a0fc3..aa1c24466c 100644 --- a/metrics-core/src/test/java/com/codahale/metrics/NoopMetricRegistryTest.java +++ b/metrics-core/src/test/java/com/codahale/metrics/NoopMetricRegistryTest.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatNullPointerException; @@ -492,4 +493,13 @@ public void registerNullMetric() { .isThrownBy(() -> registry.register("any_name", null)) .withMessage("metric == null"); } + + @Test + public void timesRunnableInstances() { + final Timer timer = registry.timer("thing"); + final AtomicBoolean called = new AtomicBoolean(); + timer.time(() -> called.set(true)); + + assertThat(called).isTrue(); + } }