Skip to content

Commit

Permalink
Fix NullPointerException in ScopeImpl.close (#29)
Browse files Browse the repository at this point in the history
* Fix NullPointerException in scope.close

* checkstyle
  • Loading branch information
mennopruijssers authored and justinjc committed May 3, 2018
1 parent f727a2e commit 5d2a5fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/src/main/java/com/uber/m3/tally/ScopeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/java/com/uber/m3/tally/ScopeImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.uber.m3.util.Duration;
import com.uber.m3.util.ImmutableMap;

import org.junit.Test;

import java.util.HashMap;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5d2a5fc

Please sign in to comment.