@@ -91,6 +91,10 @@ public void tearDown() throws Exception {
91
91
92
92
@ Test
93
93
public void testSearchAsync () throws Exception {
94
+ testSearchAsync (Helpers .wait );
95
+ }
96
+
97
+ public void testSearchAsync (int waitTimeout ) throws Exception {
94
98
// Empty search.
95
99
final CountDownLatch signal = new CountDownLatch (1 );
96
100
index .searchAsync (new Query (), new CompletionHandler () {
@@ -104,7 +108,7 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
104
108
signal .countDown ();
105
109
}
106
110
});
107
- assertTrue ("No callback was called" , signal .await (Helpers . wait , TimeUnit .SECONDS ));
111
+ assertTrue ("No callback was called" , signal .await (waitTimeout , TimeUnit .SECONDS ));
108
112
109
113
// Search with query.
110
114
final CountDownLatch signal2 = new CountDownLatch (1 );
@@ -119,7 +123,7 @@ public void requestCompleted(JSONObject content, AlgoliaException error) {
119
123
signal2 .countDown ();
120
124
}
121
125
});
122
- assertTrue ("No callback was called" , signal2 .await (Helpers . wait , TimeUnit .SECONDS ));
126
+ assertTrue ("No callback was called" , signal2 .await (waitTimeout , TimeUnit .SECONDS ));
123
127
}
124
128
125
129
@ Test
@@ -433,6 +437,26 @@ public void testHostSwitch() throws Exception {
433
437
testSearchAsync ();
434
438
}
435
439
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
+ }
436
460
437
461
@ Test
438
462
public void testSNI () throws Exception {
0 commit comments