Fix race in TcpOutboundGatewayTests

https://build.spring.io/browse/INT-MASTERSPRING40-636/

Move `serverLatch.countDown()` to a finally block.
This commit is contained in:
Gary Russell
2019-03-05 11:51:53 -05:00
parent 200f3b3c1d
commit bb62cb8471

View File

@@ -306,7 +306,7 @@ public class TcpOutboundGatewayTests {
final int port = serverSocket.getLocalPort();
AbstractClientConnectionFactory ccf = buildCF(port);
ccf.start();
testGoodNetGWTimeoutGuts(port, ccf, serverSocket);
testGoodNetGWTimeoutGuts(ccf, serverSocket);
serverSocket.close();
}
@@ -317,7 +317,7 @@ public class TcpOutboundGatewayTests {
AbstractClientConnectionFactory ccf = buildCF(port);
CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1);
cccf.start();
testGoodNetGWTimeoutGuts(port, cccf, serverSocket);
testGoodNetGWTimeoutGuts(cccf, serverSocket);
serverSocket.close();
}
@@ -336,8 +336,9 @@ public class TcpOutboundGatewayTests {
* own response, not that for the first.
* @throws Exception
*/
private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFactory ccf,
private void testGoodNetGWTimeoutGuts(AbstractClientConnectionFactory ccf,
final ServerSocket server) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
/*
@@ -366,12 +367,14 @@ public class TcpOutboundGatewayTests {
oos.writeObject(request.replace("Test", "Reply"));
logger.debug("Replied to " + request);
lastReceived.set(request);
serverLatch.countDown();
}
catch (IOException e1) {
logger.debug("error on write " + e1.getClass().getSimpleName());
logger.debug("error on write " + e1.getMessage());
socket.close();
}
finally {
serverLatch.countDown();
}
}
}
}
@@ -392,7 +395,7 @@ public class TcpOutboundGatewayTests {
Expression remoteTimeoutExpression = Mockito.mock(Expression.class);
when(remoteTimeoutExpression.getValue(Mockito.any(EvaluationContext.class), Mockito.any(Message.class),
Mockito.eq(Long.class))).thenReturn(50L, 10000L);
Mockito.eq(Long.class))).thenReturn(50L, 60000L);
gateway.setRemoteTimeoutExpression(remoteTimeoutExpression);
@@ -620,7 +623,7 @@ public class TcpOutboundGatewayTests {
ccf.setSoTimeout(10000);
ccf.setSingleUse(false);
ccf.start();
testGWPropagatesSocketCloseGuts(port, ccf, serverSocket);
testGWPropagatesSocketCloseGuts(ccf, serverSocket);
serverSocket.close();
}
@@ -634,7 +637,7 @@ public class TcpOutboundGatewayTests {
ccf.setSoTimeout(10000);
ccf.setSingleUse(false);
ccf.start();
testGWPropagatesSocketCloseGuts(port, ccf, serverSocket);
testGWPropagatesSocketCloseGuts(ccf, serverSocket);
serverSocket.close();
}
@@ -649,7 +652,7 @@ public class TcpOutboundGatewayTests {
ccf.setSingleUse(false);
CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1);
cccf.start();
testGWPropagatesSocketCloseGuts(port, cccf, serverSocket);
testGWPropagatesSocketCloseGuts(cccf, serverSocket);
serverSocket.close();
}
@@ -665,12 +668,13 @@ public class TcpOutboundGatewayTests {
FailoverClientConnectionFactory focf = new FailoverClientConnectionFactory(
Collections.singletonList(ccf));
focf.start();
testGWPropagatesSocketCloseGuts(port, focf, serverSocket);
testGWPropagatesSocketCloseGuts(focf, serverSocket);
serverSocket.close();
}
private void testGWPropagatesSocketCloseGuts(final int port, AbstractClientConnectionFactory ccf,
private void testGWPropagatesSocketCloseGuts(AbstractClientConnectionFactory ccf,
final ServerSocket server) throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
final AtomicReference<String> lastReceived = new AtomicReference<>();
@@ -693,6 +697,7 @@ public class TcpOutboundGatewayTests {
serverLatch.countDown();
}
catch (IOException e1) {
logger.debug("error on write " + e1.getMessage());
socket1.close();
}
}
@@ -707,7 +712,7 @@ public class TcpOutboundGatewayTests {
try {
socket2.close();
}
catch (IOException e3) {
catch (@SuppressWarnings("unused") IOException e3) {
}
}
});
@@ -748,7 +753,7 @@ public class TcpOutboundGatewayTests {
ccf.setSoTimeout(100);
ccf.setSingleUse(false);
ccf.start();
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
serverSocket.close();
}
@@ -762,7 +767,7 @@ public class TcpOutboundGatewayTests {
ccf.setSoTimeout(100);
ccf.setSingleUse(false);
ccf.start();
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
serverSocket.close();
}
@@ -776,7 +781,7 @@ public class TcpOutboundGatewayTests {
ccf.setSoTimeout(100);
ccf.setSingleUse(true);
ccf.start();
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
serverSocket.close();
}
@@ -790,12 +795,13 @@ public class TcpOutboundGatewayTests {
ccf.setSoTimeout(100);
ccf.setSingleUse(true);
ccf.start();
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
serverSocket.close();
}
private void testGWPropagatesSocketTimeoutGuts(final int port, AbstractClientConnectionFactory ccf,
private void testGWPropagatesSocketTimeoutGuts(AbstractClientConnectionFactory ccf,
final ServerSocket server) throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -816,7 +822,7 @@ public class TcpOutboundGatewayTests {
try {
socket.close();
}
catch (IOException e2) {
catch (@SuppressWarnings("unused") IOException e2) {
}
}
});
@@ -873,7 +879,7 @@ public class TcpOutboundGatewayTests {
try {
socket.close();
}
catch (IOException e2) {
catch (@SuppressWarnings("unused") IOException e2) {
}
}
});