RateLimiter implements AutoCloseable, PermitSupplier { @Signed Acquisition tryAcquireGetDelayNanos(final int nrPermits, final long deadlineNanos) { boolean tryAcquire = tryAcquire(nrPermits); if (tryAcquire) { return Acquisition.SUCCESS; } else { return forceReserve(deadlineNanos, nrPermits); } } RateLimiter(final long permitsPerReplenishInterval,
          final Duration replenishmentInterval,
          final long maxBurstSize, final long minReplenishInterval, final TimeUnit tu);  RateLimiter(final long permitsPerReplenishInterval,
          final Duration replenishmentInterval,
          final long maxBurstSize);  RateLimiter(final long permitsPerReplenishInterval,
          final Duration replenishmentInterval,
          final long maxBurstSize,
          final ScheduledExecutorService scheduler);  RateLimiter(final long permitsPerReplenishInterval,
          final Duration replenishmentInterval,
          final long maxBurstSize,
          final ScheduledExecutorService scheduler,
          final LongSupplier nanoTimeSupplier);  RateLimiter(final long permitsPerReplenishInterval,
          final Duration replenishmentInterval,
          final long maxBurstSize,
          final long minReplenishInterval,
          final TimeUnit tu,
          final ScheduledExecutorService scheduler,
          final LongSupplier nanoTimeSupplier);  RateLimiter(final long permitsPerReplenishInterval,
          final Duration replenishmentInterval,
          final long maxAvailablePermits,
          final long minReplenishInterval,
          final TimeUnit tu,
          final ScheduledExecutorService scheduler,
          final LongSupplier nanoTimeSupplier,
          final int maxBackoffNanos);  RateLimiter(final long permitsPerReplenishInterval,
          final Duration replenishmentInterval,
          final long initialNrOfPermits,
          final long maxAvailablePermits,
          final long minReplenishInterval,
          final TimeUnit tu,
          final ScheduledExecutorService scheduler,
          final LongSupplier nanoTimeSupplier,
          final int maxBackoffNanos); @Override boolean addPermits(final int nrPermits); boolean tryAcquire(); boolean tryAcquire(final int nrPermits); long getNrPermits(); @Override @SuppressFBWarnings("MDM_THREAD_YIELD") //fb has a point here... boolean tryAcquire(final int nrPermits, final long deadlineNanos); @CheckReturnValue @Override boolean tryAcquire(@Nonnegative final int nrPermits, @Nonnegative final long timeout, final TimeUnit unit); @Override Acquisition tryAcquireEx(final int nrPermits, final long timeout, final TimeUnit unit); @Override Acquisition tryAcquireEx(final int nrPermits, final long deadlineNanos); @Override void close(); long getPermitsPerReplenishInterval(); long getPermitReplenishIntervalNanos(); long getLastReplenishmentNanos(); @Override String toString();  }
@Test public void testRateLimitInvalidTryReservation() throws InterruptedException { long nanoTime = TestTimeSource.freezeTime(); try { ScheduledExecutorService mockExec = Mockito.mock(ScheduledExecutorService.class); ScheduledFuture mockFut = Mockito.mock(ScheduledFuture.class); Mockito.when(mockExec.scheduleAtFixedRate(Mockito.any(), Mockito.anyLong(), Mockito.anyLong(), Mockito.eq(TimeUnit.NANOSECONDS))).thenReturn(mockFut); try (RateLimiter rateLimiter = new RateLimiter(10, Duration.ofNanos(10000000), 100, mockExec)) { long s1Nanos = TimeUnit.SECONDS.toNanos(1); PermitSupplier.Acquisition acq = rateLimiter.tryAcquireGetDelayNanos(1000, nanoTime + s1Nanos); Assert.assertEquals(s1Nanos, acq.permitAvailableEstimateInNanos()); } } finally { TestTimeSource.clear(); } }