33import com .fasterxml .jackson .databind .exc .MismatchedInputException ;
44import com .fasterxml .jackson .databind .exc .ValueInstantiationException ;
55import com .github .tomakehurst .wiremock .core .WireMockConfiguration ;
6- import com .github .tomakehurst .wiremock .extension .responsetemplating .ResponseTemplateTransformer ;
76import org .hamcrest .CoreMatchers ;
87import org .junit .Test ;
98
109import java .io .IOException ;
10+ import java .time .Duration ;
1111import java .util .Date ;
1212
1313import static org .hamcrest .CoreMatchers .*;
14- import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
1514import static org .hamcrest .core .IsInstanceOf .instanceOf ;
1615
1716/**
@@ -43,8 +42,7 @@ public GHRateLimitTest() {
4342
4443 @ Override
4544 protected WireMockConfiguration getWireMockOptions () {
46- return super .getWireMockOptions ()
47- .extensions (ResponseTemplateTransformer .builder ().global (true ).maxCacheEntries (0L ).build ());
45+ return super .getWireMockOptions ().extensions (templating .newResponseTransformer ());
4846 }
4947
5048 @ Test
@@ -55,11 +53,9 @@ public void testGitHubRateLimit() throws Exception {
5553 assertThat (mockGitHub .getRequestCount (), equalTo (0 ));
5654
5755 // 4897 is just the what the limit was when the snapshot was taken
58- previousLimit = GHRateLimit
59- .fromHeaderRecord (new GHRateLimit .Record (5000 , 4897 , System .currentTimeMillis () / 1000L ));
60-
61- // Give this a moment
62- Thread .sleep (1000 );
56+ previousLimit = GHRateLimit .fromHeaderRecord (new GHRateLimit .Record (5000 ,
57+ 4897 ,
58+ (templating .testStartDate .getTime () + Duration .ofHours (1 ).toMillis ()) / 1000L ));
6359
6460 // -------------------------------------------------------------
6561 // /user gets response with rate limit information
@@ -90,6 +86,9 @@ public void testGitHubRateLimit() throws Exception {
9086 rateLimit = gitHub .getRateLimit ();
9187 assertThat (mockGitHub .getRequestCount (), equalTo (2 ));
9288
89+ // Because remaining and reset date are unchanged, the header should be unchanged as well
90+ assertThat (gitHub .lastRateLimit (), sameInstance (headerRateLimit ));
91+
9392 // rate limit request is free, remaining is unchanged
9493 verifyRateLimitValues (previousLimit , previousLimit .getRemaining ());
9594 previousLimit = rateLimit ;
@@ -101,13 +100,18 @@ public void testGitHubRateLimit() throws Exception {
101100 rateLimit = gitHub .getRateLimit ();
102101 assertThat (mockGitHub .getRequestCount (), equalTo (3 ));
103102
103+ // Because remaining and reset date are unchanged, the header should be unchanged as well
104+ assertThat (gitHub .lastRateLimit (), sameInstance (headerRateLimit ));
105+
104106 // rate limit request is free, remaining is unchanged
105107 verifyRateLimitValues (previousLimit , previousLimit .getRemaining ());
106108 previousLimit = rateLimit ;
107109
108110 gitHub .getOrganization (GITHUB_API_TEST_ORG );
109111 assertThat (mockGitHub .getRequestCount (), equalTo (4 ));
110112
113+ // Because remaining has changed the header should be different
114+ assertThat (gitHub .lastRateLimit (), not (sameInstance (headerRateLimit )));
111115 assertThat (gitHub .lastRateLimit (), not (equalTo (headerRateLimit )));
112116 rateLimit = gitHub .lastRateLimit ();
113117
@@ -118,19 +122,21 @@ public void testGitHubRateLimit() throws Exception {
118122 headerRateLimit = rateLimit ;
119123
120124 // ratelimit() should prefer headerRateLimit when it is most recent and not expired
121- assertThat (gitHub .rateLimit (), equalTo (headerRateLimit ));
125+ assertThat (gitHub .rateLimit (), sameInstance (headerRateLimit ));
122126
123127 assertThat (mockGitHub .getRequestCount (), equalTo (4 ));
124128
129+ // AT THIS POINT WE SIMULATE A RATE LIMIT RESET
130+
125131 // Give this a moment
126132 Thread .sleep (2000 );
127133
128134 // Always requests new info
129135 rateLimit = gitHub .getRateLimit ();
130136 assertThat (mockGitHub .getRequestCount (), equalTo (5 ));
131137
132- // rate limit request is free, remaining is unchanged
133- verifyRateLimitValues (previousLimit , previousLimit .getRemaining ());
138+ // rate limit request is free, remaining is unchanged date is later
139+ verifyRateLimitValues (previousLimit , previousLimit .getRemaining (), true );
134140 previousLimit = rateLimit ;
135141
136142 // When getRateLimit() succeeds, headerRateLimit updates as usual as well (if needed)
@@ -142,7 +148,7 @@ public void testGitHubRateLimit() throws Exception {
142148
143149 // Verify different instances can be compared
144150 // TODO: This is not work currently because the header rate limit has unknowns for records other than core.
145- // assertThat(gitHub.rateLimit().getCore() , equalTo(rateLimit.getCore() ));
151+ // assertThat(gitHub.rateLimit(), equalTo(rateLimit));
146152
147153 assertThat (gitHub .rateLimit (), not (sameInstance (headerRateLimit )));
148154 assertThat (gitHub .rateLimit (), sameInstance (gitHub .lastRateLimit ()));
@@ -151,13 +157,20 @@ public void testGitHubRateLimit() throws Exception {
151157 }
152158
153159 private void verifyRateLimitValues (GHRateLimit previousLimit , int remaining ) {
160+ verifyRateLimitValues (previousLimit , remaining , false );
161+ }
162+
163+ private void verifyRateLimitValues (GHRateLimit previousLimit , int remaining , boolean changedResetDate ) {
164+ // newer or unchange
165+ int resetComparisionValue = changedResetDate ? 1 : 0 ;
166+
154167 // Basic checks of values
155168 assertThat (rateLimit , notNullValue ());
156169 assertThat (rateLimit .getLimit (), equalTo (previousLimit .getLimit ()));
157170 assertThat (rateLimit .getRemaining (), equalTo (remaining ));
158171
159172 // Check that the reset date of the current limit is not older than the previous one
160- assertThat (rateLimit .getResetDate ().compareTo (previousLimit .getResetDate ()), greaterThanOrEqualTo ( 0 ));
173+ assertThat (rateLimit .getResetDate ().compareTo (previousLimit .getResetDate ()), equalTo ( resetComparisionValue ));
161174
162175 // Additional checks for record values
163176 assertThat (rateLimit .getCore ().getLimit (), equalTo (rateLimit .getLimit ()));
@@ -257,7 +270,8 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws Exception {
257270 assertThat (rateLimit , notNullValue ());
258271 assertThat (rateLimit .getLimit (), equalTo (5000 ));
259272 assertThat (rateLimit .getRemaining (), equalTo (4978 ));
260- assertThat (rateLimit .getResetDate ().compareTo (lastReset ), greaterThanOrEqualTo (0 ));
273+ // The previous record was an "Unknown", so even though this records resets sooner we take it
274+ assertThat (rateLimit .getResetDate ().compareTo (lastReset ), equalTo (-1 ));
261275 lastReset = rateLimit .getResetDate ();
262276
263277 GHRateLimit headerRateLimit = rateLimit ;
@@ -266,7 +280,7 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws Exception {
266280 Thread .sleep (1000 );
267281
268282 // ratelimit() uses headerRateLimit if available and headerRateLimit is not expired
269- assertThat (gitHub .rateLimit (), equalTo (headerRateLimit ));
283+ assertThat (gitHub .rateLimit (), sameInstance (headerRateLimit ));
270284
271285 assertThat (mockGitHub .getRequestCount (), equalTo (5 ));
272286
@@ -365,7 +379,7 @@ private void executeExpirationTest() throws Exception {
365379 assertThat ("rateLimit() selects header instance when not expired, does not ask server" ,
366380 gitHub .rateLimit (),
367381 sameInstance (headerRateLimit ));
368- assertThat ("rateLimit () selects header instance when not expired , does not ask server" ,
382+ assertThat ("lastRateLimit () always selects header instance, does not ask server" ,
369383 gitHub .lastRateLimit (),
370384 sameInstance (headerRateLimit ));
371385
0 commit comments