@Test void tryAcquire() { LocalTime time = mock(LocalTime.class); when(time.timestampMs()).thenReturn(INIT_MS); RateLimiter limiter = new RateLimiter(WINDOW, time); assertThat(limiter.tryAcquire()).isTrue(); assertThat(limiter.tryAcquire()).isFalse(); assertThat(limiter.tryAcquire()).isFalse(); when(time.timestampMs()).thenReturn(INIT_MS + WINDOW); assertThat(limiter.tryAcquire()).isTrue(); assertThat(limiter.tryAcquire()).isFalse(); assertThat(limiter.tryAcquire()).isFalse(); }
public boolean tryAcquire() { synchronized (lock) { long timeStamp = time.timestampMs(); if (lastTimestampMs + requiredWindowMs <= timeStamp || UNINITIALIZED == lastTimestampMs) { lastTimestampMs = timeStamp; return true; } return false; } }
RateLimiter { public boolean tryAcquire() { synchronized (lock) { long timeStamp = time.timestampMs(); if (lastTimestampMs + requiredWindowMs <= timeStamp || UNINITIALIZED == lastTimestampMs) { lastTimestampMs = timeStamp; return true; } return false; } } }
RateLimiter { public boolean tryAcquire() { synchronized (lock) { long timeStamp = time.timestampMs(); if (lastTimestampMs + requiredWindowMs <= timeStamp || UNINITIALIZED == lastTimestampMs) { lastTimestampMs = timeStamp; return true; } return false; } }  }
RateLimiter { public boolean tryAcquire() { synchronized (lock) { long timeStamp = time.timestampMs(); if (lastTimestampMs + requiredWindowMs <= timeStamp || UNINITIALIZED == lastTimestampMs) { lastTimestampMs = timeStamp; return true; } return false; } }  boolean tryAcquire(); }
RateLimiter { public boolean tryAcquire() { synchronized (lock) { long timeStamp = time.timestampMs(); if (lastTimestampMs + requiredWindowMs <= timeStamp || UNINITIALIZED == lastTimestampMs) { lastTimestampMs = timeStamp; return true; } return false; } }  boolean tryAcquire();  }