### Question:
CentralizedRule implements Rule, Comparable<CentralizedRule> { public SamplingStatisticsDocument snapshot(Date now) { SamplingStatisticsDocument s = new SamplingStatisticsDocument() .withRuleName(name) .withTimestamp(now); lock.writeLock().lock(); try { s.setRequestCount(statistics.getRequests()); s.setSampledCount(statistics.getSampled()); s.setBorrowCount(statistics.getBorrowed()); statistics.reset(); } finally { lock.writeLock().unlock(); } return s; } CentralizedRule(SamplingRule input, Rand rand); boolean update(SamplingRule i); boolean isStale(Instant now); static boolean isValid(SamplingRule rule); void update(SamplingTargetDocument t, Instant now); SamplingStatisticsDocument snapshot(Date now); boolean match(SamplingRequest r); @Override SamplingResponse sample(Instant now); @Override int compareTo(CentralizedRule other); String getName(); @Override boolean equals(@Nullable Object o); @Override int hashCode(); static final String DEFAULT_RULE_NAME; }### Answer:
@Test public void testSnapshot() { Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault()); SamplingRule input = createInput("r1", 300, 10, 0.0); CentralizedRule rule = new CentralizedRule(input, new RandImpl()); SamplingTargetDocument target = createTarget(2, 0.0, 1500000010); rule.update(target, clock.instant()); rule.sample(clock.instant()); Statistics s = Whitebox.getInternalState(rule, "statistics", CentralizedRule.class); Assert.assertEquals(1, s.getSampled()); Assert.assertEquals(1, s.getRequests()); Assert.assertEquals(0, s.getBorrowed()); SamplingStatisticsDocument snapshot = rule.snapshot(Date.from(clock.instant())); Assert.assertEquals("r1", snapshot.getRuleName()); Assert.assertEquals(TimeUnit.SECONDS.toMillis(1500000000), snapshot.getTimestamp().toInstant().toEpochMilli()); Assert.assertEquals(1, snapshot.getRequestCount().intValue()); Assert.assertEquals(1, snapshot.getSampledCount().intValue()); Assert.assertEquals(0, snapshot.getBorrowCount().intValue()); Assert.assertEquals(0, rule.snapshot(Date.from(clock.instant())).getRequestCount().intValue()); Assert.assertEquals(0, rule.snapshot(Date.from(clock.instant())).getSampledCount().intValue()); Assert.assertEquals(0, rule.snapshot(Date.from(clock.instant())).getBorrowCount().intValue()); }