diff --git a/src/main/java/com/cloudbees/syslog/sender/TcpSyslogMessageSender.java b/src/main/java/com/cloudbees/syslog/sender/TcpSyslogMessageSender.java index 249556f..48b8be9 100644 --- a/src/main/java/com/cloudbees/syslog/sender/TcpSyslogMessageSender.java +++ b/src/main/java/com/cloudbees/syslog/sender/TcpSyslogMessageSender.java @@ -112,11 +112,11 @@ public synchronized void sendMessage(@NonNull SyslogMessage message) throws IOEx } if (lastException != null) { sendErrorCounter.incrementAndGet(); - if (lastException instanceof IOException) { - throw (IOException) lastException; - } else if (lastException instanceof RuntimeException) { - throw (RuntimeException) lastException; - } + } + if (lastException instanceof IOException) { + throw (IOException) lastException; + } else if (lastException instanceof RuntimeException) { + throw (RuntimeException) lastException; } } finally { sendDurationInNanosCounter.addAndGet(System.nanoTime() - nanosBefore); diff --git a/src/main/java/com/cloudbees/syslog/util/IoUtils.java b/src/main/java/com/cloudbees/syslog/util/IoUtils.java index ec983fe..6d38a10 100644 --- a/src/main/java/com/cloudbees/syslog/util/IoUtils.java +++ b/src/main/java/com/cloudbees/syslog/util/IoUtils.java @@ -24,6 +24,8 @@ * @author Cyrille Le Clerc */ public class IoUtils { + private static final InternalLogger logger = InternalLogger.getLogger(IoUtils.class); + private IoUtils() { } @@ -34,6 +36,7 @@ public static void closeQuietly(@Nullable Socket socket) { socket.close(); } } catch (Exception e) { + logger.warn("Exception while closing the socket "+socket, e); } } @@ -48,7 +51,7 @@ public static void closeQuietly(@Nullable Socket socket, @Nullable Writer writer try { writer.close(); } catch (IOException e) { - + logger.warn("Exception while closing writer for socket "+socket, e); } } closeQuietly(socket); diff --git a/src/test/java/com/cloudbees/syslog/util/IoUtilsTest.java b/src/test/java/com/cloudbees/syslog/util/IoUtilsTest.java new file mode 100644 index 0000000..96076c1 --- /dev/null +++ b/src/test/java/com/cloudbees/syslog/util/IoUtilsTest.java @@ -0,0 +1,53 @@ +package com.cloudbees.syslog.util; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import java.io.ByteArrayOutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.Socket; + +import org.junit.Test; + +public class IoUtilsTest { + + @Test + public void testCloseQuietlyWithSocket() { + Socket socket = new Socket(); + IoUtils.closeQuietly(socket); + assertThat(socket.isClosed(), is(true)); + + } + + @Test + public void testCloseQuietlyWithNullSocket() { + IoUtils.closeQuietly(null); + // No exception should be thrown for null socket + } + + @Test + public void testCloseQuietlyWithSocketAndWriter() { + Socket socket = new Socket(); + Writer writer = new OutputStreamWriter(new ByteArrayOutputStream()); + + IoUtils.closeQuietly(socket, writer); + assertThat(socket.isClosed(), is(true)); + // Since we don't mock the writer, we can't directly verify if it's closed + // However, we know that the method should execute without throwing exceptions + + } + + @Test + public void testCloseQuietlyWithNullSocketAndNullWriter() { + IoUtils.closeQuietly(null, null); + // No exception should be thrown for null socket and null writer + } + + @Test + public void testCloseQuietlyWithSocketAndNullWriter() { + Socket socket = new Socket(); + IoUtils.closeQuietly(socket, null); + assertThat(socket.isClosed(), is(true)); + + } +} \ No newline at end of file