@@ -115,17 +115,6 @@ public void searchAsync() throws Exception {
115
115
116
116
public void searchAsync (int waitTimeoutSeconds ) throws Exception {
117
117
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
-
129
118
// Search with query.
130
119
index .searchAsync (new Query ("Francisco" ), new AssertCompletionHandler () {
131
120
@ Override public void doRequestCompleted (JSONObject content , AlgoliaException error ) {
@@ -451,30 +440,42 @@ public void hostSwitch() throws Exception {
451
440
452
441
@ Test
453
442
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 .
455
444
if ("true" .equals (System .getenv ("TRAVIS" ))) {
456
445
return ;
457
446
}
458
447
459
- // Given first host resulting in a DNS Timeout
448
+ // Given all hosts resulting in a DNS Timeout
460
449
String appId = (String ) Whitebox .getInternalState (client , "applicationID" );
461
450
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 );
463
456
Whitebox .setInternalState (client , "readHosts" , hostsArray );
464
457
465
- client .setConnectTimeout (2000 );
458
+ //A connect timeout of 500 ms
459
+ final int timeout = 500 ;
460
+ client .setConnectTimeout (timeout );
466
461
467
462
//And an index that does not cache search queries
468
463
index .disableSearchCache ();
469
464
470
465
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
+ });
478
479
}
479
480
480
481
@ Test
0 commit comments