|
7 | 7 | import com.github.tomakehurst.wiremock.stubbing.Scenario; |
8 | 8 | import hudson.util.LogTaskListener; |
9 | 9 | import hudson.util.RingBufferLogHandler; |
| 10 | +import org.jenkinsci.plugins.github.config.GitHubServerConfig; |
10 | 11 | import org.junit.Test; |
11 | 12 | import org.junit.Before; |
12 | 13 | import org.kohsuke.github.GitHub; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.mockito.Mockito; |
13 | 16 |
|
14 | 17 | import java.time.LocalDateTime; |
15 | 18 | import java.time.ZoneId; |
@@ -187,11 +190,34 @@ public void NoThrottleTestShouldNotThrottle() throws Exception { |
187 | 190 | setupStubs(scenarios); |
188 | 191 | ApiRateLimitChecker.NoThrottle.checkApiRateLimit(listener, github); |
189 | 192 | // there should be no output |
190 | | - assertEquals(0, countOfOutputLines(m -> m.matches("[sS]leeping"))); |
| 193 | + assertEquals(0, countOfOutputLines(m -> m.matches(".*[sS]leeping.*"))); |
191 | 194 | // github rate_limit endpoint should not be contacted |
192 | 195 | assertEquals(0, getRequestCount(githubApi)); |
193 | 196 | } |
194 | 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 | + |
195 | 221 | /** |
196 | 222 | * Verify exactly when the throttle is occurring in "OnOver" |
197 | 223 | * |
@@ -222,7 +248,7 @@ public void ThrottleOnOverTest() throws Exception { |
222 | 248 | } |
223 | 249 |
|
224 | 250 | //should be no output |
225 | | - assertEquals(0, countOfOutputLines(m -> m.matches("[sS]leeping"))); |
| 251 | + assertEquals(0, countOfOutputLines(m -> m.matches(".*[sS]leeping.*"))); |
226 | 252 |
|
227 | 253 | assertEquals(11, getRequestCount(githubApi)); |
228 | 254 |
|
@@ -515,7 +541,7 @@ public void NormalizeExpectedIdealOverTime() throws Exception { |
515 | 541 | // Making sure the budgets are correct |
516 | 542 | assertEquals(12, countOfOutputLinesContaining("0 under budget")); |
517 | 543 | // no occurrences of sleeping |
518 | | - assertEquals(0, countOfOutputLines(m -> m.matches("[sS]leeping"))); |
| 544 | + assertEquals(0, countOfOutputLines(m -> m.matches(".*[sS]leeping.*"))); |
519 | 545 | } |
520 | 546 |
|
521 | 547 | /** |
|
0 commit comments