Skip to content

Commit 6f23ea0

Browse files
committed
Merge branch 'newExp'
2 parents fb8482c + b28695a commit 6f23ea0

File tree

4 files changed

+105
-41
lines changed

4 files changed

+105
-41
lines changed

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![SmartCache for Retrofit2](logo.png)
22

3-
## SmartCache - preloaded responses for Retrofit 2 [![Build Status](https://travis-ci.org/dimitrovskif/SmartCache.svg?branch=master)](https://travis-ci.org/dimitrovskif/SmartCache)
3+
## SmartCache - preloaded responses for Retrofit 2 [![Build Status](https://www.travis-ci.com/fikisipi/SmartCache.svg?branch=master)](https://travis-ci.org/dimitrovskif/SmartCache)
44

55
#### This library calls your callback twice: first cached, then fresh data
66

@@ -12,17 +12,18 @@ Instead of showing an empty screen while waiting for a network response, why not
1212

1313
### Install and use
1414

15-
1. Gradle installation:
16-
```groovy
15+
1. Add SmartCache to Gradle dependencies, and JitPack to repos:
16+
<pre>
1717
repositories {
1818
...
19-
maven { url "https://jitpack.io" }
19+
maven { url "https://jitpack.io" } // <b><-- Add this!</b>
2020
}
2121

2222
dependencies {
23-
compile 'com.github.fikisipi:SmartCache:2.9.0'
23+
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
24+
implementation 'com.github.fikisipi:SmartCache:2.9.0' // <b><-- Add this!</b>
2425
}
25-
```
26+
</pre>
2627

2728
2. Add `SmartCallFactory` to your Retrofit `Builder`:
2829
<pre>

retrofit-smartcache/build.gradle

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ dependencies {
2929
testImplementation 'junit:junit:4.13'
3030
testImplementation 'com.squareup.okhttp3:mockwebserver:4.9.1'
3131
implementation 'com.jakewharton:disklrucache:2.0.2'
32-
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
33-
//compile 'com.google.guava:guava:18.0'
32+
implementation 'com.squareup.retrofit2:retrofit:2.9.0+'
3433
}

