|
24 | 24 | import static org.junit.Assert.assertTrue;
|
25 | 25 | import static org.junit.Assert.fail;
|
26 | 26 | import static org.mockito.Matchers.anyInt;
|
| 27 | +import static org.mockito.Mockito.doAnswer; |
27 | 28 | import static org.mockito.Mockito.doThrow;
|
28 | 29 | import static org.mockito.Mockito.mock;
|
29 | 30 | import static org.mockito.Mockito.spy;
|
@@ -1398,6 +1399,50 @@ public void testClientGetTimeout() throws IOException {
|
1398 | 1399 | assertEquals(Client.getTimeout(config), -1);
|
1399 | 1400 | }
|
1400 | 1401 |
|
| 1402 | + @Test(timeout=60000) |
| 1403 | + public void testSetupConnectionShouldNotBlockShutdown() throws Exception { |
| 1404 | + // Start server |
| 1405 | + SocketFactory mockFactory = Mockito.mock(SocketFactory.class); |
| 1406 | + Server server = new TestServer(1, true); |
| 1407 | + final InetSocketAddress addr = NetUtils.getConnectAddress(server); |
| 1408 | + |
| 1409 | + // Track how many times we retried to set up the connection |
| 1410 | + final AtomicInteger createSocketCalled = new AtomicInteger(); |
| 1411 | + |
| 1412 | + doAnswer(new Answer() { |
| 1413 | + @Override |
| 1414 | + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { |
| 1415 | + createSocketCalled.addAndGet(1); |
| 1416 | + Thread.sleep(MIN_SLEEP_TIME * 5); |
| 1417 | + throw new ConnectTimeoutException("fake"); |
| 1418 | + } |
| 1419 | + }).when(mockFactory).createSocket(); |
| 1420 | + final Client client = new Client(LongWritable.class, conf, mockFactory); |
| 1421 | + |
| 1422 | + final AtomicBoolean callStarted = new AtomicBoolean(false); |
| 1423 | + |
| 1424 | + // Call a random function asynchronously so that we can call stop() |
| 1425 | + new Thread(new Runnable() { |
| 1426 | + public void run() { |
| 1427 | + try { |
| 1428 | + callStarted.set(true); |
| 1429 | + call(client, RANDOM.nextLong(), addr, conf); |
| 1430 | + } catch (IOException ignored) {} |
| 1431 | + } |
| 1432 | + }).start(); |
| 1433 | + |
| 1434 | + GenericTestUtils.waitFor(new Supplier<Boolean>() { |
| 1435 | + @Override |
| 1436 | + public Boolean get() { |
| 1437 | + return callStarted.get() && createSocketCalled.get() == 1; |
| 1438 | + } |
| 1439 | + }, 50, 60000); |
| 1440 | + |
| 1441 | + // stop() should stop the client immediately without any more retries |
| 1442 | + client.stop(); |
| 1443 | + assertEquals(1, createSocketCalled.get()); |
| 1444 | + } |
| 1445 | + |
1401 | 1446 | private void assertRetriesOnSocketTimeouts(Configuration conf,
|
1402 | 1447 | int maxTimeoutRetries) throws IOException {
|
1403 | 1448 | SocketFactory mockFactory = Mockito.mock(SocketFactory.class);
|
|
0 commit comments