MaxLongGauge { public void recordMax(long currentValue) { boolean done = false; while (!done) { long maxValue = max.get(); if (currentValue <= maxValue) { done = true; } else { done = max.compareAndSet(maxValue, currentValue); if (done) { stats.incLong(statId, currentValue - maxValue); } } } } MaxLongGauge(int statId, Statistics stats); void recordMax(long currentValue);  }
@Test public void recordMax_singleRecord() { MaxLongGauge maxLongGauge = new MaxLongGauge(statId1, fakeStatistics); maxLongGauge.recordMax(12); assertThat(fakeStatistics.getLong(statId1)).isEqualTo(12); }
@Test public void recordMax_multipleRecords() { MaxLongGauge maxLongGauge = new MaxLongGauge(statId1, fakeStatistics); maxLongGauge.recordMax(12); maxLongGauge.recordMax(13); assertThat(fakeStatistics.getLong(statId1)).isEqualTo(13); }
@Test public void recordMax_recordNothing_ifMaxIsNotExceeded() { MaxLongGauge maxLongGauge = new MaxLongGauge(statId1, fakeStatistics); maxLongGauge.recordMax(12); maxLongGauge.recordMax(11); assertThat(fakeStatistics.getLong(statId1)).isEqualTo(12); }
@Test public void recordMax_ignoresNegatives() { MaxLongGauge maxLongGauge = new MaxLongGauge(statId1, fakeStatistics); maxLongGauge.recordMax(-12); assertThat(fakeStatistics.getLong(statId1)).isEqualTo(0); }
@Test public void recordMax_ignoresZero() { MaxLongGauge maxLongGauge = new MaxLongGauge(statId1, fakeStatistics); maxLongGauge.recordMax(0); assertThat(fakeStatistics.getLong(statId1)).isEqualTo(0); }
@Test public void recordMax_usesStatId() { MaxLongGauge maxLongGauge = new MaxLongGauge(statId2, fakeStatistics); maxLongGauge.recordMax(17); assertThat(fakeStatistics.getLong(statId2)).isEqualTo(17); }