Skip to content

Commit 3b1db3e

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

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
@@ -312,7 +312,7 @@ public void testGoodNetGWTimeout() throws Exception {
312312
final int port = serverSocket.getLocalPort();
313313
AbstractClientConnectionFactory ccf = buildCF(port);
314314
ccf.start();
315-
testGoodNetGWTimeoutGuts(port, ccf, serverSocket);
315+
testGoodNetGWTimeoutGuts(ccf, serverSocket);
316316
serverSocket.close();
317317
}
318318

@@ -323,7 +323,7 @@ public void testGoodNetGWTimeoutCached() throws Exception {
323323
AbstractClientConnectionFactory ccf = buildCF(port);
324324
CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1);
325325
cccf.start();
326-
testGoodNetGWTimeoutGuts(port, cccf, serverSocket);
326+
testGoodNetGWTimeoutGuts(cccf, serverSocket);
327327
serverSocket.close();
328328
}
329329

@@ -342,8 +342,9 @@ private AbstractClientConnectionFactory buildCF(final int port) {
342342
* own response, not that for the first.
343343
* @throws Exception
344344
*/
345-
private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFactory ccf,
345+
private void testGoodNetGWTimeoutGuts(AbstractClientConnectionFactory ccf,
346346
final ServerSocket server) throws InterruptedException {
347+
347348
final CountDownLatch latch = new CountDownLatch(1);
348349
final AtomicBoolean done = new AtomicBoolean();
349350
/*
@@ -372,12 +373,14 @@ private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFa
372373
oos.writeObject(request.replace("Test", "Reply"));
373374
logger.debug("Replied to " + request);
374375
lastReceived.set(request);
375-
serverLatch.countDown();
376376
}
377377
catch (IOException e1) {
378-
logger.debug("error on write " + e1.getClass().getSimpleName());
378+
logger.debug("error on write " + e1.getMessage());
379379
socket.close();
380380
}
381+
finally {
382+
serverLatch.countDown();
383+
}
381384
}
382385
}
383386
}
@@ -398,7 +401,7 @@ private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFa
398401
Expression remoteTimeoutExpression = Mockito.mock(Expression.class);
399402

400403
when(remoteTimeoutExpression.getValue(Mockito.any(EvaluationContext.class), Mockito.any(Message.class),
401-
Mockito.eq(Long.class))).thenReturn(50L, 10000L);
404+
Mockito.eq(Long.class))).thenReturn(50L, 60000L);
402405

403406
gateway.setRemoteTimeoutExpression(remoteTimeoutExpression);
404407

@@ -626,7 +629,7 @@ public void testNetGWPropagatesSocketClose() throws Exception {
626629
ccf.setSoTimeout(10000);
627630
ccf.setSingleUse(false);
628631
ccf.start();
629-
testGWPropagatesSocketCloseGuts(port, ccf, serverSocket);
632+
testGWPropagatesSocketCloseGuts(ccf, serverSocket);
630633
serverSocket.close();
631634
}
632635

@@ -640,7 +643,7 @@ public void testNioGWPropagatesSocketClose() throws Exception {
640643
ccf.setSoTimeout(10000);
641644
ccf.setSingleUse(false);
642645
ccf.start();
643-
testGWPropagatesSocketCloseGuts(port, ccf, serverSocket);
646+
testGWPropagatesSocketCloseGuts(ccf, serverSocket);
644647
serverSocket.close();
645648
}
646649

@@ -655,7 +658,7 @@ public void testCachedGWPropagatesSocketClose() throws Exception {
655658
ccf.setSingleUse(false);
656659
CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1);
657660
cccf.start();
658-
testGWPropagatesSocketCloseGuts(port, cccf, serverSocket);
661+
testGWPropagatesSocketCloseGuts(cccf, serverSocket);
659662
serverSocket.close();
660663
}
661664

@@ -671,12 +674,13 @@ public void testFailoverGWPropagatesSocketClose() throws Exception {
671674
FailoverClientConnectionFactory focf = new FailoverClientConnectionFactory(
672675
Collections.singletonList(ccf));
673676
focf.start();
674-
testGWPropagatesSocketCloseGuts(port, focf, serverSocket);
677+
testGWPropagatesSocketCloseGuts(focf, serverSocket);
675678
serverSocket.close();
676679
}
677680

678-
private void testGWPropagatesSocketCloseGuts(final int port, AbstractClientConnectionFactory ccf,
681+
private void testGWPropagatesSocketCloseGuts(AbstractClientConnectionFactory ccf,
679682
final ServerSocket server) throws Exception {
683+
680684
final CountDownLatch latch = new CountDownLatch(1);
681685
final AtomicBoolean done = new AtomicBoolean();
682686
final AtomicReference<String> lastReceived = new AtomicReference<>();
@@ -699,6 +703,7 @@ private void testGWPropagatesSocketCloseGuts(final int port, AbstractClientConne
699703
serverLatch.countDown();
700704
}
701705
catch (IOException e1) {
706+
logger.debug("error on write " + e1.getMessage());
702707
socket1.close();
703708
}
704709
}
@@ -713,7 +718,7 @@ private void testGWPropagatesSocketCloseGuts(final int port, AbstractClientConne
713718
try {
714719
socket2.close();
715720
}
716-
catch (IOException e3) {
721+
catch (@SuppressWarnings("unused") IOException e3) {
717722
}
718723
}
719724
});
@@ -754,7 +759,7 @@ public void testNetGWPropagatesSocketTimeout() throws Exception {
754759
ccf.setSoTimeout(100);
755760
ccf.setSingleUse(false);
756761
ccf.start();
757-
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
762+
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
758763
serverSocket.close();
759764
}
760765

@@ -768,7 +773,7 @@ public void testNioGWPropagatesSocketTimeout() throws Exception {
768773
ccf.setSoTimeout(100);
769774
ccf.setSingleUse(false);
770775
ccf.start();
771-
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
776+
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
772777
serverSocket.close();
773778
}
774779

@@ -782,7 +787,7 @@ public void testNetGWPropagatesSocketTimeoutSingleUse() throws Exception {
782787
ccf.setSoTimeout(100);
783788
ccf.setSingleUse(true);
784789
ccf.start();
785-
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
790+
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
786791
serverSocket.close();
787792
}
788793

@@ -796,12 +801,13 @@ public void testNioGWPropagatesSocketTimeoutSingleUse() throws Exception {
796801
ccf.setSoTimeout(100);
797802
ccf.setSingleUse(true);
798803
ccf.start();
799-
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
804+
testGWPropagatesSocketTimeoutGuts(ccf, serverSocket);
800805
serverSocket.close();
801806
}
802807

803-
private void testGWPropagatesSocketTimeoutGuts(final int port, AbstractClientConnectionFactory ccf,
808+
private void testGWPropagatesSocketTimeoutGuts(AbstractClientConnectionFactory ccf,
804809
final ServerSocket server) throws Exception {
810+
805811
final CountDownLatch latch = new CountDownLatch(1);
806812
final AtomicBoolean done = new AtomicBoolean();
807813

@@ -822,7 +828,7 @@ private void testGWPropagatesSocketTimeoutGuts(final int port, AbstractClientCon
822828
try {
823829
socket.close();
824830
}
825-
catch (IOException e2) {
831+
catch (@SuppressWarnings("unused") IOException e2) {
826832
}
827833
}
828834
});
@@ -879,7 +885,7 @@ public void testNioSecondChance() throws Exception {
879885
try {
880886
socket.close();
881887
}
882-
catch (IOException e2) {
888+
catch (@SuppressWarnings("unused") IOException e2) {
883889
}
884890
}
885891
});

0 commit comments

Comments
 (0)