Guard NPE in HeartbeatMonitor

fixes gh-129
This commit is contained in:
Spencer Gibb
2016-09-09 11:40:13 -06:00
parent 3cfda6b31d
commit 23d3a2ec3d
2 changed files with 12 additions and 1 deletions

View File

@@ -34,7 +34,7 @@ public class HeartbeatMonitor {
*/
public boolean update(Object value) {
Object last = this.latestHeartbeat.get();
if (!value.equals(last)) {
if (value != null && !value.equals(last)) {
return this.latestHeartbeat.compareAndSet(last, value);
}
return false;

View File

@@ -40,4 +40,15 @@ public class HeartbeatMonitorTests {
assertTrue(this.monitor.update("bar"));
}
@Test
public void nullInitialValue() {
assertFalse(this.monitor.update(null));
}
@Test
public void nullSecondValue() {
assertTrue(this.monitor.update("foo"));
assertFalse(this.monitor.update(null));
}
}