@Test
public void testAtMostEvery() {

  FakeLoggerBackend backend = new FakeLoggerBackend();
  TestLogger logger = TestLogger.create(backend);
  // Logging occurs at: +0ms, +2400ms, +4800ms
  // Note it will not occur at 4200ms (which is the first logging attempt after the
  // 2nd multiple of 2 seconds because the timestamp is reset to be (start + 2400ms)
  // and not (start + 2000ms). atMostEvery() does not rate limit over multiple samples.
  long startNanos = MILLISECONDS.toNanos(System.currentTimeMillis());
  for (int millis = 0, count = 0; millis <= 5000; millis += 600) {
    long timestampNanos = startNanos + MILLISECONDS.toNanos(millis);
    logger.at(INFO, timestampNanos).atMostEvery(2, SECONDS).log("Count=%d", count++);
  }
  assertThat(backend.getLoggedCount()).isEqualTo(3);
  backend
      .assertLogged(0)
      .metadata()
      .containsUniqueEntry(Key.LOG_AT_MOST_EVERY, LogSiteStats.newRateLimitPeriod(2, SECONDS));
  backend.assertLogged(0).hasArguments(0);
  backend.assertLogged(1).hasArguments(4);
  backend.assertLogged(2).hasArguments(8);
}