HealthGauge { public synchronized HealthSnapshot getHealth(long timePeriod, TimeUnit timeUnit, long nanoTime) { long total = 0; long failures = 0; for (InternalGauge<?> gauge : gauges) { gauge.refreshHealth(timePeriod, timeUnit, nanoTime); total += gauge.total; failures += gauge.failures; } return new HealthSnapshot(total, failures); }  synchronized HealthSnapshot getHealth(long timePeriod, TimeUnit timeUnit, long nanoTime); void add(Rolling<PartitionedCount<Result>> metrics);  }
@Test public void requestedTimeIsDelegatedToIntervals() { when(rolling1.intervals(30L)).thenReturn(intervals1); when(rolling2.intervals(30L)).thenReturn(intervals2); healthGauge.getHealth(10L, TimeUnit.NANOSECONDS, 30L); verify(intervals1).limit(10L, TimeUnit.NANOSECONDS); verify(intervals2).limit(10L, TimeUnit.NANOSECONDS); }
@Test public void snapshotReflectsMetrics() { PartitionedCount<SimpleResult> count1 = new LongAdderCounter<>(SimpleResult.class); addSimpleResults(count1, 5, 7); PartitionedCount<SimpleResult> count2 = new LongAdderCounter<>(SimpleResult.class); addSimpleResults(count2, 10, 3); PartitionedCount<SimpleResult> count3 = new LongAdderCounter<>(SimpleResult.class); addSimpleResults(count3, 2, 0); PartitionedCount<TimeoutableResult> count4 = new LongAdderCounter<>(TimeoutableResult.class); addTimeoutableResults(count4, 18, 2, 7); PartitionedCount<TimeoutableResult> count5 = new LongAdderCounter<>(TimeoutableResult.class); addTimeoutableResults(count5, 9, 1, 0); when(rolling1.intervals(30L)).thenReturn(intervals1); when(rolling2.intervals(30L)).thenReturn(intervals2); when(intervals1.hasNext()).thenReturn(true, true, true, false); when(intervals1.next()).thenReturn(count1, count2, count3); when(intervals2.hasNext()).thenReturn(true, true, false); when(intervals2.next()).thenReturn(count4, count5); HealthSnapshot health = healthGauge.getHealth(10L, TimeUnit.NANOSECONDS, 30L); assertEquals(64, health.total); assertEquals(20, health.failures); assertEquals(100 * 20 / 64, health.failurePercentage); }
@Test public void oldValuesAreRefreshed() { PartitionedCount<SimpleResult> count1 = new LongAdderCounter<>(SimpleResult.class); addSimpleResults(count1, 5, 7); PartitionedCount<SimpleResult> count2 = new LongAdderCounter<>(SimpleResult.class); addSimpleResults(count2, 10, 3); PartitionedCount<SimpleResult> count3 = new LongAdderCounter<>(SimpleResult.class); addSimpleResults(count3, 2, 0); PartitionedCount<TimeoutableResult> count4 = new LongAdderCounter<>(TimeoutableResult.class); addTimeoutableResults(count4, 18, 2, 7); PartitionedCount<TimeoutableResult> count5 = new LongAdderCounter<>(TimeoutableResult.class); addTimeoutableResults(count5, 9, 1, 0); when(rolling1.intervals(30L)).thenReturn(intervals1); when(rolling2.intervals(30L)).thenReturn(intervals2); when(intervals1.hasNext()).thenReturn(true, true, false, true, false); when(intervals1.next()).thenReturn(count1, count2, count3); when(intervals2.hasNext()).thenReturn(true, false, true, false); when(intervals2.next()).thenReturn(count4, count5); healthGauge.getHealth(10L, TimeUnit.NANOSECONDS, 30L); HealthSnapshot health = healthGauge.getHealth(10L, TimeUnit.NANOSECONDS, 30L); assertEquals(12, health.total); assertEquals(1, health.failures); assertEquals(100 * 1 / 12, health.failurePercentage); }