|
29 | 29 | import org.mockito.internal.util.reflection.Whitebox;
|
30 | 30 | import org.robolectric.util.concurrent.RoboExecutorService;
|
31 | 31 |
|
| 32 | +import java.io.IOException; |
| 33 | +import java.net.ServerSocket; |
| 34 | +import java.net.Socket; |
32 | 35 | import java.util.ArrayList;
|
33 | 36 | import java.util.Arrays;
|
34 | 37 | import java.util.HashMap;
|
@@ -464,6 +467,67 @@ public void testDNSTimeout() throws Exception {
|
464 | 467 | assertTrue("We should first timeout before successfully searching, but test took only " + duration + " ms.", duration > 2000);
|
465 | 468 | }
|
466 | 469 |
|
| 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 | + |
467 | 531 | @Test
|
468 | 532 | public void testSNI() throws Exception {
|
469 | 533 | // Given all hosts using SNI
|
|
0 commit comments