RatioGauge implements Gauge<Double> { public Double getValue() { try { Ratio r = getRatio(); if (r == null) { return (double)NOT_AVAILABLE; } double result = r.getValue(); lastUpdated = System.currentTimeMillis(); return result; } catch (Exception e) { return (double)NOT_AVAILABLE; } }  Double getValue(); @Override long lastUpdateTime();  }
@Test public void calculatesTheRatioOfTheNumeratorToTheDenominator() throws Exception { final RatioGauge regular = new RatioGauge() { @Override protected Ratio getRatio() { return RatioGauge.Ratio.of(2, 4); } }; assertThat(regular.getValue()) .isEqualTo(0.5); }
@Test public void handlesDivideByZeroIssues() throws Exception { final RatioGauge divByZero = new RatioGauge() { @Override protected Ratio getRatio() { return Ratio.of(100, 0); } }; assertThat(divByZero.getValue()) .isNaN(); }
@Test public void handlesInfiniteDenominators() throws Exception { final RatioGauge infinite = new RatioGauge() { @Override protected Ratio getRatio() { return Ratio.of(10, Double.POSITIVE_INFINITY); } }; assertThat(infinite.getValue()) .isNaN(); }
@Test public void handlesNaNDenominators() throws Exception { final RatioGauge nan = new RatioGauge() { @Override protected Ratio getRatio() { return Ratio.of(10, Double.NaN); } }; assertThat(nan.getValue()) .isNaN(); }