Skip to content

Commit 5e87d58

Browse files
committed
Add tests showing cache performance
1 parent 9e1f16b commit 5e87d58

5 files changed

Lines changed: 335 additions & 48 deletions

File tree

src/main/java/org/kohsuke/github/GitHubBuilder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @since 1.59
2121
*/
22-
public class GitHubBuilder {
22+
public class GitHubBuilder implements Cloneable {
2323

2424
// default scoped so unit tests can read them.
2525
/* private */ String endpoint = GitHub.GITHUB_URL;
@@ -206,4 +206,13 @@ public HttpURLConnection connect(URL url) throws IOException {
206206
public GitHub build() throws IOException {
207207
return new GitHub(endpoint, user, oauthToken, password, connector, rateLimitHandler, abuseLimitHandler);
208208
}
209+
210+
@Override
211+
public GitHubBuilder clone() {
212+
try {
213+
return (GitHubBuilder) super.clone();
214+
} catch (CloneNotSupportedException e) {
215+
throw new RuntimeException("Clone should be supported", e);
216+
}
217+
}
209218
}

src/main/java/org/kohsuke/github/extras/OkHttpConnector.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,41 @@
3434
public class OkHttpConnector implements HttpConnector {
3535
private final OkUrlFactory urlFactory;
3636

37+
private final String maxAgeHeaderValue;
38+
3739
public OkHttpConnector(OkUrlFactory urlFactory) {
40+
this(urlFactory, 0);
41+
}
42+
43+
/**
44+
* package private for tests to be able to change max-age for cache.
45+
* @param urlFactory
46+
* @param cacheMaxAge
47+
*/
48+
OkHttpConnector(OkUrlFactory urlFactory, int cacheMaxAge) {
3849
urlFactory.client().setSslSocketFactory(TlsSocketFactory());
3950
urlFactory.client().setConnectionSpecs(TlsConnectionSpecs());
4051
this.urlFactory = urlFactory;
52+
53+
if (cacheMaxAge >= 0 && urlFactory.client() != null) {
54+
maxAgeHeaderValue = "max-age=" + cacheMaxAge;
55+
} else {
56+
maxAgeHeaderValue = null;
57+
}
4158
}
4259

60+
4361
public HttpURLConnection connect(URL url) throws IOException {
4462
HttpURLConnection urlConnection = urlFactory.open(url);
45-
if (urlFactory.client() != null && urlFactory.client().getCache() != null) {
63+
// Cache can be added after client is created so we have to check it for each call
64+
if (maxAgeHeaderValue != null && urlFactory.client().getCache() != null) {
4665
// By default OkHttp honors max-age, meaning it will use local cache
4766
// without checking the network within that time frame.
4867
// However, that can result in stale data being returned during that time so
4968
// we force network-based checking no matter how often the query is made.
5069
// OkHttp still automatically does ETag checking and returns cached data when
5170
// GitHub reports 304, but those do not count against rate limit.
52-
urlConnection.setRequestProperty("Cache-Control", "max-age=0");
71+
urlConnection.setRequestProperty("Cache-Control", maxAgeHeaderValue);
5372
}
5473

5574
return urlConnection;

src/test/java/org/kohsuke/github/AbstractGitHubApiWireMockTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,7 @@ private static GitHubBuilder createGitHubBuilder() {
8383
}
8484

8585
protected GitHubBuilder getGitHubBuilder() {
86-
return githubBuilder;
87-
}
88-
89-
@Before
90-
public void wireMockSetup() throws Exception {
91-
GitHubBuilder builder = getGitHubBuilder();
86+
GitHubBuilder builder = githubBuilder.clone();
9287

9388
if (!githubApi.isUseProxy()) {
9489
// This sets the user and password to a placeholder for wiremock testing
@@ -97,12 +92,19 @@ public void wireMockSetup() throws Exception {
9792
builder.withPassword(STUBBED_USER_LOGIN, STUBBED_USER_PASSWORD);
9893
}
9994

95+
return builder;
96+
}
97+
98+
@Before
99+
public void wireMockSetup() throws Exception {
100+
GitHubBuilder builder = getGitHubBuilder()
101+
.withEndpoint(githubApi.baseUrl());
102+
100103
gitHub = builder
101-
.withEndpoint("http://localhost:" + githubApi.port())
102104
.build();
103105

104106
if (githubApi.isUseProxy()) {
105-
gitHubBeforeAfter = builder
107+
gitHubBeforeAfter = getGitHubBuilder()
106108
.withEndpoint("https://api.github.com/")
107109
.build();
108110
} else {

src/test/java/org/kohsuke/github/OkHttpConnectorTest.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)