retrofit-smartcache/src/main/java/dimitrovskif/smartcache/SmartCallFactory.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class SmartCallFactory extends CallAdapter.Factory {
2424
private final CachingSystem cachingSystem;
2525
private final Executor asyncExecutor;
26-
private final CachingSystem.RequestFilter requestFilter;
26+
private CachingSystem.RequestFilter requestFilter;
2727

2828
public static final CachingSystem.RequestFilter CACHE_GET_REQUESTS = new CachingSystem.RequestFilter() {
2929
@Override
@@ -50,6 +50,10 @@ public static SmartCallFactory createBasic(Context ctx) {
5050
return new SmartCallFactory(BasicCaching.buildFromContext(ctx));
5151
}
5252

53+
public static SmartCallFactory createBasic(Context ctx, CachingSystem.RequestFilter requestFilter) {
54+
return new SmartCallFactory(BasicCaching.buildFromContext(ctx), new AndroidExecutor(), requestFilter);
55+
}
56+
5357
@Override
5458
public CallAdapter<?, ?> get(final Type returnType, final Annotation[] annotations,
5559
final Retrofit retrofit) {

retrofit-smartcache/src/test/java/dimitrovskif/smartcache/CallbackTest.java

+92-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dimitrovskif.smartcache;
22

3+
import org.junit.After;
4+
import org.junit.Before;
35
import org.junit.Rule;
46
import org.junit.Test;
57

@@ -33,25 +35,38 @@ private CallAdapter.Factory buildSmartCacheFactory(){
3335
return factory;
3436
}
3537

38+
private DemoService demoService = null;
39+
private Logger log = null;
40+
41+
@Before
42+
public void setUp() {
43+
Retrofit r = new Retrofit.Builder()
44+
.baseUrl(server.url("/"))
45+
.addConverterFactory(new ToStringConverterFactory())
46+
.addCallAdapterFactory(buildSmartCacheFactory())
47+
.build();
48+
this.demoService = r.create(DemoService.class);
49+
log = Logger.getLogger("test_logger");
50+
}
51+
52+
@After
53+
public void tearDown() {
54+
this.demoService = null;
55+
}
56+
3657
@Rule public final MockWebServer server = new MockWebServer();
58+
3759
@Test
38-
public void dispatch_isGood() throws Exception{
60+
public void simpleNetworkTest() throws Exception {
3961
/* Set up the mock webserver */
4062
MockResponse resp = new MockResponse();
4163
resp.setBody("VERY_BASIC_BODY");
4264
server.enqueue(resp);
4365
server.enqueue(resp.clone());
4466

45-
Retrofit r = new Retrofit.Builder()
46-
.baseUrl(server.url("/"))
47-
.addConverterFactory(new ToStringConverterFactory())
48-
.addCallAdapterFactory(buildSmartCacheFactory())
49-
.build();
50-
DemoService demoService = r.create(DemoService.class);
51-
5267

5368
final Logger log = Logger.getLogger("test_logger");
54-
final CountDownLatch latch = new CountDownLatch(1);
69+
final CountDownLatch latch = new CountDownLatch(2);
5570
final AtomicReference<Response<String>> responseRef = new AtomicReference<>();
5671
demoService.getHome().enqueue(new Callback<String>() {
5772
@Override
@@ -66,54 +81,99 @@ public void onFailure(Call<String> call, Throwable t) {
6681
fail("Failure executing the request.");
6782
}
6883
});
69-
assertTrue(latch.await(1, TimeUnit.SECONDS));
84+
latch.await(1, TimeUnit.SECONDS);
85+
assertEquals(latch.getCount(), 1);
7086
assertEquals(responseRef.get().body(), "VERY_BASIC_BODY");
71-
final CountDownLatch latch2 = new CountDownLatch(2);
72-
final AtomicReference<Response<String>> response2Ref = new AtomicReference<>();
87+
assertTrue("No network flag on response.", SmartCache.isResponseFromNetwork(responseRef.get()));
88+
}
89+
90+
@Test
91+
public void twoTestCheck() throws Exception{
92+
/* Set up the mock webserver */
93+
MockResponse resp = new MockResponse();
94+
resp.setBody("VERY_BASIC_BODY");
95+
server.enqueue(resp);
96+
server.enqueue(resp.clone());
97+
98+
final CountDownLatch latch = new CountDownLatch(2);
99+
final AtomicReference<Response<String>> responseRef = new AtomicReference<>();
73100
demoService.getHome().enqueue(new Callback<String>() {
74101
@Override
75102
public void onResponse(Call<String> call, Response<String> response) {
76-
latch2.countDown();
77-
if(latch2.getCount() == 1){ // the cache hit one.
78-
log.info("Got a response. Should be a cache hit.");
79-
response2Ref.set(response);
80-
}else{ // the network one.
81-
log.info("Got a response. Should be a real network hit.");
82-
assertEquals(response.body(), response2Ref.get().body());
83-
}
103+
log.info("Got a response in pass #1 in twoTestCheck() call.");
104+
responseRef.set(response);
105+
latch.countDown();
84106
}
85107

86108
@Override
87109
public void onFailure(Call<String> call, Throwable t) {
88110
fail("Failure executing the request.");
89111
}
90112
});
91-
assertTrue(latch2.await(1, TimeUnit.SECONDS));
113+
latch.await(1, TimeUnit.SECONDS);
114+
assertEquals(latch.getCount(), 1);
115+
assertEquals(responseRef.get().body(), "VERY_BASIC_BODY");
116+
assertTrue("Response isn't from network.", SmartCache.isResponseFromNetwork(responseRef.get()));
92117

93-
server.enqueue(resp.clone());
118+
final CountDownLatch doubleLatch = new CountDownLatch(2);
119+
final AtomicReference<Response<String>> secondTestResponse = new AtomicReference<>();
120+
final AtomicReference<Response<String>> secondTestResponse2 = new AtomicReference<>();
94121

95-
final CountDownLatch latch3 = new CountDownLatch(2);
96-
final AtomicInteger postResponses = new AtomicInteger(0);
97-
// final AtomicReference<Response<String>> response3Ref = new AtomicReference<>();
98-
demoService.postHome().enqueue(new Callback<String>() {
122+
demoService.getHome().enqueue(new Callback<String>() {
99123
@Override
100124
public void onResponse(Call<String> call, Response<String> response) {
101-
log.info("Got post response");
102-
postResponses.incrementAndGet();
103-
latch3.countDown();
125+
doubleLatch.countDown();
126+
if(doubleLatch.getCount() == 1){
127+
secondTestResponse.set(response);
128+
log.info("Got a response in pass #2 in twoTestCheck() call. [latch = 1]");
129+
} else { // the network one.
130+
secondTestResponse2.set(response);
131+
log.info("Got a response in pass #2 in twoTestCheck() call. [latch = 2]");
132+
}
104133
}
105134

106135
@Override
107136
public void onFailure(Call<String> call, Throwable t) {
108137
fail("Failure executing the request.");
109138
}
110139
});
111-
latch3.await(1, TimeUnit.SECONDS);
112-
assertEquals(1, postResponses.get());
113140

141+
assertTrue(doubleLatch.await(1, TimeUnit.SECONDS));
142+
Response<String> resp1 = secondTestResponse.get();
143+
Response<String> resp2 = secondTestResponse2.get();
144+
145+
assertNotNull(resp1);
146+
assertNotNull(resp2);
147+
148+
assertFalse(SmartCache.isResponseFromNetwork(secondTestResponse.get()));
149+
assertTrue(SmartCache.isResponseFromNetwork(secondTestResponse2.get()));
150+
}
151+
152+
@Test
153+
public void testDisabledCache() throws Exception {
154+
MockResponse resp = new MockResponse();
155+
resp.setBody("VERY_BASIC_BODY");
156+
server.enqueue(resp);
114157
server.enqueue(resp.clone());
115158

116-
assertTrue("Synchronous call works.", demoService.getHome().execute().body().length() > 0);
159+
for(int i = 0; i < 2; i++) {
160+
log.info("Doing disabled test cache twice. i=" + String.valueOf(i));
161+
CountDownLatch latch = new CountDownLatch(2);
162+
demoService.postHome().enqueue(new Callback<String>() {
163+
@Override
164+
public void onResponse(Call<String> call, Response<String> response) {
165+
latch.countDown();
166+
log.info("Got a response in testDisabledCache()");
167+
}
168+
169+
@Override
170+
public void onFailure(Call<String> call, Throwable t) {
171+
log.warning("Got a failure in testDisabledCache()");
172+
}
173+
});
174+
latch.await(1, TimeUnit.SECONDS);
175+
assertEquals(latch.getCount(), 1);
176+
}
117177
}
118178

119179
static class MainThreadExecutor implements Executor{

0 commit comments

Comments
 (0)