mirror of
https://github.com/jlengrand/github-api.git
synced 2026-04-14 00:11:23 +00:00
Integrate full rate limit checking
This commit is contained in:
@@ -9,7 +9,7 @@ import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Unit test for {@link GitHub} static helpers.
|
||||
@@ -65,68 +65,138 @@ public class GitHubStaticTest extends AbstractGitHubWireMockTest {
|
||||
@Test
|
||||
public void testGitHubRateLimitShouldReplaceRateLimit() throws Exception {
|
||||
|
||||
GHRateLimit.Record unknown0 = GHRateLimit.Unknown().getCore();
|
||||
GHRateLimit.Record unknown1 = GHRateLimit.Unknown().getCore();
|
||||
GHRateLimit.UnknownLimitRecord.unknownLimitResetSeconds = 5;
|
||||
GHRateLimit.UnknownLimitRecord.reset();
|
||||
|
||||
GHRateLimit.Record record0 = new GHRateLimit.Record(10, 10, 10L);
|
||||
GHRateLimit.Record record1 = new GHRateLimit.Record(10, 9, 10L);
|
||||
GHRateLimit.Record record2 = new GHRateLimit.Record(10, 2, 10L);
|
||||
GHRateLimit.Record record3 = new GHRateLimit.Record(10, 10, 20L);
|
||||
GHRateLimit.Record record4 = new GHRateLimit.Record(10, 5, 20L);
|
||||
GHRateLimit.Record unknown0 = GHRateLimit.Unknown("").getCore();
|
||||
|
||||
Thread.sleep(2000);
|
||||
|
||||
// To reduce object creation: There is only one valid Unknown record at a time.
|
||||
assertThat("Unknown current should should limit the creation of new unknown records",
|
||||
unknown0,
|
||||
sameInstance(GHRateLimit.Unknown("").getCore()));
|
||||
|
||||
// For testing, we create an new unknown.
|
||||
GHRateLimit.Record unknown1 = new GHRateLimit.UnknownLimitRecord();
|
||||
|
||||
assertThat("Valid unknown should not replace an existing one, regardless of created or reset time",
|
||||
unknown1.currentOrUpdated(unknown0),
|
||||
sameInstance(unknown1));
|
||||
assertThat("Valid unknown should not replace an existing one, regardless of created or reset time",
|
||||
unknown0.currentOrUpdated(unknown1),
|
||||
sameInstance(unknown0));
|
||||
|
||||
// Sleep to make different created time
|
||||
Thread.sleep(2000);
|
||||
|
||||
long epochSeconds = Instant.now().getEpochSecond();
|
||||
|
||||
GHRateLimit.Record record0 = new GHRateLimit.Record(10, 10, epochSeconds + 10L);
|
||||
GHRateLimit.Record record1 = new GHRateLimit.Record(10, 9, epochSeconds + 10L);
|
||||
GHRateLimit.Record record2 = new GHRateLimit.Record(10, 2, epochSeconds + 10L);
|
||||
GHRateLimit.Record record3 = new GHRateLimit.Record(10, 10, epochSeconds + 20L);
|
||||
GHRateLimit.Record record4 = new GHRateLimit.Record(10, 5, epochSeconds + 20L);
|
||||
GHRateLimit.Record recordExpired0 = new GHRateLimit.Record(10, 10, epochSeconds - 1L);
|
||||
GHRateLimit.Record recordExpired1 = new GHRateLimit.Record(10, 10, epochSeconds + 2L);
|
||||
|
||||
// Sleep to make expired and different created time
|
||||
Thread.sleep(3000);
|
||||
|
||||
GHRateLimit.Record recordWorst = new GHRateLimit.Record(Integer.MAX_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE);
|
||||
GHRateLimit.Record record00 = new GHRateLimit.Record(10, 10, 10L);
|
||||
GHRateLimit.Record unknown2 = GHRateLimit.Unknown().getCore();
|
||||
GHRateLimit.Record record00 = new GHRateLimit.Record(10, 10, epochSeconds + 10L);
|
||||
|
||||
GHRateLimit.Record unknownExpired0 = unknown0;
|
||||
GHRateLimit.Record unknownExpired1 = unknown1;
|
||||
unknown0 = GHRateLimit.Unknown("").getCore();
|
||||
|
||||
// Rate-limit records maybe created and returned in different orders.
|
||||
// We should update to the regular records over unknowns.
|
||||
// We should update to the unexpired regular records over unknowns.
|
||||
// After that, we should update to the candidate if its limit is lower or its reset is later.
|
||||
|
||||
assertThat("Equivalent unknown should not replace", GitHubClient.shouldReplace(unknown0, unknown1), is(false));
|
||||
assertThat("Equivalent unknown should not replace", GitHubClient.shouldReplace(unknown1, unknown0), is(false));
|
||||
assertThat("Expired unknowns should not replace another expired one, regardless of created or reset time",
|
||||
unknownExpired0.currentOrUpdated(unknownExpired1),
|
||||
sameInstance(unknownExpired0));
|
||||
assertThat("Expired unknowns should not replace another expired one, regardless of created or reset time",
|
||||
unknownExpired1.currentOrUpdated(unknownExpired0),
|
||||
sameInstance(unknownExpired1));
|
||||
|
||||
assertThat("Later unknown should replace earlier", GitHubClient.shouldReplace(unknown2, unknown0), is(true));
|
||||
assertThat("Earlier unknown should not replace later",
|
||||
GitHubClient.shouldReplace(unknown0, unknown2),
|
||||
is(false));
|
||||
assertThat(
|
||||
"Expired unknown should not be replaced by expired earlier normal record, regardless of created or reset time",
|
||||
unknownExpired0.currentOrUpdated(recordExpired0),
|
||||
sameInstance(unknownExpired0));
|
||||
assertThat(
|
||||
"Expired normal record should not be replace an expired earlier unknown record, regardless of created or reset time",
|
||||
recordExpired0.currentOrUpdated(unknownExpired0),
|
||||
sameInstance(recordExpired0));
|
||||
|
||||
assertThat("Worst record should replace later unknown",
|
||||
GitHubClient.shouldReplace(recordWorst, unknown1),
|
||||
is(true));
|
||||
assertThat("Unknown should not replace worst record",
|
||||
GitHubClient.shouldReplace(unknown1, recordWorst),
|
||||
is(false));
|
||||
assertThat(
|
||||
"Expired unknown should be replaced by expired later normal record, regardless of created or reset time",
|
||||
unknownExpired0.currentOrUpdated(recordExpired1),
|
||||
sameInstance(recordExpired1));
|
||||
assertThat(
|
||||
"Expired later normal record should not be replace an expired unknown record, regardless of created or reset time",
|
||||
recordExpired1.currentOrUpdated(unknownExpired0),
|
||||
sameInstance(recordExpired1));
|
||||
|
||||
assertThat("Earlier record should replace later worst",
|
||||
GitHubClient.shouldReplace(record0, recordWorst),
|
||||
is(true));
|
||||
assertThat("Valid unknown should not be replaced by an expired unknown",
|
||||
unknown0.currentOrUpdated(unknownExpired0),
|
||||
sameInstance(unknown0));
|
||||
assertThat("Expired unknown should be replaced by valid unknown",
|
||||
unknownExpired0.currentOrUpdated(unknown0),
|
||||
sameInstance(unknown0));
|
||||
|
||||
assertThat("Valid unknown should replace an expired normal record",
|
||||
recordExpired1.currentOrUpdated(unknown0),
|
||||
sameInstance(unknown0));
|
||||
assertThat("Expired normal record should not replace a valid unknown record",
|
||||
unknown0.currentOrUpdated(recordExpired1),
|
||||
sameInstance(unknown0));
|
||||
|
||||
// In normal comparision, expiration doesn't matter
|
||||
assertThat("Expired normal should not be replaced by an earlier expired one",
|
||||
recordExpired1.currentOrUpdated(recordExpired0),
|
||||
sameInstance(recordExpired1));
|
||||
assertThat("Expired normal should be replaced by a later expired one",
|
||||
recordExpired0.currentOrUpdated(recordExpired1),
|
||||
sameInstance(recordExpired1));
|
||||
|
||||
assertThat("Later worst record should be replaced by earlier record",
|
||||
recordWorst.currentOrUpdated(record0),
|
||||
sameInstance(record0));
|
||||
assertThat("Later worst record should not replace earlier",
|
||||
GitHubClient.shouldReplace(recordWorst, record0),
|
||||
is(false));
|
||||
record0.currentOrUpdated(recordWorst),
|
||||
sameInstance(record0));
|
||||
|
||||
assertThat("Equivalent record should not replace", GitHubClient.shouldReplace(record0, record00), is(false));
|
||||
assertThat("Equivalent record should not replace", GitHubClient.shouldReplace(record00, record0), is(false));
|
||||
assertThat("Equivalent record should not replace other",
|
||||
record00.currentOrUpdated(record0),
|
||||
sameInstance(record00));
|
||||
assertThat("Equivalent record should not replace other",
|
||||
record0.currentOrUpdated(record00),
|
||||
sameInstance(record0));
|
||||
|
||||
assertThat("Lower limit record should replace higher", GitHubClient.shouldReplace(record1, record0), is(true));
|
||||
assertThat("Lower limit record should replace higher", GitHubClient.shouldReplace(record2, record1), is(true));
|
||||
assertThat("Higher limit record should be replaced by lower",
|
||||
record0.currentOrUpdated(record1),
|
||||
sameInstance(record1));
|
||||
assertThat("Higher limit record should be replaced by lower",
|
||||
record1.currentOrUpdated(record2),
|
||||
sameInstance(record2));
|
||||
|
||||
assertThat("Higher limit record should not replace lower",
|
||||
GitHubClient.shouldReplace(record1, record2),
|
||||
is(false));
|
||||
assertThat("Lower limit record should not be replaced higher",
|
||||
record2.currentOrUpdated(record1),
|
||||
sameInstance(record2));
|
||||
|
||||
assertThat("Higher limit record with later reset should replace lower",
|
||||
GitHubClient.shouldReplace(record3, record2),
|
||||
is(true));
|
||||
assertThat("Lower limit record should be replaced by higher limit record with later reset",
|
||||
record2.currentOrUpdated(record3),
|
||||
sameInstance(record3));
|
||||
|
||||
assertThat("Lower limit record with later reset should replace higher",
|
||||
GitHubClient.shouldReplace(record4, record1),
|
||||
is(true));
|
||||
assertThat("Higher limit record should be replaced by lower limit record with later reset",
|
||||
record1.currentOrUpdated(record4),
|
||||
sameInstance(record4));
|
||||
|
||||
assertThat("Lower limit record with earlier reset should not replace higher",
|
||||
GitHubClient.shouldReplace(record2, record4),
|
||||
is(false));
|
||||
assertThat("Higher limit record should not be replaced by lower limit record with earlier reset",
|
||||
record4.currentOrUpdated(record2),
|
||||
sameInstance(record4));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user