PollingThrottlerTest.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.provectus.kafka.ui.util;
  2. import static org.assertj.core.api.Assertions.assertThat;
  3. import static org.assertj.core.data.Percentage.withPercentage;
  4. import com.google.common.base.Stopwatch;
  5. import com.google.common.util.concurrent.RateLimiter;
  6. import com.provectus.kafka.ui.emitter.PollingThrottler;
  7. import java.util.concurrent.ThreadLocalRandom;
  8. import java.util.concurrent.TimeUnit;
  9. import org.junit.jupiter.api.Test;
  10. class PollingThrottlerTest {
  11. @Test
  12. void testTrafficThrottled() {
  13. var throttler = new PollingThrottler("test", RateLimiter.create(1000));
  14. long polledBytes = 0;
  15. var stopwatch = Stopwatch.createStarted();
  16. while (stopwatch.elapsed(TimeUnit.SECONDS) < 1) {
  17. int newPolled = ThreadLocalRandom.current().nextInt(10);
  18. throttler.throttleAfterPoll(newPolled);
  19. polledBytes += newPolled;
  20. }
  21. assertThat(polledBytes).isCloseTo(1000, withPercentage(3.0));
  22. }
  23. @Test
  24. void noopThrottlerDoNotLimitPolling() {
  25. var noopThrottler = PollingThrottler.noop();
  26. var stopwatch = Stopwatch.createStarted();
  27. // emulating that we polled 1GB
  28. for (int i = 0; i < 1024; i++) {
  29. noopThrottler.throttleAfterPoll(1024 * 1024);
  30. }
  31. // checking that were are able to "poll" 1GB in less than a second
  32. assertThat(stopwatch.elapsed().getSeconds()).isLessThan(1);
  33. }
  34. }