PollableInfoWrapper extends InfoWrapper { public boolean isAutoPolledAllowed(long stepMills) { if (suggestion == null) { return true; } if (suggestion == AutoPollSuggestion.NEVEL_AUTO_POLL) { return false; } if (suggestion == AutoPollSuggestion.POLL_WHEN_UPDATED) { if (lastPolledTime <= ((AutoPollFriendlyInfo) info).lastModifiedTime()) { lastPolledTime = clock.wallTime(); return true; } return false; } if (suggestion.intervalMills() < stepMills) { return true; } if (lastPolledTime <= 0 || (clock.wallTime() - lastPolledTime >= suggestion.intervalMills())) { lastPolledTime = clock.wallTime(); return true; } return false; } PollableInfoWrapper(Id id, Info info, Clock clock); boolean isAutoPolledAllowed(long stepMills); @Override Indicator measure();  }
@Test public void testAutoPollSuggestion_POLL_WHEN_UPDATED() { ManualClock clock = new ManualClock(); Registry r = new DefaultRegistry(clock); PollableInfoWrapper pollableInfoWrapper = new PollableInfoWrapper(r.createId("name"), new AutoPollFriendlyInfo() { @Override public AutoPollSuggestion autoPollSuggest() { return POLL_WHEN_UPDATED; } @Override public long lastModifiedTime() { return 1; } @Override public String value() { return "x"; } }, clock); Assert.assertTrue(pollableInfoWrapper.isAutoPolledAllowed(1000)); Assert.assertTrue(pollableInfoWrapper.isAutoPolledAllowed(9000)); }
@Test public void testAutoPollSuggestionAfterInterval() { ManualClock clock = new ManualClock(); clock.setWallTime(1); Registry r = new DefaultRegistry(clock); PollableInfoWrapper pollableInfoWrapper = new PollableInfoWrapper(r.createId("name"), new AutoPollFriendlyInfo() { @Override public AutoPollSuggestion autoPollSuggest() { return new AutoPollSuggestion(2, TimeUnit.SECONDS); } @Override public long lastModifiedTime() { return 1; } @Override public String value() { return "x"; } }, clock); Assert.assertTrue(pollableInfoWrapper.isAutoPolledAllowed(1000)); clock.setWallTime(3000); Assert.assertTrue(pollableInfoWrapper.isAutoPolledAllowed(2000)); }