-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathGHRateLimitTest.java
More file actions
600 lines (467 loc) · 25.6 KB
/
GHRateLimitTest.java
File metadata and controls
600 lines (467 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
package org.kohsuke.github;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.junit.Test;
import tools.jackson.databind.exc.MismatchedInputException;
import tools.jackson.databind.exc.ValueInstantiationException;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
// TODO: Auto-generated Javadoc
/**
* Test showing the behavior of OkHttpGitHubConnector with and without cache.
* <p>
* Key take aways:
*
* <ul>
* <li>These tests are artificial and intended to highlight the differences in behavior between scenarios. However, the
* differences they indicate are stark.</li>
* <li>Caching reduces rate limit consumption by at least a factor of two in even the simplest case.</li>
* <li>The OkHttp cache is pretty smart and will often connect read and write requests made on the same client and
* invalidate caches.</li>
* <li>Changes made outside the current client cause the OkHttp cache to return stale data. This is expected and correct
* behavior.</li>
* <li>"max-age=0" addresses the problem of external changes by revalidating caches for each request. This produces the
* same number of requests as OkHttp without caching, but those requests only count towards the GitHub rate limit if
* data has changes.</li>
* </ul>
*
* @author Liam Newman
*/
public class GHRateLimitTest extends AbstractGitHubWireMockTest {
private static GHRepository getRepository(GitHub gitHub) throws IOException {
return gitHub.getOrganization("hub4j-test-org").getRepository("github-api");
}
/** The previous limit. */
GHRateLimit previousLimit = null;
/** The rate limit. */
GHRateLimit rateLimit = null;
/**
* Instantiates a new GH rate limit test.
*/
public GHRateLimitTest() {
useDefaultGitHub = false;
}
/**
* Test git hub enterprise does not have rate limit.
*
* @throws Exception
* the exception
*/
@Test
public void testGitHubEnterpriseDoesNotHaveRateLimit() throws Exception {
// Customized response that results in file not found the same as GitHub Enterprise
snapshotNotAllowed();
GHRateLimit.UnknownLimitRecord.reset();
assertThat(mockGitHub.getRequestCount(), equalTo(0));
GHRateLimit rateLimit = null;
Instant lastReset = Instant.ofEpochMilli(System.currentTimeMillis() / 1000L);
// Give this a moment
Thread.sleep(1500);
// -------------------------------------------------------------
// Before any queries, rate limit starts as default but may be requested
gitHub = GitHub.connectToEnterpriseWithOAuth(mockGitHub.apiServer().baseUrl(), "bogus", "bogus");
assertThat(mockGitHub.getRequestCount(), equalTo(0));
assertThat(gitHub.lastRateLimit(), sameInstance(GHRateLimit.DEFAULT));
rateLimit = gitHub.rateLimit();
assertThat(gitHub.lastRateLimit(), not(equalTo(GHRateLimit.DEFAULT)));
assertThat(rateLimit.getCore(), instanceOf(GHRateLimit.UnknownLimitRecord.class));
assertThat(rateLimit.getLimit(), equalTo(GHRateLimit.UnknownLimitRecord.unknownLimit));
assertThat(rateLimit.getRemaining(), equalTo(GHRateLimit.UnknownLimitRecord.unknownRemaining));
assertThat(rateLimit.getResetDate().compareTo(Date.from(lastReset)), equalTo(1));
lastReset = rateLimit.getCore().getResetInstant();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
// lastRateLimit the same as rateLimit
assertThat(gitHub.lastRateLimit(), sameInstance(rateLimit));
assertThat(mockGitHub.getRequestCount(), equalTo(1));
// Give this a moment
Thread.sleep(1500);
// -------------------------------------------------------------
// Some versions of GHE include header rate limit information, some do not
// This response mocks the behavior without header rate limit information
gitHub = GitHub.connectToEnterpriseWithOAuth(mockGitHub.apiServer().baseUrl(), "bogus", "bogus");
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(2));
assertThat(gitHub.lastRateLimit(), sameInstance(GHRateLimit.DEFAULT));
rateLimit = gitHub.rateLimit();
assertThat(rateLimit.getCore(), instanceOf(GHRateLimit.UnknownLimitRecord.class));
assertThat(rateLimit.getLimit(), equalTo(GHRateLimit.UnknownLimitRecord.unknownLimit));
assertThat(rateLimit.getRemaining(), equalTo(GHRateLimit.UnknownLimitRecord.unknownRemaining));
// Same unknown instance is reused for a while
assertThat(rateLimit.getResetDate().compareTo(Date.from(lastReset)), equalTo(0));
lastReset = rateLimit.getCore().getResetInstant();
assertThat(mockGitHub.getRequestCount(), equalTo(3));
// Give this a moment
Thread.sleep(1500);
// -------------------------------------------------------------
// GHE returns 404 from /rate_limit
// Some versions of GHE include header rate limit information, some do not
// This response mocks the behavior without header rate limit information
// Always requests new info
rateLimit = gitHub.getRateLimit();
assertThat(mockGitHub.getRequestCount(), equalTo(4));
assertThat(rateLimit.getCore(), instanceOf(GHRateLimit.UnknownLimitRecord.class));
assertThat(rateLimit.getLimit(), equalTo(GHRateLimit.UnknownLimitRecord.unknownLimit));
assertThat(rateLimit.getRemaining(), equalTo(GHRateLimit.UnknownLimitRecord.unknownRemaining));
// When not expired, unknowns do not replace each other so last reset remains unchanged
assertThat(rateLimit.getResetDate().compareTo(Date.from(lastReset)), equalTo(0));
// Give this a moment
Thread.sleep(1500);
// ratelimit() tries not to make additional requests, uses queried rate limit since header not available
Thread.sleep(1500);
assertThat(gitHub.rateLimit(), sameInstance(rateLimit));
assertThat(mockGitHub.getRequestCount(), equalTo(4));
// -------------------------------------------------------------
// Some versions of GHE include header rate limit information, some do not
// This response mocks the behavior with header rate limit information
gitHub = GitHub.connectToEnterpriseWithOAuth(mockGitHub.apiServer().baseUrl(), "bogus", "bogus");
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(5));
// Since just got rate limit from header, these don't request again
rateLimit = gitHub.lastRateLimit();
assertThat(rateLimit, notNullValue());
assertThat(rateLimit.getLimit(), equalTo(5000));
assertThat(rateLimit.getRemaining(), equalTo(4978));
assertThat(rateLimit.getResetDate().compareTo(Date.from(lastReset)), equalTo(1));
lastReset = rateLimit.getCore().getResetInstant();
// When getting only header updates, the unknowns are also expired
assertThat(rateLimit.getSearch().isExpired(), is(true));
GHRateLimit headerRateLimit = rateLimit;
// Give this a moment
Thread.sleep(1500);
// ratelimit() uses headerRateLimit if available and headerRateLimit is not expired
assertThat(gitHub.rateLimit(), sameInstance(headerRateLimit));
assertThat(mockGitHub.getRequestCount(), equalTo(5));
// Give this a moment
Thread.sleep(1500);
// -------------------------------------------------------------
// GHE returns 404 from /rate_limit
// Some versions of GHE include header rate limit information, some do not
// This response mocks the behavior with header rate limit information
// Mocks that other requests come in since previous call
// Always requests new info
rateLimit = gitHub.getRateLimit();
assertThat(mockGitHub.getRequestCount(), equalTo(6));
// getRateLimit() uses headerRateLimit if /rate_limit returns a 404
// and headerRateLimit is available and not expired
assertThat(rateLimit, notNullValue());
assertThat(rateLimit.getLimit(), equalTo(5000));
// 11 requests since previous api call
// This verifies that header rate limit info is recorded even for /rate_limit endpoint and 404 response
assertThat(rateLimit.getRemaining(), equalTo(4967));
assertThat(rateLimit.getResetDate().compareTo(Date.from(lastReset)), equalTo(0));
// getRateLimit() uses headerRateLimit if /rate_limit returns a 404
// and headerRateLimit is available and not expired
assertThat(rateLimit, sameInstance(gitHub.lastRateLimit()));
headerRateLimit = rateLimit;
// ratelimit() should prefer headerRateLimit when getRateLimit fails and headerRateLimit is not expired
assertThat(gitHub.rateLimit(), sameInstance(rateLimit));
assertThat(mockGitHub.getRequestCount(), equalTo(6));
// Verify the requesting a search url updates the search rate limit
// Core rate limit record should not change while search is updated.
assertThat(gitHub.lastRateLimit().getSearch(), instanceOf(GHRateLimit.UnknownLimitRecord.class));
assertThat(gitHub.lastRateLimit().getSearch().isExpired(), equalTo(true));
HashMap<String, Object> searchResult = (HashMap<String, Object>) gitHub.createRequest()
.rateLimit(RateLimitTarget.SEARCH)
.setRawUrlPath(mockGitHub.apiServer().baseUrl()
+ "/search/repositories?q=tetris+language%3Aassembly&sort=stars&order=desc")
.fetch(Object.class);
assertThat(searchResult.get("total_count"), equalTo(1918));
assertThat(mockGitHub.getRequestCount(), equalTo(7));
assertThat(gitHub.lastRateLimit(), not(sameInstance(headerRateLimit)));
assertThat(gitHub.lastRateLimit().getCore(), sameInstance(headerRateLimit.getCore()));
assertThat(gitHub.lastRateLimit().getSearch(), not(sameInstance(headerRateLimit.getSearch())));
assertThat(gitHub.lastRateLimit().getSearch().getRemaining(), equalTo(29));
PagedSearchIterable<GHRepository> searchResult2 = gitHub.searchRepositories()
.q("tetris")
.language("assembly")
.sort(GHRepositorySearchBuilder.Sort.STARS)
.order(GHDirection.DESC)
.list();
assertThat(searchResult2.getTotalCount(), equalTo(1918));
assertThat(mockGitHub.getRequestCount(), equalTo(8));
assertThat(gitHub.lastRateLimit(), not(sameInstance(headerRateLimit)));
assertThat(gitHub.lastRateLimit().getCore(), sameInstance(headerRateLimit.getCore()));
assertThat(gitHub.lastRateLimit().getSearch(), not(sameInstance(headerRateLimit.getSearch())));
assertThat(gitHub.lastRateLimit().getSearch().getRemaining(), equalTo(28));
}
/**
* Test git hub rate limit.
*
* @throws Exception
* the exception
*/
@Test
public void testGitHubRateLimit() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
GHRateLimit.UnknownLimitRecord.reset();
assertThat(mockGitHub.getRequestCount(), equalTo(0));
// 4897 is just the what the limit was when the snapshot was taken
previousLimit = GHRateLimit.fromRecord(
new GHRateLimit.Record(5000,
4897,
(templating.testStartDate.getTime() + Duration.ofHours(1).toMillis()) / 1000L),
RateLimitTarget.CORE);
// -------------------------------------------------------------
// /user gets response with rate limit information
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl()).build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
// Since we already had rate limit info these don't request again
rateLimit = gitHub.lastRateLimit();
verifyRateLimitValues(previousLimit, previousLimit.getRemaining());
previousLimit = rateLimit;
GHRateLimit headerRateLimit = rateLimit;
// Give this a moment
Thread.sleep(1500);
// ratelimit() uses cached rate limit if available and not expired
assertThat(gitHub.rateLimit(), sameInstance(headerRateLimit));
assertThat(mockGitHub.getRequestCount(), equalTo(1));
// Give this a moment
Thread.sleep(1500);
// Always requests new info
rateLimit = gitHub.getRateLimit();
assertThat(mockGitHub.getRequestCount(), equalTo(2));
// Because remaining and reset date are unchanged in core, the header should be unchanged as well
// But the overall instance has changed because of filling in of unknown data.
assertThat(gitHub.lastRateLimit(), not(sameInstance(headerRateLimit)));
// Identical Records should be preserved even when GHRateLimit is merged
assertThat(gitHub.lastRateLimit().getCore(), sameInstance(headerRateLimit.getCore()));
assertThat(gitHub.lastRateLimit().getSearch(), not(sameInstance(headerRateLimit.getSearch())));
headerRateLimit = gitHub.lastRateLimit();
// rate limit request is free, remaining is unchanged
verifyRateLimitValues(previousLimit, previousLimit.getRemaining());
previousLimit = rateLimit;
// Give this a moment
Thread.sleep(1500);
// Always requests new info
rateLimit = gitHub.getRateLimit();
assertThat(mockGitHub.getRequestCount(), equalTo(3));
// Because remaining and reset date are unchanged, the header should be unchanged as well
assertThat(gitHub.lastRateLimit(), sameInstance(headerRateLimit));
// rate limit request is free, remaining is unchanged
verifyRateLimitValues(previousLimit, previousLimit.getRemaining());
previousLimit = rateLimit;
gitHub.getOrganization(GITHUB_API_TEST_ORG);
assertThat(mockGitHub.getRequestCount(), equalTo(4));
// Because remaining has changed the header should be different
assertThat(gitHub.lastRateLimit(), not(sameInstance(headerRateLimit)));
assertThat(gitHub.lastRateLimit(), not(equalTo(headerRateLimit)));
rateLimit = gitHub.lastRateLimit();
// Org costs limit to query
verifyRateLimitValues(previousLimit, previousLimit.getRemaining() - 1);
previousLimit = rateLimit;
headerRateLimit = rateLimit;
// ratelimit() should prefer headerRateLimit when it is most recent and not expired
assertThat(gitHub.rateLimit(), sameInstance(headerRateLimit));
assertThat(mockGitHub.getRequestCount(), equalTo(4));
// AT THIS POINT WE SIMULATE A RATE LIMIT RESET
// Give this a moment
Thread.sleep(2000);
// Always requests new info
rateLimit = gitHub.getRateLimit();
assertThat(mockGitHub.getRequestCount(), equalTo(5));
// rate limit request is free, remaining is unchanged date is later
verifyRateLimitValues(previousLimit, previousLimit.getRemaining(), true);
previousLimit = rateLimit;
// When getRateLimit() succeeds, cached rate limit updates as usual as well (if needed)
assertThat(gitHub.rateLimit(), sameInstance(rateLimit));
// Verify different record instances can be compared
assertThat(gitHub.rateLimit().getCore(), equalTo(rateLimit.getCore()));
// Verify different instances can be compared
// TODO: This is not work currently because the header rate limit has unknowns for records other than core.
// assertThat(gitHub.rateLimit(), equalTo(rateLimit));
assertThat(gitHub.rateLimit(), not(sameInstance(headerRateLimit)));
assertThat(gitHub.rateLimit(), sameInstance(gitHub.lastRateLimit()));
headerRateLimit = gitHub.lastRateLimit();
assertThat(mockGitHub.getRequestCount(), equalTo(5));
// Verify the requesting a search url updates the search rate limit
assertThat(gitHub.lastRateLimit().getSearch().getRemaining(), equalTo(30));
HashMap<String, Object> searchResult = gitHub.createRequest()
.rateLimit(RateLimitTarget.SEARCH)
.setRawUrlPath(mockGitHub.apiServer().baseUrl()
+ "/search/repositories?q=tetris+language%3Aassembly&sort=stars&order=desc")
.fetch(HashMap.class);
assertThat(searchResult.get("total_count"), equalTo(1918));
assertThat(mockGitHub.getRequestCount(), equalTo(6));
assertThat(gitHub.lastRateLimit(), not(sameInstance(headerRateLimit)));
assertThat(gitHub.lastRateLimit().getCore(), sameInstance(headerRateLimit.getCore()));
assertThat(gitHub.lastRateLimit().getSearch(), not(sameInstance(headerRateLimit.getSearch())));
assertThat(gitHub.lastRateLimit().getSearch().getRemaining(), equalTo(29));
PagedSearchIterable<GHRepository> searchResult2 = gitHub.searchRepositories()
.q("tetris")
.language("assembly")
.sort(GHRepositorySearchBuilder.Sort.STARS)
.order(GHDirection.DESC)
.list();
assertThat(searchResult2.getTotalCount(), equalTo(1918));
assertThat(mockGitHub.getRequestCount(), equalTo(7));
assertThat(gitHub.lastRateLimit(), not(sameInstance(headerRateLimit)));
assertThat(gitHub.lastRateLimit().getCore(), sameInstance(headerRateLimit.getCore()));
assertThat(gitHub.lastRateLimit().getSearch(), not(sameInstance(headerRateLimit.getSearch())));
assertThat(gitHub.lastRateLimit().getSearch().getRemaining(), equalTo(28));
}
/**
* Test git hub rate limit expiration server five minutes ahead.
*
* @throws Exception
* the exception
*/
// These tests should behave the same, showing server time adjustment working
@Test
public void testGitHubRateLimitExpirationServerFiveMinutesAhead() throws Exception {
executeExpirationTest();
}
/**
* Test git hub rate limit expiration server five minutes behind.
*
* @throws Exception
* the exception
*/
@Test
public void testGitHubRateLimitExpirationServerFiveMinutesBehind() throws Exception {
executeExpirationTest();
}
/**
* Test git hub rate limit with bad data.
*
* @throws Exception
* the exception
*/
@Test
public void testGitHubRateLimitWithBadData() throws Exception {
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl()).build();
gitHub.getMyself();
try {
gitHub.getRateLimit();
fail("Invalid rate limit missing some records should throw");
} catch (Exception e) {
assertThat(e, instanceOf(HttpException.class));
// In Jackson 3, the exception is wrapped in IOException
assertThat(e.getCause(), instanceOf(IOException.class));
assertThat(e.getCause().getCause(), instanceOf(ValueInstantiationException.class));
assertThat(e.getCause().getCause().getMessage(),
containsString(
"Cannot construct instance of `org.kohsuke.github.GHRateLimit`, problem: `java.lang.NullPointerException`"));
}
try {
gitHub.getRateLimit();
fail("Invalid rate limit record missing a value should throw");
} catch (Exception e) {
assertThat(e, instanceOf(HttpException.class));
// In Jackson 3, the exception is wrapped in IOException
assertThat(e.getCause(), instanceOf(IOException.class));
assertThat(e.getCause().getCause(), instanceOf(MismatchedInputException.class));
assertThat(e.getCause().getCause().getMessage(),
containsString("Missing required creator property 'reset' (index 2)"));
}
}
private void executeExpirationTest() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
GHRateLimit.UnknownLimitRecord.reset();
assertThat(mockGitHub.getRequestCount(), equalTo(0));
GHRateLimit rateLimit = null;
GHRateLimit headerRateLimit = null;
// Give this a moment
Thread.sleep(1500);
// -------------------------------------------------------------
// /user gets response with rate limit information
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl()).build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
// Since we already had rate limit info these don't request again
headerRateLimit = gitHub.lastRateLimit();
rateLimit = gitHub.rateLimit();
assertThat(rateLimit, notNullValue());
assertThat("rateLimit() selects header instance when not expired, does not ask server",
rateLimit,
sameInstance(headerRateLimit));
// Nothing changes still valid
Thread.sleep(1500);
assertThat("rateLimit() selects header instance when not expired, does not ask server",
gitHub.rateLimit(),
sameInstance(headerRateLimit));
assertThat(mockGitHub.getRequestCount(), equalTo(1));
// This time, rateLimit() should find an expired record and get a new one.
Thread.sleep(2500);
assertThat("Header instance has expired", gitHub.lastRateLimit().isExpired());
assertThat("rateLimit() will ask server when cached instance has expired",
gitHub.rateLimit(),
not(sameInstance(rateLimit)));
assertThat("lastRateLimit() is populated as part of internal call to getRateLimit()",
gitHub.lastRateLimit(),
not(sameInstance(rateLimit)));
assertThat("After request, rateLimit() selects cached since it has been refreshed",
gitHub.rateLimit(),
sameInstance(gitHub.lastRateLimit()));
headerRateLimit = gitHub.lastRateLimit();
assertThat(mockGitHub.getRequestCount(), equalTo(2));
// During the previous call we returned expired header info but valid returned record
// Merging means this has already been merged into a valid cached instance
Thread.sleep(4000);
rateLimit = gitHub.rateLimit();
// Using custom data to have a header instance that expires before the queried instance
assertThat("if valid ratelimit() uses it without asking server",
gitHub.rateLimit(),
sameInstance(gitHub.lastRateLimit()));
assertThat("ratelimit() should almost never return a return a GHRateLimit that is already expired",
gitHub.rateLimit().isExpired(),
is(false));
assertThat("Cached instance hasn't been reloaded", gitHub.lastRateLimit(), sameInstance(headerRateLimit));
assertThat("Cached instance has not expired", gitHub.lastRateLimit().isExpired(), is(false));
assertThat(mockGitHub.getRequestCount(), equalTo(2));
// Finally the cached instance expires and rateLimit() should get a new record
Thread.sleep(2000);
headerRateLimit = gitHub.rateLimit();
assertThat("rateLimit() has asked server for new information",
gitHub.rateLimit(),
not(sameInstance(rateLimit)));
assertThat("rateLimit() has asked server for new information",
gitHub.lastRateLimit(),
not(sameInstance(rateLimit)));
assertThat("rateLimit() selects header instance when not expired, does not ask server",
gitHub.rateLimit(),
sameInstance((gitHub.lastRateLimit())));
assertThat(mockGitHub.getRequestCount(), equalTo(3));
}
private void verifyRateLimitValues(GHRateLimit previousLimit, int remaining) {
verifyRateLimitValues(previousLimit, remaining, false);
}
private void verifyRateLimitValues(GHRateLimit previousLimit, int remaining, boolean changedResetDate) {
// Basic checks of values
assertThat(rateLimit, notNullValue());
assertThat(rateLimit.getLimit(), equalTo(previousLimit.getLimit()));
assertThat(rateLimit.getRemaining(), equalTo(remaining));
// Check that the reset date of the current limit is not older than the previous one
long diffMillis = rateLimit.getCore().getResetInstant().toEpochMilli()
- previousLimit.getCore().getResetInstant().toEpochMilli();
assertThat(diffMillis, greaterThanOrEqualTo(0L));
if (changedResetDate) {
assertThat(diffMillis, greaterThan(1000L));
} else {
assertThat(diffMillis, lessThanOrEqualTo(1000L));
}
// Additional checks for record values
assertThat(rateLimit.getCore().getLimit(), equalTo(rateLimit.getLimit()));
assertThat(rateLimit.getCore().getRemaining(), equalTo(rateLimit.getRemaining()));
assertThat(rateLimit.getCore().getResetEpochSeconds(), equalTo(rateLimit.getResetEpochSeconds()));
assertThat(rateLimit.getCore().getResetDate(), equalTo(rateLimit.getResetDate()));
}
/**
* Gets the wire mock options.
*
* @return the wire mock options
*/
@Override
protected WireMockConfiguration getWireMockOptions() {
return super.getWireMockOptions().extensions(templating.newResponseTransformer());
}
}