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

Commit 7aecbaf

Browse files
committed
Test: Ensuring graceful DNS timeout handling (#79)
1 parent e68236b commit 7aecbaf

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

algoliasearch/src/test/java/com/algolia/search/saas/IndexTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public void tearDown() throws Exception {
9191

9292
@Test
9393
public void testSearchAsync() throws Exception {
94+
testSearchAsync(Helpers.wait);
95+
}
96+
97+
public void testSearchAsync(int waitTimeout) throws Exception {
9498
// Empty search.
9599
final CountDownLatch signal = new CountDownLatch(1);
96100
index.searchAsync(new Query(), new CompletionHandler() {
@@ -104,7 +108,7 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
104108
signal.countDown();
105109
}
106110
});
107-
assertTrue("No callback was called", signal.await(Helpers.wait, TimeUnit.SECONDS));
111+
assertTrue("No callback was called", signal.await(waitTimeout, TimeUnit.SECONDS));
108112

109113
// Search with query.
110114
final CountDownLatch signal2 = new CountDownLatch(1);
@@ -119,7 +123,7 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
119123
signal2.countDown();
120124
}
121125
});
122-
assertTrue("No callback was called", signal2.await(Helpers.wait, TimeUnit.SECONDS));
126+
assertTrue("No callback was called", signal2.await(waitTimeout, TimeUnit.SECONDS));
123127
}
124128

125129
@Test
@@ -433,6 +437,26 @@ public void testHostSwitch() throws Exception {
433437
testSearchAsync();
434438
}
435439

440+
@Test
441+
public void testDNSTimeout() throws Exception {
442+
// Given first host resulting in a DNS Timeout
443+
String appId = (String) Whitebox.getInternalState(client, "applicationID");
444+
List<String> hostsArray = (List<String>) Whitebox.getInternalState(client, "readHosts");
445+
hostsArray.set(0, appId + "-dsn.algolia.biz");
446+
Whitebox.setInternalState(client, "readHosts", hostsArray);
447+
448+
//And an index that does not cache search queries
449+
index.disableSearchCache();
450+
451+
452+
// Expect successful search within 5 seconds
453+
long startTime = System.nanoTime();
454+
testSearchAsync(5);
455+
final long duration = (System.nanoTime() - startTime) / 1000000;
456+
457+
// Which should take at least 2 seconds, as per Client.connectTimeout
458+
assertTrue("We should first timeout before successfully searching, but test took only " + duration + " ms.", duration > 2000);
459+
}
436460

437461
@Test
438462
public void testSNI() throws Exception {

0 commit comments

Comments
 (0)