You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RatioBasedRateLimitCheckerTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.alm.client;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.runner.RunWith;
  27. import org.slf4j.event.Level;
  28. import org.sonar.api.testfixtures.log.LogTester;
  29. import static java.lang.String.format;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. import static org.mockito.Mockito.mock;
  32. import static org.mockito.Mockito.when;
  33. import static org.sonar.alm.client.RatioBasedRateLimitChecker.RATE_RATIO_EXCEEDED_MESSAGE;
  34. @RunWith(DataProviderRunner.class)
  35. public class RatioBasedRateLimitCheckerTest {
  36. @Rule
  37. public LogTester logTester = new LogTester();
  38. private static final long MILLIS_BEFORE_RESET = 100L;
  39. public static final int TIME_UNTIL_RESET = 2;
  40. RatioBasedRateLimitChecker ratioBasedRateLimitChecker = new RatioBasedRateLimitChecker();
  41. @DataProvider
  42. public static Object[][] rates() {
  43. return new Object[][] {
  44. {10000, 100000, false},
  45. {10000, 10000, false},
  46. {10000, 9999, false},
  47. {10000, 9900, false},
  48. {10000, 1001, false},
  49. {10000, 1000, true},
  50. {10000, 500, true},
  51. {10000, 0, true},
  52. };
  53. }
  54. @Test
  55. @UseDataProvider("rates")
  56. public void checkRateLimit(int limit, int remaining, boolean rateLimitShouldBeExceeded) throws InterruptedException {
  57. ApplicationHttpClient.RateLimit record = mock();
  58. when(record.limit()).thenReturn(limit);
  59. when(record.remaining()).thenReturn(remaining);
  60. when(record.reset()).thenReturn(System.currentTimeMillis() / 1000 + TIME_UNTIL_RESET);
  61. long start = System.currentTimeMillis();
  62. boolean result = ratioBasedRateLimitChecker.checkRateLimit(record);
  63. long stop = System.currentTimeMillis();
  64. long totalTime = stop - start;
  65. if (rateLimitShouldBeExceeded) {
  66. assertThat(result).isTrue();
  67. assertThat(stop).isGreaterThanOrEqualTo(record.reset());
  68. assertThat(logTester.logs(Level.WARN)).contains(
  69. format(RATE_RATIO_EXCEEDED_MESSAGE.replaceAll("\\{\\}", "%s"), limit - remaining, limit));
  70. } else {
  71. assertThat(result).isFalse();
  72. assertThat(totalTime).isLessThan(MILLIS_BEFORE_RESET);
  73. assertThat(logTester.logs(Level.WARN)).isEmpty();
  74. }
  75. }
  76. }