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

Commit 7796a90

Browse files
authored
Tests: Fix IndexTest.searchAync and IndexTest.DNSTimeout (#191)
* IndexTest: Fix searchAsync(timeout) doing 2 queries * IndexTest: Update reason for skipping DNS tests on Travis * IndexTest: Update DNS test to test for failure after timeout
1 parent 776b03d commit 7796a90

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,6 @@ public void searchAsync() throws Exception {
115115

116116
public void searchAsync(int waitTimeoutSeconds) throws Exception {
117117
final long begin = System.nanoTime();
118-
// Empty search.
119-
index.searchAsync(new Query(), new AssertCompletionHandler() {
120-
@Override public void doRequestCompleted(JSONObject content, AlgoliaException error) {
121-
if (error == null) {
122-
assertEquals("Result length does not match nbHits", objects.size(), content.optInt("nbHits"));
123-
} else {
124-
fail(error.getMessage());
125-
}
126-
}
127-
});
128-
129118
// Search with query.
130119
index.searchAsync(new Query("Francisco"), new AssertCompletionHandler() {
131120
@Override public void doRequestCompleted(JSONObject content, AlgoliaException error) {
@@ -451,30 +440,42 @@ public void hostSwitch() throws Exception {
451440

452441
@Test
453442
public void DNSTimeout() throws Exception {
454-
// On Travis, the imposed DNS timeout prevents us from testing this feature.
443+
// On Travis, the reported run duration is not reliable.
455444
if ("true".equals(System.getenv("TRAVIS"))) {
456445
return;
457446
}
458447

459-
// Given first host resulting in a DNS Timeout
448+
// Given all hosts resulting in a DNS Timeout
460449
String appId = (String) Whitebox.getInternalState(client, "applicationID");
461450
List<String> hostsArray = (List<String>) Whitebox.getInternalState(client, "readHosts");
462-
hostsArray.set(0, appId + "-dsn.algolia.biz");
451+
final String hostName = appId + "-dsn.algolia.biz"; //TODO: Random host name to avoid system DNS cache
452+
hostsArray.set(0, hostName);
453+
hostsArray.set(1, hostName);
454+
hostsArray.set(2, hostName);
455+
hostsArray.set(3, hostName);
463456
Whitebox.setInternalState(client, "readHosts", hostsArray);
464457

465-
client.setConnectTimeout(2000);
458+
//A connect timeout of 500 ms
459+
final int timeout = 500;
460+
client.setConnectTimeout(timeout);
466461

467462
//And an index that does not cache search queries
468463
index.disableSearchCache();
469464

470465

471-
// Expect successful search within 5 seconds
472-
long startTime = System.nanoTime();
473-
searchAsync(5);
474-
final long duration = (System.nanoTime() - startTime) / 1000000;
475-
476-
// Which should take at least 2 seconds, as per Client.connectTimeout
477-
assertTrue("We should first timeout before successfully searching, but test took only " + duration + " ms.", duration > 2000);
466+
// Expect failed search after timeout
467+
final long startTime = System.nanoTime();
468+
index.searchAsync(new Query(), new AssertCompletionHandler() {
469+
@Override
470+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
471+
if (error != null) {
472+
final long duration = (System.nanoTime() - startTime) / 1000000;
473+
assertTrue("We should hit 4 times the timeout before failing, but test took only " + duration + " ms.", duration > timeout * 4);
474+
} else {
475+
fail("Searching with failing hosts should result in an error.");
476+
}
477+
}
478+
});
478479
}
479480

480481
@Test

0 commit comments

Comments
 (0)