Skip to content

Commit bb62cb8

Browse files
committed
Fix race in TcpOutboundGatewayTests
https://build.spring.io/browse/INT-MASTERSPRING40-636/ Move `serverLatch.countDown()` to a finally block.
1 parent 200f3b3 commit bb62cb8

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public void testGoodNetGWTimeout() throws Exception {
306306
final int port = serverSocket.getLocalPort();
307307
AbstractClientConnectionFactory ccf = buildCF(port);
308308
ccf.start();
309-
testGoodNetGWTimeoutGuts(port, ccf, serverSocket);
309+
testGoodNetGWTimeoutGuts(ccf, serverSocket);
310310
serverSocket.close();
311311
}
312312

@@ -317,7 +317,7 @@ public void testGoodNetGWTimeoutCached() throws Exception {
317317
AbstractClientConnectionFactory ccf = buildCF(port);
318318
CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1);
319319
cccf.start();
320-
testGoodNetGWTimeoutGuts(port, cccf, serverSocket);
320+
testGoodNetGWTimeoutGuts(cccf, serverSocket);
321321
serverSocket.close();
322322
}
323323

@@ -336,8 +336,9 @@ private AbstractClientConnectionFactory buildCF(final int port) {
336336
* own response, not that for the first.
337337
* @throws Exception
338338
*/
339-
private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFactory ccf,
339+
private void testGoodNetGWTimeoutGuts(AbstractClientConnectionFactory ccf,
340340
final ServerSocket server) throws InterruptedException {
341+
341342
final CountDownLatch latch = new CountDownLatch(1);
342343
final AtomicBoolean done = new AtomicBoolean();
343344
/*
@@ -366,12 +367,14 @@ private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFa
366367
oos.writeObject(request.replace("Test", "Reply"));
367368
logger.debug("Replied to " + request);
368369
lastReceived.set(request);
369-
serverLatch.countDown();
370370
}
371371
catch (IOException e1) {
372-
logger.debug("error on write " + e1.getClass().getSimpleName());
372+
logger.debug("error on write " + e1.getMessage());
373373
socket.close();
374374
}
375+
finally {
376+
serverLatch.countDown();
377+
}
375378
}
376379
}
377380
}
@@ -392,7 +395,7 @@ private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFa
392395
Expression remoteTimeoutExpression = Mockito.mock(Expression.class);
393396

394397
when(remoteTimeoutExpression.getValue(Mockito.any(EvaluationContext.class), Mockito.any(Message.class),
395-
Mockito.eq(Long.class))).thenReturn(50L, 10000L);
398+
Mockito.eq(Long.class))).thenReturn(50L, 60000L);
396399

397400
gateway.setRemoteTimeoutExpression(remoteTimeoutExpression);
398401

@@ -620,7 +623,7 @@ public void testNetGWPropagatesSocketClose() throws Exception {
620623
ccf.setSoTimeout(10000);
621624
ccf.setSingleUse(false);
622625
ccf.start();
623-
testGWPropagatesSocketCloseGuts(port, ccf, serverSocket);
626+
testGWPropagatesSocketCloseGuts(ccf, serverSocket);
624627
serverSocket.close();
625628
}
626629

@@ -634,7 +637,7 @@ public void testNioGWPropagatesSocketClose() throws Exception {
634637
ccf.setSoTimeout(10000);
635638
ccf.setSingleUse(false);
636639
ccf.start();
637-
testGWPropagatesSocketCloseGuts(port, ccf, serverSocket);
640+
testGWPropagatesSocketCloseGuts(ccf, serverSocket);
638641
serverSocket.close();
639642
}
640643

@@ -649,7 +652,7 @@ public void testCachedGWPropagatesSocketClose() throws Exception {
649652
ccf.setSingleUse(false);
650653
CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1);
651654
cccf.start();
652-
testGWPropagatesSocketCloseGuts(port, cccf, serverSocket);
655+
testGWPropagatesSocketCloseGuts(cccf, serverSocket);
653656
serverSocket.close();
654657
}
655658

@@ -665,12 +668,13 @@ public void testFailoverGWPropagatesSocketClose() throws Exception {
665668
FailoverClientConnectionFactory focf = new FailoverClientConnectionFactory(
666669
Collections.singletonList(ccf));
667670
focf.start();
668-
testGWPropagatesSocketCloseGuts(port, focf, serverSocket);
671+
testGWPropagatesSocketCloseGuts(focf, serverSocket);
669672
serverSocket.close();
670673
}
671674

672-
private void testGWPropagatesSocketCloseGuts(final int port, AbstractClientConnectionFactory ccf,
675+
private void testGWPropagatesSocketCloseGuts(AbstractClientConnectionFactory ccf,
673676
final ServerSocket server) throws Exception {
677+
674678
final CountDownLatch latch = new CountDownLatch(1);
675679
final AtomicBoolean done = new AtomicBoolean();
676680
final AtomicReference<String> lastReceived = new AtomicReference<>();
@@ -693,6 +697,7 @@ private void testGWPropagatesSocketCloseGuts(final int port, AbstractClientConne
693697
serverLatch.countDown();
694698
}
695699
catch (IOException e1) {
700+
logger.debug("error on write " + e1.getMessage());
696701
socket1.close();
697702
}
698703
}
@@ -707,7 +712,7 @@ private void testGWPropagatesSocketCloseGuts(final int port, AbstractClientConne
707712
try {
708713
socket2.close();
709714
}
710-
catch (IOException e3) {
715+
catch (@SuppressWarnings("unused") IOException e3) {
711716
}
712717
}
713718
});
@@ -748,7 +753,7 @@ public void testNetGWPropagatesSocketTimeout() throws Exception {
748753
ccf.setSoTimeout(100);
749754
ccf.setSingleUse(false);
750755
ccf.start();
751-
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
756+
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
752757
serverSocket.close();
753758
}
754759

@@ -762,7 +767,7 @@ public void testNioGWPropagatesSocketTimeout() throws Exception {
762767
ccf.setSoTimeout(100);
763768
ccf.setSingleUse(false);
764769
ccf.start();
765-
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
770+
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
766771
serverSocket.close();
767772
}
768773

@@ -776,7 +781,7 @@ public void testNetGWPropagatesSocketTimeoutSingleUse() throws Exception {
776781
ccf.setSoTimeout(100);
777782
ccf.setSingleUse(true);
778783
ccf.start();
779-
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
784+
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
780785
serverSocket.close();
781786
}
782787

@@ -790,12 +795,13 @@ public void testNioGWPropagatesSocketTimeoutSingleUse() throws Exception {
790795
ccf.setSoTimeout(100);
791796
ccf.setSingleUse(true);
792797
ccf.start();
793-
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
798+
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
794799
serverSocket.close();
795800
}
796801

797-
private void testGWPropagatesSocketTimeoutGuts(final int port, AbstractClientConnectionFactory ccf,
802+
private void testGWPropagatesSocketTimeoutGuts(AbstractClientConnectionFactory ccf,
798803
final ServerSocket server) throws Exception {
804+
799805
final CountDownLatch latch = new CountDownLatch(1);
800806
final AtomicBoolean done = new AtomicBoolean();
801807

@@ -816,7 +822,7 @@ private void testGWPropagatesSocketTimeoutGuts(final int port, AbstractClientCon
816822
try {
817823
socket.close();
818824
}
819-
catch (IOException e2) {
825+
catch (@SuppressWarnings("unused") IOException e2) {
820826
}
821827
}
822828
});
@@ -873,7 +879,7 @@ public void testNioSecondChance() throws Exception {
873879
try {
874880
socket.close();
875881
}
876-
catch (IOException e2) {
882+
catch (@SuppressWarnings("unused") IOException e2) {
877883
}
878884
}
879885
});

0 commit comments

Comments
 (0)