Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit 1e30bc3

Browse files
author
Clément Le Provost
committed
[test][offline] Use AssertCompletionHandler
[ci skip]
1 parent fecd999 commit 1e30bc3

File tree

4 files changed

+132
-150
lines changed

4 files changed

+132
-150
lines changed

algoliasearch/src/testOffline/java/com/algolia/search/saas/MirroredIndexTest.java

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import static org.junit.Assert.assertFalse;
4242
import static org.junit.Assert.assertNotEquals;
4343
import static org.junit.Assert.assertNull;
44-
import static org.junit.Assert.assertTrue;
4544
import static org.junit.Assert.fail;
4645

4746

@@ -106,26 +105,26 @@ interface SyncCompletionHandler {
106105

107106
private void sync(final @NonNull MirroredIndex index, final @NonNull SyncCompletionHandler completionHandler) {
108107
// Delete the index.
109-
client.deleteIndexAsync(index.getIndexName(), new CompletionHandler() {
108+
client.deleteIndexAsync(index.getIndexName(), new AssertCompletionHandler() {
110109
@Override
111-
public void requestCompleted(JSONObject content, AlgoliaException error) {
110+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
112111
assertNull(error);
113112
int taskID = content.optInt("taskID", -1);
114113
assertNotEquals(-1, taskID);
115-
index.waitTaskAsync(Integer.toString(taskID), new CompletionHandler() {
114+
index.waitTaskAsync(Integer.toString(taskID), new AssertCompletionHandler() {
116115
@Override
117-
public void requestCompleted(JSONObject content, AlgoliaException error) {
116+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
118117
assertNull(error);
119118
// Populate the online index.
120-
index.addObjectsAsync(new JSONArray(moreObjects.values()), new CompletionHandler() {
119+
index.addObjectsAsync(new JSONArray(moreObjects.values()), new AssertCompletionHandler() {
121120
@Override
122-
public void requestCompleted(JSONObject content, AlgoliaException error) {
121+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
123122
assertNull(error);
124123
int taskID = content.optInt("taskID", -1);
125124
assertNotEquals(-1, taskID);
126-
index.waitTaskAsync(Integer.toString(taskID), new CompletionHandler() {
125+
index.waitTaskAsync(Integer.toString(taskID), new AssertCompletionHandler() {
127126
@Override
128-
public void requestCompleted(JSONObject content, AlgoliaException error) {
127+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
129128
assertNull(error);
130129

131130
// Sync the offline mirror.
@@ -214,7 +213,6 @@ public void syncDidFinish(MirroredIndex index, Throwable error, MirroredIndex.Sy
214213
}
215214
}
216215
});
217-
assertTrue("No callback was called", signal.await(onlineTimeout * 2 + waitTimeout, TimeUnit.SECONDS));
218216
}
219217

220218
@Test
@@ -229,9 +227,9 @@ public void syncCompleted(@Nullable Throwable error) {
229227
assertNull(error);
230228

231229
// Query the online index explicitly.
232-
index.searchOnlineAsync(new Query(), new CompletionHandler() {
230+
index.searchOnlineAsync(new Query(), new AssertCompletionHandler() {
233231
@Override
234-
public void requestCompleted(JSONObject content, AlgoliaException error) {
232+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
235233
assertNull(error);
236234
assertEquals(5, content.optInt("nbHits"));
237235
assertEquals("remote", content.optString("origin"));
@@ -240,9 +238,9 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
240238
});
241239

242240
// Query the offline index explicitly.
243-
index.searchOfflineAsync(new Query(), new CompletionHandler() {
241+
index.searchOfflineAsync(new Query(), new AssertCompletionHandler() {
244242
@Override
245-
public void requestCompleted(JSONObject content, AlgoliaException error) {
243+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
246244
assertNull(error);
247245
assertEquals(3, content.optInt("nbHits"));
248246
assertEquals("local", content.optString("origin"));
@@ -251,7 +249,6 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
251249
});
252250
}
253251
});
254-
assertTrue("No callback was called", signal.await(onlineTimeout, TimeUnit.SECONDS));
255252
}
256253

257254
@Test
@@ -268,9 +265,9 @@ public void syncCompleted(@Nullable Throwable error) {
268265
// Test offline fallback.
269266
client.setReadHosts("unknown.algolia.com");
270267
index.setRequestStrategy(MirroredIndex.Strategy.FALLBACK_ON_FAILURE);
271-
index.searchAsync(new Query(), new CompletionHandler() {
268+
index.searchAsync(new Query(), new AssertCompletionHandler() {
272269
@Override
273-
public void requestCompleted(JSONObject content, AlgoliaException error) {
270+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
274271
assertNull(error);
275272
assertEquals(3, content.optInt("nbHits"));
276273
assertEquals("local", content.optString("origin"));
@@ -279,7 +276,6 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
279276
});
280277
}
281278
});
282-
assertTrue("No callback was called", signal.await(onlineTimeout, TimeUnit.SECONDS));
283279
}
284280

285281
@Test
@@ -294,27 +290,25 @@ public void syncCompleted(@Nullable Throwable error) {
294290
assertNull(error);
295291

296292
// Query the online index explicitly.
297-
index.browseAsync(new Query(), new CompletionHandler() {
293+
index.browseAsync(new Query(), new AssertCompletionHandler() {
298294
@Override
299-
public void requestCompleted(JSONObject content, AlgoliaException error) {
295+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
300296
assertNull(error);
301297
assertEquals(5, content.optInt("nbHits"));
302298
signal.countDown();
303299
}
304300
});
305301

306302
// Query the offline index explicitly.
307-
index.browseMirrorAsync(new Query(), new CompletionHandler() {
303+
index.browseMirrorAsync(new Query(), new AssertCompletionHandler() {
308304
@Override
309-
public void requestCompleted(JSONObject content, AlgoliaException error) {
305+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
310306
assertNull(error);
311307
assertEquals(3, content.optInt("nbHits"));
312308
signal.countDown();
313309
}
314310
});
315311
}
316312
});
317-
assertTrue("No callback was called", signal.await(onlineTimeout, TimeUnit.SECONDS));
318313
}
319-
320314
}

algoliasearch/src/testOffline/java/com/algolia/search/saas/OfflineClientTest.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.junit.Test;
2929

3030
import java.util.concurrent.CountDownLatch;
31-
import java.util.concurrent.TimeUnit;
3231

3332
import static org.junit.Assert.assertEquals;
3433
import static org.junit.Assert.assertFalse;
@@ -46,9 +45,9 @@ public class OfflineClientTest extends OfflineTestBase {
4645
public void testListIndexes() throws Exception {
4746
final CountDownLatch signal = new CountDownLatch(1);
4847
final OfflineIndex index = client.getOfflineIndex(Helpers.getMethodName());
49-
client.listIndexesOfflineAsync(new CompletionHandler() {
48+
client.listIndexesOfflineAsync(new AssertCompletionHandler() {
5049
@Override
51-
public void requestCompleted(JSONObject content, AlgoliaException error) {
50+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
5251
// Check that response is valid.
5352
assertNotNull(content);
5453
JSONArray items = content.optJSONArray("items");
@@ -62,17 +61,17 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
6261
assertNotEquals(index.getName(), name);
6362
}
6463
final OfflineIndex.WriteTransaction transaction = index.newTransaction();
65-
transaction.saveObjectAsync(objects.get("snoopy"), new CompletionHandler() {
64+
transaction.saveObjectAsync(objects.get("snoopy"), new AssertCompletionHandler() {
6665
@Override
67-
public void requestCompleted(JSONObject content, AlgoliaException error) {
66+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
6867
assertNull(error);
69-
transaction.commitAsync(new CompletionHandler() {
68+
transaction.commitAsync(new AssertCompletionHandler() {
7069
@Override
71-
public void requestCompleted(JSONObject content, AlgoliaException error) {
70+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
7271
assertNull(error);
73-
client.listIndexesOfflineAsync(new CompletionHandler() {
72+
client.listIndexesOfflineAsync(new AssertCompletionHandler() {
7473
@Override
75-
public void requestCompleted(JSONObject content, AlgoliaException error) {
74+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
7675
// Check that response is valid.
7776
assertNotNull(content);
7877
JSONArray items = content.optJSONArray("items");
@@ -98,26 +97,25 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
9897
});
9998
}
10099
});
101-
assertTrue("No callback was called", signal.await(waitTimeout, TimeUnit.SECONDS));
102100
}
103101

104102
@Test
105103
public void testDeleteIndex() throws Exception {
106104
final CountDownLatch signal = new CountDownLatch(1);
107105
final OfflineIndex index = client.getOfflineIndex(Helpers.getMethodName());
108106
final OfflineIndex.WriteTransaction transaction = index.newTransaction();
109-
transaction.saveObjectsAsync(new JSONArray(objects.values()), new CompletionHandler() {
107+
transaction.saveObjectsAsync(new JSONArray(objects.values()), new AssertCompletionHandler() {
110108
@Override
111-
public void requestCompleted(JSONObject content, AlgoliaException error) {
109+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
112110
assertNull(error);
113-
transaction.commitAsync(new CompletionHandler() {
111+
transaction.commitAsync(new AssertCompletionHandler() {
114112
@Override
115-
public void requestCompleted(JSONObject content, AlgoliaException error) {
113+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
116114
assertNull(error);
117115
assertTrue(client.hasOfflineData(index.getName()));
118-
client.deleteIndexOfflineAsync(index.getName(), new CompletionHandler() {
116+
client.deleteIndexOfflineAsync(index.getName(), new AssertCompletionHandler() {
119117
@Override
120-
public void requestCompleted(JSONObject content, AlgoliaException error) {
118+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
121119
assertNotNull(content);
122120
assertNotNull(content.optString("deletedAt", null));
123121
assertFalse(client.hasOfflineData(index.getName()));
@@ -128,7 +126,6 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
128126
});
129127
}
130128
});
131-
assertTrue("No callback was called", signal.await(waitTimeout, TimeUnit.SECONDS));
132129
}
133130

134131
@Test
@@ -137,26 +134,26 @@ public void testMoveIndex() throws Exception {
137134
final OfflineIndex srcIndex = client.getOfflineIndex(Helpers.getMethodName());
138135
final OfflineIndex dstIndex = client.getOfflineIndex(Helpers.getMethodName() + "_new");
139136
final OfflineIndex.WriteTransaction transaction = srcIndex.newTransaction();
140-
transaction.saveObjectsAsync(new JSONArray(objects.values()), new CompletionHandler() {
137+
transaction.saveObjectsAsync(new JSONArray(objects.values()), new AssertCompletionHandler() {
141138
@Override
142-
public void requestCompleted(JSONObject content, AlgoliaException error) {
139+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
143140
assertNull(error);
144-
transaction.commitAsync(new CompletionHandler() {
141+
transaction.commitAsync(new AssertCompletionHandler() {
145142
@Override
146-
public void requestCompleted(JSONObject content, AlgoliaException error) {
143+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
147144
assertNull(error);
148145
assertTrue(client.hasOfflineData(srcIndex.getName()));
149146
assertFalse(client.hasOfflineData(dstIndex.getName()));
150-
client.moveIndexOfflineAsync(srcIndex.getName(), dstIndex.getName(), new CompletionHandler() {
147+
client.moveIndexOfflineAsync(srcIndex.getName(), dstIndex.getName(), new AssertCompletionHandler() {
151148
@Override
152-
public void requestCompleted(JSONObject content, AlgoliaException error) {
149+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
153150
assertNotNull(content);
154151
assertNotNull(content.optString("updatedAt", null));
155152
assertFalse(client.hasOfflineData(srcIndex.getName()));
156153
assertTrue(client.hasOfflineData(dstIndex.getName()));
157-
dstIndex.searchAsync(new Query("woodstock"), new CompletionHandler() {
154+
dstIndex.searchAsync(new Query("woodstock"), new AssertCompletionHandler() {
158155
@Override
159-
public void requestCompleted(JSONObject content, AlgoliaException error) {
156+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
160157
assertNotNull(content);
161158
assertEquals(1, content.optInt("nbHits"));
162159
signal.countDown();
@@ -168,6 +165,5 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
168165
});
169166
}
170167
});
171-
assertTrue("No callback was called", signal.await(waitTimeout, TimeUnit.SECONDS));
172168
}
173169
}

0 commit comments

Comments
 (0)