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

Commit 2c30109

Browse files
committed
Tests: Connect timeout & Connection reset
1 parent 5e7be4f commit 2c30109

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import org.mockito.internal.util.reflection.Whitebox;
3030
import org.robolectric.util.concurrent.RoboExecutorService;
3131

32+
import java.io.IOException;
33+
import java.net.ServerSocket;
34+
import java.net.Socket;
3235
import java.util.ArrayList;
3336
import java.util.Arrays;
3437
import java.util.HashMap;
@@ -464,6 +467,67 @@ public void testDNSTimeout() throws Exception {
464467
assertTrue("We should first timeout before successfully searching, but test took only " + duration + " ms.", duration > 2000);
465468
}
466469

470+
@Test
471+
public void testConnectTimeout() throws AlgoliaException {
472+
List<String> hostsArray = (List<String>) Whitebox.getInternalState(client, "readHosts");
473+
hostsArray.set(0, "notcp-xx-1.algolianet.com");
474+
Whitebox.setInternalState(client, "readHosts", hostsArray);
475+
476+
client.setConnectTimeout(1000);
477+
client.setReadTimeout(1000);
478+
479+
Long start = System.currentTimeMillis();
480+
assertNotNull(client.listIndexes());
481+
assertTrue((System.currentTimeMillis() - start) < 2 * 1000);
482+
}
483+
484+
@Test
485+
public void testMultipleConnectTimeout() throws AlgoliaException {
486+
List<String> hostsArray = (List<String>) Whitebox.getInternalState(client, "readHosts");
487+
hostsArray.set(0, "notcp-xx-1.algolianet.com");
488+
hostsArray.set(1, "notcp-xx-1.algolianet.com");
489+
Whitebox.setInternalState(client, "readHosts", hostsArray);
490+
491+
client.setConnectTimeout(1000);
492+
client.setReadTimeout(1000);
493+
494+
Long start = System.currentTimeMillis();
495+
assertNotNull(client.listIndexes());
496+
assertTrue((System.currentTimeMillis() - start) < 3 * 1000);
497+
}
498+
499+
500+
@Test
501+
public void testConnectionResetException() throws IOException, AlgoliaException {
502+
Thread runnable = new Thread() {
503+
@Override
504+
public void run() {
505+
try {
506+
ServerSocket serverSocket = new ServerSocket(8080);
507+
Socket socket = serverSocket.accept();
508+
socket.setSoLinger(true, 0);
509+
socket.close();
510+
} catch (IOException ignored) {
511+
ignored.printStackTrace();
512+
}
513+
}
514+
};
515+
516+
runnable.start();
517+
518+
List<String> hostsArray = (List<String>) Whitebox.getInternalState(client, "readHosts");
519+
hostsArray.set(0, "localhost:8080");
520+
hostsArray.set(1, "notcp-xx-1.algolianet.com");
521+
522+
client.setConnectTimeout(1000);
523+
client.setReadTimeout(1000);
524+
525+
Long start = System.currentTimeMillis();
526+
assertNotNull(client.listIndexes());
527+
long end = System.currentTimeMillis() - start;
528+
assertTrue(end < 2 * 1000);
529+
}
530+
467531
@Test
468532
public void testSNI() throws Exception {
469533
// Given all hosts using SNI

0 commit comments

Comments
 (0)