Skip to content

Commit f3a0b35

Browse files
committed
Updated Tests to have non-cache tests run in CI
1 parent 64e3be3 commit f3a0b35

71 files changed

Lines changed: 11059 additions & 62 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public OkHttpConnector(OkUrlFactory urlFactory) {
5858
.maxAge(cacheMaxAge, TimeUnit.SECONDS)
5959
.build()
6060
.toString();
61-
6261
} else {
6362
maxAgeHeaderValue = null;
6463
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public abstract class AbstractGitHubApiWireMockTest extends Assert {
3535
final static String STUBBED_USER_LOGIN = "placeholder-user";
3636
final static String STUBBED_USER_PASSWORD = "placeholder-password";
3737

38+
protected boolean useDefaultGitHub = true;
39+
3840
/**
3941
* {@link GitHub} instance for use during test.
4042
* Traffic will be part of snapshot when taken.
@@ -103,8 +105,10 @@ public void wireMockSetup() throws Exception {
103105
GitHubBuilder builder = getGitHubBuilder()
104106
.withEndpoint(githubApi.baseUrl());
105107

106-
gitHub = builder
107-
.build();
108+
if (useDefaultGitHub) {
109+
gitHub = builder
110+
.build();
111+
}
108112

109113
if (githubApi.isUseProxy()) {
110114
gitHubBeforeAfter = getGitHubBuilder()

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

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.io.IOException;
1515
import java.util.Objects;
1616

17-
import static org.hamcrest.Matchers.nullValue;
17+
import static org.hamcrest.Matchers.*;
1818
import static org.hamcrest.core.Is.is;
1919
import static org.junit.Assume.assumeFalse;
2020
import static org.junit.Assume.assumeTrue;
@@ -41,60 +41,56 @@
4141
*/
4242
public class OkHttpConnectorTest extends AbstractGitHubApiWireMockTest {
4343

44-
private static int defaultRateLimitUsed = 21;
44+
public OkHttpConnectorTest() {
45+
useDefaultGitHub = false;
46+
}
47+
48+
private static int defaultRateLimitUsed = 17;
4549
private static int okhttpRateLimitUsed = 17;
4650
private static int maxAgeZeroRateLimitUsed = 7;
4751
private static int maxAgeThreeRateLimitUsed = 7;
4852
private static int maxAgeNoneRateLimitUsed = 4;
4953

54+
private static int userRequestCount = 0;
55+
5056
private static int defaultNetworkRequestCount = 16;
51-
private static int okhttpNetworkRequestCount = 17;
52-
private static int maxAgeZeroNetworkRequestCount = 17;
57+
private static int okhttpNetworkRequestCount = 16;
58+
private static int maxAgeZeroNetworkRequestCount = 16;
5359
private static int maxAgeThreeNetworkRequestCount = 9;
54-
private static int maxAgeNoneNetworkRequestCount = 6;
60+
private static int maxAgeNoneNetworkRequestCount = 5;
5561

5662
private static int maxAgeZeroHitCount = 10;
5763
private static int maxAgeThreeHitCount = 10;
5864
private static int maxAgeNoneHitCount = 11;
5965

66+
private GHRateLimit rateLimitBefore;
67+
6068
@Before
6169
public void setupRepo() throws Exception {
62-
assumeFalse("Test only valid when not taking a snapshot", githubApi.isTakeSnapshot());
63-
assumeTrue("Test only valid when proxying (-Dtest.github.useProxy to enable)", githubApi.isUseProxy());
64-
65-
// TODO: (bitiwseman) These tests work locally when proxying but run in to some kind of issue
66-
// when running via snapshot. I think part of it is cache aging but there is also some
67-
// other issue which I do not have the bandwidth to track down right now.
68-
// For the moment, I'm committing this code as documentation of testing.
69-
7070
if (githubApi.isUseProxy()) {
7171
GHRepository repo = getRepository(gitHubBeforeAfter);
7272
repo.setDescription("Resetting");
7373

7474
// Let things settle a bit between tests when working against the live site
7575
Thread.sleep(5000);
76+
userRequestCount = 1;
7677
}
7778
}
7879

7980
@Test
8081
public void DefaultConnector() throws Exception {
8182

82-
GHRateLimit rateLimitBefore = gitHub.rateLimit();
83+
this.gitHub = getGitHubBuilder()
84+
.withEndpoint(githubApi.baseUrl())
85+
.build();
86+
8387
doTestActions();
8488

8589
// Testing behavior after change
8690
// Uncached connection gets updated correctly but at cost of rate limit
8791
assertThat(getRepository(gitHub).getDescription(), is("Tricky"));
8892

89-
GHRateLimit rateLimitAfter = gitHub.rateLimit();
90-
91-
assertThat("Request Count",
92-
getRequestCount(),
93-
is(defaultNetworkRequestCount));
94-
95-
assertThat("Rate Limit Change",
96-
rateLimitBefore.remaining - rateLimitAfter.remaining,
97-
is(defaultRateLimitUsed));
93+
checkRequestAndLimit(defaultNetworkRequestCount, defaultRateLimitUsed);
9894
}
9995

10096
@Test
@@ -108,29 +104,30 @@ public void OkHttpConnector_NoCache() throws Exception {
108104
.withConnector(connector)
109105
.build();
110106

111-
GHRateLimit rateLimitBefore = gitHub.rateLimit();
112107
doTestActions();
113108

114109
// Testing behavior after change
115110
// Uncached okhttp connection gets updated correctly but at cost of rate limit
116111
assertThat(getRepository(gitHub).getDescription(), is("Tricky"));
117112

118-
GHRateLimit rateLimitAfter = gitHub.rateLimit();
119-
120-
assertThat("Request Count",
121-
getRequestCount(),
122-
is(okhttpNetworkRequestCount));
123-
124-
assertThat("Rate Limit Change",
125-
rateLimitBefore.remaining - rateLimitAfter.remaining,
126-
is(okhttpRateLimitUsed));
113+
checkRequestAndLimit(okhttpNetworkRequestCount, okhttpRateLimitUsed);
127114

128115
Cache cache = client.getCache();
129116
assertThat("Cache", cache, is(nullValue()));
130117
}
131118

132119
@Test
133120
public void OkHttpConnector_Cache_MaxAgeNone() throws Exception {
121+
// TODO: (bitiwseman) These tests work locally when proxying but run in to some kind of issue
122+
// when running via snapshot. I think part of it is cache aging but there is also some
123+
// other issue which I do not have the bandwidth to track down right now.
124+
// For the moment, I'm committing this code as documentation of testing.
125+
126+
// NOTE: Tried removing "Date" from mappings.
127+
// That made MaxAgeNon pass but disabled max-age detection, so it is not a valid data.
128+
// Likely need to dynamically construct mapping for these three tests
129+
assumeFalse("Test only valid when not taking a snapshot", githubApi.isTakeSnapshot());
130+
assumeTrue("Test only valid when proxying (-Dtest.github.useProxy to enable)", githubApi.isUseProxy());
134131

135132
OkHttpClient client = createClient(true);
136133
OkHttpConnector connector = new OkHttpConnector(new OkUrlFactory(client), -1);
@@ -140,23 +137,14 @@ public void OkHttpConnector_Cache_MaxAgeNone() throws Exception {
140137
.withConnector(connector)
141138
.build();
142139

143-
GHRateLimit rateLimitBefore = gitHub.rateLimit();
144140
doTestActions();
145141

146142
// Testing behavior after change
147143
// NOTE: this is wrong! The live data changed!
148144
// Due to max-age (default 60 from response) the cache returns the old data.
149145
assertThat(getRepository(gitHub).getDescription(), is(githubApi.getMethodName()));
150146

151-
GHRateLimit rateLimitAfter = gitHub.rateLimit();
152-
153-
assertThat("Request Count",
154-
getRequestCount(),
155-
is(maxAgeNoneNetworkRequestCount));
156-
157-
assertThat("Rate Limit Change",
158-
rateLimitBefore.remaining - rateLimitAfter.remaining,
159-
is(maxAgeNoneRateLimitUsed));
147+
checkRequestAndLimit(maxAgeNoneNetworkRequestCount, maxAgeNoneRateLimitUsed);
160148

161149
Cache cache = client.getCache();
162150

@@ -167,6 +155,17 @@ public void OkHttpConnector_Cache_MaxAgeNone() throws Exception {
167155

168156
@Test
169157
public void OkHttpConnector_Cache_MaxAge_Three() throws Exception {
158+
// TODO: (bitiwseman) These tests work locally when proxying but run in to some kind of issue
159+
// when running via snapshot. I think part of it is cache aging but there is also some
160+
// other issue which I do not have the bandwidth to track down right now.
161+
// For the moment, I'm committing this code as documentation of testing.
162+
163+
// NOTE: Tried removing "Date" from mappings.
164+
// That made MaxAgeNon pass but disabled max-age detection, so it is not a valid data.
165+
// Likely need to dynamically construct mapping for these three tests
166+
assumeFalse("Test only valid when not taking a snapshot", githubApi.isTakeSnapshot());
167+
assumeTrue("Test only valid when proxying (-Dtest.github.useProxy to enable)", githubApi.isUseProxy());
168+
170169

171170
OkHttpClient client = createClient(true);
172171
OkHttpConnector connector = new OkHttpConnector(new OkUrlFactory(client), 3);
@@ -176,28 +175,30 @@ public void OkHttpConnector_Cache_MaxAge_Three() throws Exception {
176175
.withConnector(connector)
177176
.build();
178177

179-
GHRateLimit rateLimitBefore = gitHub.rateLimit();
180178
doTestActions();
181179

182180
// Due to max-age=5 this eventually checks the site and gets updated information. Yay?
183181
assertThat(getRepository(gitHub).getDescription(), is("Tricky"));
184182

185-
GHRateLimit rateLimitAfter = gitHub.rateLimit();
186-
187-
assertThat("Request Count",
188-
getRequestCount(),
189-
is(maxAgeThreeNetworkRequestCount));
190-
191-
assertThat("Rate Limit Change",
192-
rateLimitBefore.remaining - rateLimitAfter.remaining,
193-
is(maxAgeThreeRateLimitUsed));
183+
checkRequestAndLimit(maxAgeThreeNetworkRequestCount, maxAgeThreeRateLimitUsed);
194184

195185
Cache cache = client.getCache();
196186
assertThat("getHitCount", cache.getHitCount(), is(maxAgeThreeHitCount));
197187
}
198188

199189
@Test
200190
public void OkHttpConnector_Cache_MaxAgeDefault_Zero() throws Exception {
191+
// TODO: (bitiwseman) These tests work locally when proxying but run in to some kind of issue
192+
// when running via snapshot. I think part of it is cache aging but there is also some
193+
// other issue which I do not have the bandwidth to track down right now.
194+
// For the moment, I'm committing this code as documentation of testing.
195+
196+
// NOTE: Tried removing "Date" from mappings.
197+
// That made MaxAgeNon pass but disabled max-age detection, so it is not a valid data.
198+
// Likely need to dynamically construct mapping for these three tests
199+
assumeFalse("Test only valid when not taking a snapshot", githubApi.isTakeSnapshot());
200+
assumeTrue("Test only valid when proxying (-Dtest.github.useProxy to enable)", githubApi.isUseProxy());
201+
201202
OkHttpClient client = createClient(true);
202203
OkHttpConnector connector = new OkHttpConnector(new OkUrlFactory(client));
203204

@@ -206,25 +207,29 @@ public void OkHttpConnector_Cache_MaxAgeDefault_Zero() throws Exception {
206207
.withConnector(connector)
207208
.build();
208209

209-
GHRateLimit rateLimitBefore = gitHub.rateLimit();
210210
doTestActions();
211211

212212
// Testing behavior after change
213213
// NOTE: max-age=0 produces the same result at uncached without added rate-limit use.
214214
assertThat(getRepository(gitHub).getDescription(), is("Tricky"));
215215

216-
GHRateLimit rateLimitAfter = gitHub.rateLimit();
216+
checkRequestAndLimit(maxAgeZeroNetworkRequestCount, maxAgeZeroRateLimitUsed);
217+
218+
Cache cache = client.getCache();
219+
assertThat("getHitCount", cache.getHitCount(), is(maxAgeZeroHitCount));
220+
}
217221

222+
private void checkRequestAndLimit(int networkRequestCount, int rateLimitUsed) throws IOException {
223+
GHRateLimit rateLimitAfter = gitHub.rateLimit();
218224
assertThat("Request Count",
219225
getRequestCount(),
220-
is(maxAgeZeroNetworkRequestCount));
226+
is(networkRequestCount + userRequestCount));
221227

228+
// Rate limit must be under this value, but if it wiggles we don't care
222229
assertThat("Rate Limit Change",
223230
rateLimitBefore.remaining - rateLimitAfter.remaining,
224-
is(maxAgeZeroRateLimitUsed));
231+
is(lessThanOrEqualTo(rateLimitUsed + userRequestCount)));
225232

226-
Cache cache = client.getCache();
227-
assertThat("getHitCount", cache.getHitCount(), is(maxAgeZeroHitCount));
228233
}
229234

230235
private int getRequestCount() {
@@ -252,8 +257,11 @@ private OkHttpClient createClient(boolean useCache) throws IOException {
252257
* @throws Exception
253258
*/
254259
private void doTestActions() throws Exception {
260+
rateLimitBefore = gitHub.getRateLimit();
261+
255262
String name = githubApi.getMethodName();
256263

264+
257265
GHRepository repo = getRepository(gitHub);
258266

259267
// Testing behavior when nothing has changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"login": "github-api-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/github-api-test-org",
6+
"repos_url": "https://api.github.com/orgs/github-api-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/github-api-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/github-api-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/github-api-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/github-api-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/github-api-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
13+
"description": null,
14+
"is_verified": false,
15+
"has_organization_projects": true,
16+
"has_repository_projects": true,
17+
"public_repos": 9,
18+
"public_gists": 0,
19+
"followers": 0,
20+
"following": 0,
21+
"html_url": "https://github.com/github-api-test-org",
22+
"created_at": "2014-05-10T19:39:11Z",
23+
"updated_at": "2015-04-20T00:42:30Z",
24+
"type": "Organization",
25+
"total_private_repos": 0,
26+
"owned_private_repos": 0,
27+
"private_gists": 0,
28+
"disk_usage": 132,
29+
"collaborators": 0,
30+
"billing_email": "kk@kohsuke.org",
31+
"default_repository_permission": "none",
32+
"members_can_create_repositories": false,
33+
"two_factor_requirement_enabled": false,
34+
"plan": {
35+
"name": "free",
36+
"space": 976562499,
37+
"private_repos": 0,
38+
"filled_seats": 3,
39+
"seats": 0
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"resources": {
3+
"core": {
4+
"limit": 5000,
5+
"remaining": 4717,
6+
"reset": 1569866107
7+
},
8+
"search": {
9+
"limit": 30,
10+
"remaining": 30,
11+
"reset": 1569862574
12+
},
13+
"graphql": {
14+
"limit": 5000,
15+
"remaining": 5000,
16+
"reset": 1569866114
17+
},
18+
"integration_manifest": {
19+
"limit": 5000,
20+
"remaining": 5000,
21+
"reset": 1569866114
22+
}
23+
},
24+
"rate": {
25+
"limit": 5000,
26+
"remaining": 4717,
27+
"reset": 1569866107
28+
}
29+
}

0 commit comments

Comments
 (0)