Skip to content

Commit

Permalink
Test changes to support github-api 1.318 (#749)
Browse files Browse the repository at this point in the history
* Update pom.xml with 1.318

* Fix tests

* Revert hard coded github-api version
  • Loading branch information
bitwiseman authored Nov 27, 2023
1 parent 90e17c4 commit c201a02
Showing 1 changed file with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ private void setupStubs(List<RateLimit> scenarios) throws Exception {
githubApi.stubFor(get(urlEqualTo("/meta"))
.willReturn(aResponse().withStatus(200).withBody("{\"verifiable_password_authentication\": false}")));

githubApi.stubFor(get(urlEqualTo("/licenses/MIT"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{" + "\"key\": \"mit\"," + "\"name\": \"MIT License\"}")));

githubApi.stubFor(get(urlEqualTo("/"))
.willReturn(aResponse()
.withStatus(200)
Expand Down Expand Up @@ -174,15 +179,15 @@ public void NoCheckerConfigured() throws Exception {
setupStubs(scenarios);

GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
github.getMeta();
github.getLicense("MIT");
ApiRateLimitChecker.resetLocalChecker();

GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
github.getMeta();
github.getLicense("MIT");
ApiRateLimitChecker.resetLocalChecker();

GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.NoThrottle);
github.getMeta();
github.getLicense("MIT");

assertEquals(3, countOfOutputLinesContaining("LocalChecker for rate limit was not set for this thread."));
assertEquals(3, countOfOutputLinesContaining("with API URL 'https://api.github.com'"));
Expand Down Expand Up @@ -249,7 +254,7 @@ public void ThrottleOnOverTestWithQuota() throws Exception {
// Given a full rate limit quota, then we expect no throttling
// Also only 1 call to get rate limit, since rate limit record is valid for a while
for (int i = 0; i < 100; i++) {
github.getMeta();
github.getLicense("MIT");
}

assertEquals(0, countOfOutputLinesContaining("Sleeping"));
Expand Down Expand Up @@ -277,7 +282,7 @@ public void ThrottleOnNormalizeTestWithQuota() throws Exception {

// Given a full rate limit quota, then we expect no throttling
for (int i = 0; i < 100; i++) {
github.getMeta();
github.getLicense("MIT");
}

assertEquals(0, countOfOutputLinesContaining("Sleeping"));
Expand All @@ -303,7 +308,7 @@ public void NoThrottleTestShouldNotThrottle() throws Exception {
ApiRateLimitChecker.configureThreadLocalChecker(listener, github);

for (int i = 0; i < 100; i++) {
github.getMeta();
github.getLicense("MIT");
}

// there should be no output
Expand Down Expand Up @@ -331,6 +336,8 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {

ApiRateLimitChecker.configureThreadLocalChecker(listener, github);

// sanity cached to limit queries to once per second
Thread.sleep(1001);
github.getMeta();

// The core should be unknown, but different from initial
Expand Down Expand Up @@ -368,6 +375,8 @@ public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws

ApiRateLimitChecker.configureThreadLocalChecker(listener, spy);

// sanity cached to limit queries to once per second
Thread.sleep(1001);
spy.getMeta();

assertEquals(1, countOfOutputLinesContaining("ThrottleOnOver will be used instead"));
Expand Down Expand Up @@ -408,6 +417,8 @@ public void ThrottleOnOverTest() throws Exception {
// does not happen until under buffer
for (int i = 0; i < 11; i++) {
assertFalse(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 0));
// sanity cached to limit queries to once per second
Thread.sleep(1001);
}

// should be no output
Expand All @@ -418,10 +429,14 @@ public void ThrottleOnOverTest() throws Exception {
// check rate limit to hit the next 9 scenarios
for (int i = 0; i < 10; i++) {
assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), i));
// sanity cached to limit queries to once per second
Thread.sleep(1001);
}
// This simulates the waiting until refreshed
currentChecker.resetExpiration();
assertFalse(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 10));
// sanity cached to limit queries to once per second
Thread.sleep(1001);

// output for all the throttled scenarios. Sleeps normally on the first and then the `notify`
// hits the next 9
Expand Down Expand Up @@ -492,6 +507,8 @@ public void ThrottleForNormalizeTestWithinIdeal() throws Exception {

// First check will not say under budget (add counts)
github.getMeta();
// sanity cached to limit queries to once per second
Thread.sleep(1001);

assertEquals(4, getRequestCount(githubApi));
// Feature removed, no output for under budget
Expand All @@ -503,13 +520,17 @@ public void ThrottleForNormalizeTestWithinIdeal() throws Exception {
// check rate limit to hit the next 6 scenarios
for (int i = 0; i < 6; i++) {
assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), i));
// sanity cached to limit queries to once per second
Thread.sleep(1001);
}

assertEquals(initialRequestCount + 5, handler.getView().size());

// This simulates the waiting until refreshed
currentChecker.resetExpiration();
assertFalse(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 9));
// sanity cached to limit queries to once per second
Thread.sleep(1001);

assertEquals(initialRequestCount + 9, getRequestCount(githubApi));
// Functionality removed
Expand Down Expand Up @@ -557,11 +578,18 @@ public void NormalizeThrottleWithBurnedBuffer() throws Exception {
ApiRateLimitChecker.LocalChecker currentChecker = ApiRateLimitChecker.getLocalChecker();

assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 0));
// sanity cached to limit queries to once per second
Thread.sleep(1001);
assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 1));
Thread.sleep(1001);
assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 2));
Thread.sleep(1001);
assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 3));
Thread.sleep(1001);
assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 4));
Thread.sleep(1001);
assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 5));
Thread.sleep(1001);

