INT-3310 Fix Syslog Error on Socket Close
https://jira.springsource.org/browse/INT-3310 Previously, the syslog adapter tried to convert the ErrorMessage that results from a socket error to a syslog packet, resulting in a stack trace on stderr. Test for the ErrorMessage and log at DEBUG.
This commit is contained in:
committed by
Artem Bilan
parent
233aaffb3b
commit
3761f2cf80
@@ -19,8 +19,11 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
@@ -31,15 +34,18 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean;
|
||||
import org.springframework.integration.test.util.SocketUtils;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
|
||||
@@ -95,11 +101,27 @@ public class SyslogReceivingChannelAdapterTests {
|
||||
factory.afterPropertiesSet();
|
||||
factory.start();
|
||||
TcpSyslogReceivingChannelAdapter adapter = (TcpSyslogReceivingChannelAdapter) factory.getObject();
|
||||
Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
|
||||
doReturn(true).when(logger).isDebugEnabled();
|
||||
final CountDownLatch sawLog = new CountDownLatch(1);
|
||||
doAnswer(new Answer<Void>(){
|
||||
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
if (((String) invocation.getArguments()[0]).contains("Error on syslog socket")) {
|
||||
sawLog.countDown();
|
||||
}
|
||||
invocation.callRealMethod();
|
||||
return null;
|
||||
}
|
||||
}).when(logger).debug(anyString());
|
||||
new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
|
||||
Thread.sleep(1000);
|
||||
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);
|
||||
socket.getOutputStream().write(buf);
|
||||
socket.close();
|
||||
assertTrue(sawLog.await(10, TimeUnit.SECONDS));
|
||||
Message<?> message = outputChannel.receive(10000);
|
||||
assertNotNull(message);
|
||||
assertEquals("WEBERN", message.getHeaders().get("syslog_HOST"));
|
||||
|
||||
Reference in New Issue
Block a user