1414import java .io .IOException ;
1515import java .util .Objects ;
1616
17- import static org .hamcrest .Matchers .nullValue ;
17+ import static org .hamcrest .Matchers .* ;
1818import static org .hamcrest .core .Is .is ;
1919import static org .junit .Assume .assumeFalse ;
2020import static org .junit .Assume .assumeTrue ;
4141 */
4242public 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.
0 commit comments