1
1
package dimitrovskif .smartcache ;
2
2
3
+ import org .junit .After ;
4
+ import org .junit .Before ;
3
5
import org .junit .Rule ;
4
6
import org .junit .Test ;
5
7
@@ -33,25 +35,38 @@ private CallAdapter.Factory buildSmartCacheFactory(){
33
35
return factory ;
34
36
}
35
37
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
+
36
57
@ Rule public final MockWebServer server = new MockWebServer ();
58
+
37
59
@ Test
38
- public void dispatch_isGood () throws Exception {
60
+ public void simpleNetworkTest () throws Exception {
39
61
/* Set up the mock webserver */
40
62
MockResponse resp = new MockResponse ();
41
63
resp .setBody ("VERY_BASIC_BODY" );
42
64
server .enqueue (resp );
43
65
server .enqueue (resp .clone ());
44
66
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
-
52
67
53
68
final Logger log = Logger .getLogger ("test_logger" );
54
- final CountDownLatch latch = new CountDownLatch (1 );
69
+ final CountDownLatch latch = new CountDownLatch (2 );
55
70
final AtomicReference <Response <String >> responseRef = new AtomicReference <>();
56
71
demoService .getHome ().enqueue (new Callback <String >() {
57
72
@ Override
@@ -66,54 +81,99 @@ public void onFailure(Call<String> call, Throwable t) {
66
81
fail ("Failure executing the request." );
67
82
}
68
83
});
69
- assertTrue (latch .await (1 , TimeUnit .SECONDS ));
84
+ latch .await (1 , TimeUnit .SECONDS );
85
+ assertEquals (latch .getCount (), 1 );
70
86
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 <>();
73
100
demoService .getHome ().enqueue (new Callback <String >() {
74
101
@ Override
75
102
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 ();
84
106
}
85
107
86
108
@ Override
87
109
public void onFailure (Call <String > call , Throwable t ) {
88
110
fail ("Failure executing the request." );
89
111
}
90
112
});
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 ()));
92
117
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 <>();
94
121
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 >() {
99
123
@ Override
100
124
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
+ }
104
133
}
105
134
106
135
@ Override
107
136
public void onFailure (Call <String > call , Throwable t ) {
108
137
fail ("Failure executing the request." );
109
138
}
110
139
});
111
- latch3 .await (1 , TimeUnit .SECONDS );
112
- assertEquals (1 , postResponses .get ());
113
140
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 );
114
157
server .enqueue (resp .clone ());
115
158
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
+ }
117
177
}
118
178
119
179
static class MainThreadExecutor implements Executor {
0 commit comments