|
2 | 2 |
|
3 | 3 | import com.github.tomakehurst.wiremock.WireMockServer; |
4 | 4 | import com.github.tomakehurst.wiremock.client.ScenarioMappingBuilder; |
| 5 | +import com.github.tomakehurst.wiremock.http.RequestMethod; |
5 | 6 | import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder; |
6 | 7 | import com.github.tomakehurst.wiremock.stubbing.Scenario; |
7 | 8 | import hudson.util.LogTaskListener; |
8 | 9 | import hudson.util.RingBufferLogHandler; |
| 10 | +import org.jenkinsci.plugins.github.config.GitHubServerConfig; |
9 | 11 | import org.junit.Test; |
10 | 12 | import org.junit.Before; |
11 | 13 | import org.kohsuke.github.GitHub; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.mockito.Mockito; |
12 | 16 |
|
13 | 17 | import java.time.LocalDateTime; |
14 | 18 | import java.time.ZoneId; |
|
24 | 28 | import static com.github.tomakehurst.wiremock.client.WireMock.get; |
25 | 29 | import static com.github.tomakehurst.wiremock.client.WireMock.resetAllScenarios; |
26 | 30 | import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 31 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
27 | 32 |
|
28 | 33 | public class ApiRateLimitCheckerTest extends AbstractGitHubWireMockTest { |
29 | 34 |
|
@@ -168,6 +173,51 @@ public void ThrottleOnNormalizeTestWithQuota() throws Exception { |
168 | 173 | assertEquals(100, getRequestCount(githubApi)); |
169 | 174 | } |
170 | 175 |
|
| 176 | + /** |
| 177 | + * Verify that "NoThrottle" does not contact the GitHub api nor output any logs |
| 178 | + * |
| 179 | + * @author Marc Salles Navarro |
| 180 | + */ |
| 181 | + @Test |
| 182 | + public void NoThrottleTestShouldNotThrottle() throws Exception { |
| 183 | + // set up scenarios |
| 184 | + List<RateLimit> scenarios = new ArrayList<>(); |
| 185 | + int limit = 5000; |
| 186 | + // Keep decreasing quota until there's none left |
| 187 | + for (int i = 500; i >= 0; i--) { |
| 188 | + scenarios.add(new RateLimit(limit, i, soon)); |
| 189 | + } |
| 190 | + setupStubs(scenarios); |
| 191 | + ApiRateLimitChecker.NoThrottle.checkApiRateLimit(listener, github); |
| 192 | + // there should be no output |
| 193 | + assertEquals(0, countOfOutputLines(m -> m.matches(".*[sS]leeping.*"))); |
| 194 | + // github rate_limit endpoint should not be contacted |
| 195 | + assertEquals(0, getRequestCount(githubApi)); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Verify that "NoThrottle" falls back to "ThrottleOnOver" if using GitHub.com |
| 200 | + * |
| 201 | + * @author Marc Salles Navarro |
| 202 | + */ |
| 203 | + @Test |
| 204 | + public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws Exception { |
| 205 | + GitHub spy = Mockito.spy(github); |
| 206 | + Mockito.when(spy.getApiUrl()).thenReturn(GitHubServerConfig.GITHUB_URL).thenReturn(github.getApiUrl()); |
| 207 | + // set up scenarios |
| 208 | + List<RateLimit> scenarios = new ArrayList<>(); |
| 209 | + int limit = 5000; |
| 210 | + int buffer = ApiRateLimitChecker.calculateBuffer(limit); |
| 211 | + scenarios.add(new RateLimit(limit, buffer -1, soon)); |
| 212 | + scenarios.add(new RateLimit(limit, limit, new Date(soon.getTime() + 2000))); |
| 213 | + setupStubs(scenarios); |
| 214 | + ApiRateLimitChecker.NoThrottle.checkApiRateLimit(listener, spy); |
| 215 | + |
| 216 | + assertEquals(1, countOfOutputLines(m -> m.matches(".*[sS]leeping.*"))); |
| 217 | + // github rate_limit endpoint should be contacted by ThrottleOnOver |
| 218 | + assertEquals(3, getRequestCount(githubApi)); |
| 219 | + } |
| 220 | + |
171 | 221 | /** |
172 | 222 | * Verify exactly when the throttle is occurring in "OnOver" |
173 | 223 | * |
@@ -198,7 +248,7 @@ public void ThrottleOnOverTest() throws Exception { |
198 | 248 | } |
199 | 249 |
|
200 | 250 | //should be no output |
201 | | - assertEquals(0, countOfOutputLines(m -> m.matches("[sS]leeping"))); |
| 251 | + assertEquals(0, countOfOutputLines(m -> m.matches(".*[sS]leeping.*"))); |
202 | 252 |
|
203 | 253 | assertEquals(11, getRequestCount(githubApi)); |
204 | 254 |
|
@@ -491,7 +541,7 @@ public void NormalizeExpectedIdealOverTime() throws Exception { |
491 | 541 | // Making sure the budgets are correct |
492 | 542 | assertEquals(12, countOfOutputLinesContaining("0 under budget")); |
493 | 543 | // no occurrences of sleeping |
494 | | - assertEquals(0, countOfOutputLines(m -> m.matches("[sS]leeping"))); |
| 544 | + assertEquals(0, countOfOutputLines(m -> m.matches(".*[sS]leeping.*"))); |
495 | 545 | } |
496 | 546 |
|
497 | 547 | /** |
|
0 commit comments