From 5659b311e17dfcf9aa729a4f54224b7942c47cca Mon Sep 17 00:00:00 2001 From: Aurelien Poscia Date: Tue, 12 Sep 2023 11:34:46 +0200 Subject: [PATCH] SONAR-20451 Fix flakyness of RatioBasedRateLimitCheckerTest.checkRateLimit (cherry picked from commit b1b3b2270e81053663d07967b68ba027e1564996) --- .../client/github/RatioBasedRateLimitCheckerTest.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/RatioBasedRateLimitCheckerTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/RatioBasedRateLimitCheckerTest.java index 407e9056144..83913b19cb4 100644 --- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/RatioBasedRateLimitCheckerTest.java +++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/RatioBasedRateLimitCheckerTest.java @@ -23,18 +23,18 @@ import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.sql.Date; -import java.time.Instant; import java.time.temporal.ChronoUnit; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.kohsuke.github.GHRateLimit; -import org.mockito.Mockito; import org.slf4j.event.Level; import org.sonar.api.testfixtures.log.LogTester; import static java.lang.String.format; +import static java.time.Instant.now; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.alm.client.github.RatioBasedRateLimitChecker.RATE_RATIO_EXCEEDED_MESSAGE; @@ -63,18 +63,19 @@ public class RatioBasedRateLimitCheckerTest { @Test @UseDataProvider("rates") public void checkRateLimit(int limit, int remaining, boolean rateLimitShouldBeExceeded) throws InterruptedException { - GHRateLimit.Record record = Mockito.mock(GHRateLimit.Record.class); + GHRateLimit.Record record = mock(); when(record.getLimit()).thenReturn(limit); when(record.getRemaining()).thenReturn(remaining); - when(record.getResetDate()).thenReturn(Date.from(Instant.now().plus(100, ChronoUnit.MILLIS))); + when(record.getResetDate()).thenReturn(Date.from(now().plus(100, ChronoUnit.MILLIS))); long start = System.currentTimeMillis(); boolean result = ratioBasedRateLimitChecker.checkRateLimit(record, 10); long stop = System.currentTimeMillis(); long totalTime = stop - start; + if (rateLimitShouldBeExceeded) { assertThat(result).isTrue(); - assertThat(totalTime).isGreaterThanOrEqualTo(MILLIS_BEFORE_RESET - 10); + assertThat(stop).isGreaterThanOrEqualTo(record.getResetDate().getTime()); assertThat(logTester.logs(Level.WARN)).contains( format(RATE_RATIO_EXCEEDED_MESSAGE.replaceAll("\\{\\}", "%s"), limit - remaining, limit)); } else { -- 2.39.5