WindowedMapGauge extends Gauge<Map<String,Object>> { public void update(String src, V val){ if (src == null) { throw new NullPointerException("Null source"); } Windows newWindows = getWindowStartTimes(clock.currentTimeMillis()); if (!newWindows.equals(windows)) { prevWindowMap = newWindows.prevStartMs == windows.activeStartMs? curWindowMap : new HashMap<>(); curWindowMap = new HashMap<>(); windows = newWindows; } curWindowMap.merge(src, val, mergeFunc); } WindowedMapGauge(String name, long windowDurationMs, BiFunction<V,V,V> mergeFunc);  WindowedMapGauge(String name, long windowDurationMs, BiFunction<V,V,V> mergeFunc, Clock clock); void update(String src, V val); @Override Map<String,Object> getValue(); Map<String,V> getSnapshot(); Windows getWindowStartTimes(long tsMs);  }
@Test(expected = NullPointerException.class) public void testUpdateWithNull() { WindowedMapGauge<Long> gauge = new WindowedMapGauge<>("wtf", 60000L, Long::max); gauge.update(null, 10L); }
@Test public void testUpdate() throws Exception { final long windowMs = 60000L; when(clock.currentTimeMillis()).thenReturn(0L); WindowedMapGauge<Long> gauge = new WindowedMapGauge<>("wtf", windowMs, Long::max, clock); gauge.update("key1", 5L); gauge.update("key1", 6L); gauge.update("key1", 4L); assertEquals(0, ((Map) gauge.getValue().get("data")).size()); when(clock.currentTimeMillis()).thenReturn(windowMs); assertEquals(0, ((Map) gauge.getValue().get("data")).size()); gauge.update("key1", 20L); assertEquals(6L, ((Map<String, Long>) gauge.getValue().get("data")).get("key1").longValue()); when(clock.currentTimeMillis()).thenReturn(windowMs*2); assertEquals(6L, ((Map<String, Long>) gauge.getValue().get("data")).get("key1").longValue()); gauge.update("key1", 99L); assertEquals(20L, ((Map<String, Long>) gauge.getValue().get("data")).get("key1").longValue()); when(clock.currentTimeMillis()).thenReturn(windowMs); assertEquals(20L, ((Map<String, Long>) gauge.getValue().get("data")).get("key1").longValue()); when(clock.currentTimeMillis()).thenReturn(windowMs*4); assertEquals(0, ((Map) gauge.getValue().get("data")).size()); }