// Expect a triggered throttle for normalize
// GitHubRateLimitChecker add 1 second to notification loop, this hides the entropy value
Expand Down Expand Up @@ -618,6 +646,8 @@ public void OnOverThrottleTimingRateLimitCheck() throws Exception {
github.getRateLimit();
// calls rateLimit() for first loop so we have to getRateLimit() for each loop
github.getMeta();
// sanity cached to limit queries to once per second
Thread.sleep(1001);
}

// (rate_limit + meta) x 6
Expand All @@ -631,10 +661,14 @@ public void OnOverThrottleTimingRateLimitCheck() throws Exception {
// check rate limit to hit the next 5 scenarios
for (int i = 0; i < 5; i++) {
assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), i));
// sanity cached to limit queries to once per second
Thread.sleep(1001);
}
// This simulates the waiting until refreshed
currentChecker.resetExpiration();
assertFalse(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 5));
// sanity cached to limit queries to once per second
Thread.sleep(1001);

assertEquals(initialRequestCount + 18, getRequestCount(githubApi));

Expand Down Expand Up @@ -689,6 +723,8 @@ public void NormalizeThrottleTimingRateLimitCheck() throws Exception {
ApiRateLimitChecker.configureThreadLocalChecker(listener, github);

github.getMeta();
// sanity cached to limit queries to once per second
Thread.sleep(1001);

// start timing
long start = System.currentTimeMillis();
Expand All @@ -697,11 +733,16 @@ public void NormalizeThrottleTimingRateLimitCheck() throws Exception {
ApiRateLimitChecker.LocalChecker currentChecker = ApiRateLimitChecker.getLocalChecker();

assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 0));
// sanity cached to limit queries to once per second
Thread.sleep(1001);
assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 1));
Thread.sleep(1001);
assertTrue(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 2));
Thread.sleep(1001);
// This simulates the waiting until refreshed
currentChecker.resetExpiration();
assertFalse(currentChecker.checkRateLimit(github.getRateLimit().getCore(), 3));
Thread.sleep(1001);

assertEquals(initialRequestCount + 6, getRequestCount(githubApi));

Expand Down Expand Up @@ -777,6 +818,8 @@ public void NormalizeExpectedIdealOverTime() throws Exception {
}
// calls rateLimit() for first loop so we have to getRateLimit() for each loop
github.getMeta();
// sanity cached to limit queries to once per second
Thread.sleep(1001);
}

// rate_limit + meta x 12
Expand Down Expand Up @@ -829,6 +872,8 @@ public void OnOverExpectedIdealOverTime() throws Exception {
}
// calls rateLimit() for first loop so we have to getRateLimit() for each loop
github.getMeta();
// sanity cached to limit queries to once per second
Thread.sleep(1001);
}

assertEquals(12, getRequestCount(githubApi));
Expand Down

0 comments on commit c201a02

Please sign in to comment.