From 5d2a5fc7ede67a646824e42ed33c5ae23500e63c Mon Sep 17 00:00:00 2001 From: Menno Pruijssers Date: Thu, 3 May 2018 16:59:03 +0200 Subject: [PATCH] Fix NullPointerException in ScopeImpl.close (#29) * Fix NullPointerException in scope.close * checkstyle --- core/src/main/java/com/uber/m3/tally/ScopeImpl.java | 6 ++++-- .../test/java/com/uber/m3/tally/ScopeImplTest.java | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/uber/m3/tally/ScopeImpl.java b/core/src/main/java/com/uber/m3/tally/ScopeImpl.java index 5f8a380..e10086e 100644 --- a/core/src/main/java/com/uber/m3/tally/ScopeImpl.java +++ b/core/src/main/java/com/uber/m3/tally/ScopeImpl.java @@ -172,8 +172,10 @@ public void close() { // all metrics. reportLoopIteration(); - // Now that all metrics should be known to the reporter, close the reporter - reporter.close(); + if (reporter != null) { + // Now that all metrics should be known to the reporter, close the reporter + reporter.close(); + } } /** diff --git a/core/src/test/java/com/uber/m3/tally/ScopeImplTest.java b/core/src/test/java/com/uber/m3/tally/ScopeImplTest.java index fb6d257..2d43317 100644 --- a/core/src/test/java/com/uber/m3/tally/ScopeImplTest.java +++ b/core/src/test/java/com/uber/m3/tally/ScopeImplTest.java @@ -22,6 +22,7 @@ import com.uber.m3.util.Duration; import com.uber.m3.util.ImmutableMap; + import org.junit.Test; import java.util.HashMap; @@ -109,6 +110,16 @@ public void close() { assertEquals(123, reporter.nextGaugeVal(), EPSILON); } + @Test + public void closeWithoutReporter() throws ScopeCloseException { + try (Scope scope = new RootScopeBuilder().reportEvery(Duration.ofMinutes(1))) { + // Create a gauge, update it, and let the AutoCloseable interface + // functionality close the scope right away. + Gauge shortLifeGauge = scope.gauge("shortLifeGauge"); + shortLifeGauge.update(123); + } + } + @Test public void subscopes() { final int REPORT_INTERVAL_MILLIS = 10;