From c5355a34171019c01b823f053646b1e9087a2ee6 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 19 Aug 2013 00:35:23 -0400 Subject: [PATCH] Fix Syslog Test Test didn't wait for the TCP server to start listening. --- ...logReceivingChannelAdapterParserTests.java | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/spring-integration-syslog/src/test/java/org/springframework/integration/syslog/config/SyslogReceivingChannelAdapterParserTests.java b/spring-integration-syslog/src/test/java/org/springframework/integration/syslog/config/SyslogReceivingChannelAdapterParserTests.java index 4ee0bf08c6..3442ec8421 100644 --- a/spring-integration-syslog/src/test/java/org/springframework/integration/syslog/config/SyslogReceivingChannelAdapterParserTests.java +++ b/spring-integration-syslog/src/test/java/org/springframework/integration/syslog/config/SyslogReceivingChannelAdapterParserTests.java @@ -39,7 +39,6 @@ import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.Message; import org.springframework.integration.core.PollableChannel; -import org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory; import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; import org.springframework.integration.syslog.MessageConverter; import org.springframework.integration.syslog.inbound.TcpSyslogReceivingChannelAdapter; @@ -131,7 +130,10 @@ public class SyslogReceivingChannelAdapterParserTests { @Test public void testSimplestTcp() throws Exception { - int port = TestUtils.getPropertyValue(adapter2, "connectionFactory", AbstractConnectionFactory.class).getPort(); + AbstractServerConnectionFactory connectionFactory = TestUtils.getPropertyValue(adapter2, "connectionFactory", + AbstractServerConnectionFactory.class); + int port = connectionFactory.getPort(); + waitListening(connectionFactory, 10000L); byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE\n".getBytes("UTF-8"); Socket socket = SocketFactory.getDefault().createSocket("localhost", port); Thread.sleep(1000); @@ -209,4 +211,36 @@ public class SyslogReceivingChannelAdapterParserTests { } } + + /** + * Wait for a server connection factory to actually start listening before + * starting a test. Waits for up to 10 seconds by default. + * @param serverConnectionFactory The server connection factory. + * @param delay How long to wait in milliseconds; default 10000 (10 seconds) if null. + * @throws IllegalStateException + */ + private void waitListening(AbstractServerConnectionFactory serverConnectionFactory, Long delay) + throws IllegalStateException { + if (delay == null) { + delay = 100L; + } + else { + delay = delay / 100; + } + int n = 0; + while (!serverConnectionFactory.isListening()) { + try { + Thread.sleep(100); + } + catch (InterruptedException e1) { + Thread.currentThread().interrupt(); + throw new IllegalStateException(e1); + } + + if (n++ > delay) { + throw new IllegalStateException("Server didn't start listening."); + } + } + } + }