Skip to content

Commit 652c832

Browse files
committed
Use OS-selected port in failed TCP test
https://build.spring.io/browse/INT-AT42SIO-456/
1 parent 823885b commit 652c832

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/CachingClientConnectionFactoryTests-context.xml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
88
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
99

10-
<bean id="tcpIpUtils" class="org.springframework.integration.test.util.SocketUtils" />
11-
1210
<int-ip:tcp-connection-factory
13-
id="scf"
14-
type="server"
15-
so-timeout="60000"
16-
port="#{tcpIpUtils.findAvailableServerSocket()}"/>
11+
id="scf"
12+
type="server"
13+
so-timeout="60000"
14+
port="0" />
1715

1816
<int-ip:tcp-inbound-channel-adapter
1917
connection-factory="scf"
@@ -29,14 +27,14 @@
2927

3028
<int:channel id="replies" />
3129

30+
<!-- Since we use OS-assigning port in the server, this definition is for example.
31+
The real port is assigned to client in test code via reflection. -->
3232
<int-ip:tcp-connection-factory
33-
id="ccf"
34-
type="client"
35-
host="localhost"
36-
port="#{scf.port}"
37-
so-timeout="60000"
38-
39-
/>
33+
id="ccf"
34+
type="client"
35+
host="localhost"
36+
port="#{scf.port}"
37+
so-timeout="60000" />
4038

4139
<bean id="caching.ccf" class="org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactory">
4240
<constructor-arg ref="ccf" />
@@ -50,6 +48,8 @@
5048

5149
<int:channel id="outbound" />
5250

51+
<!-- Since we use OS-assigning port in the server, this definition is for example.
52+
The real port is assigned to client in test code via reflection. -->
5353
<int-ip:tcp-connection-factory
5454
id="gateway.ccf"
5555
type="client"

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,15 @@ public class CachingClientConnectionFactoryTests {
117117

118118
@Autowired
119119
@Qualifier("gateway.caching.ccf")
120-
CachingClientConnectionFactory gatewayCF;
120+
private CachingClientConnectionFactory gatewayCF;
121+
122+
@Autowired
123+
@Qualifier("gateway.ccf")
124+
private AbstractClientConnectionFactory clientGatewayCf;
125+
126+
@Autowired
127+
@Qualifier("ccf")
128+
private AbstractClientConnectionFactory clientAdapterCf;
121129

122130
@Test
123131
public void testReuse() throws Exception {
@@ -446,7 +454,9 @@ private TcpConnectionSupport makeMockConnection(String name, boolean closeOk) {
446454
@Test
447455
public void integrationTest() throws Exception {
448456
TestingUtilities.waitListening(serverCf, null);
449-
outbound.send(new GenericMessage<String>("Hello, world!"));
457+
new DirectFieldAccessor(this.clientAdapterCf).setPropertyValue("port", this.serverCf.getPort());
458+
459+
this.outbound.send(new GenericMessage<>("Hello, world!"));
450460
Message<?> m = inbound.receive(10000);
451461
assertNotNull(m);
452462
String connectionId = m.getHeaders().get(IpHeaders.CONNECTION_ID, String.class);
@@ -480,7 +490,9 @@ public void run() {
480490

481491
});
482492
TestingUtilities.waitListening(serverCf, null);
483-
toGateway.send(new GenericMessage<String>("Hello, world!"));
493+
new DirectFieldAccessor(this.clientGatewayCf).setPropertyValue("port", this.serverCf.getPort());
494+
495+
this.toGateway.send(new GenericMessage<>("Hello, world!"));
484496
Message<?> m = fromGateway.receive(1000);
485497
assertNotNull(m);
486498
assertEquals("foo:" + "Hello, world!", new String((byte[]) m.getPayload()));
@@ -507,18 +519,17 @@ public void run() {
507519

508520
@Test
509521
public void testCloseOnTimeoutNet() throws Exception {
510-
TcpNetClientConnectionFactory cf = new TcpNetClientConnectionFactory("localhost", serverCf.getPort());
511-
testCloseOnTimeoutGuts(cf);
522+
TestingUtilities.waitListening(serverCf, null);
523+
testCloseOnTimeoutGuts(new TcpNetClientConnectionFactory("localhost", serverCf.getPort()));
512524
}
513525

514526
@Test
515527
public void testCloseOnTimeoutNio() throws Exception {
516-
TcpNioClientConnectionFactory cf = new TcpNioClientConnectionFactory("localhost", serverCf.getPort());
517-
testCloseOnTimeoutGuts(cf);
528+
TestingUtilities.waitListening(serverCf, null);
529+
testCloseOnTimeoutGuts(new TcpNioClientConnectionFactory("localhost", serverCf.getPort()));
518530
}
519531

520532
private void testCloseOnTimeoutGuts(AbstractClientConnectionFactory cf) throws Exception {
521-
TestingUtilities.waitListening(serverCf, null);
522533
cf.setSoTimeout(100);
523534
CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(cf, 1);
524535
cccf.start();

0 commit comments

Comments
 (0)