From fc1458400d91868b99b286cebf5938f0eb22a7e6 Mon Sep 17 00:00:00 2001 From: arjun12102019 Date: Thu, 2 May 2024 11:27:13 +0530 Subject: [PATCH 1/2] Changes related to issue 137 --- .../syslog/sender/TcpSyslogMessageSender.java | 10 +++++----- src/main/java/com/cloudbees/syslog/util/IoUtils.java | 5 ++++- 2 files changed, 9 insertions(+), 6 deletions(-) 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); From f6bd987081adb47ee1dc346d59ed6411c6a0cf28 Mon Sep 17 00:00:00 2001 From: arjun12102019 Date: Thu, 2 May 2024 11:54:54 +0530 Subject: [PATCH 2/2] Junits for IoUtil changes --- .../cloudbees/syslog/util/IoUtilsTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/test/java/com/cloudbees/syslog/util/IoUtilsTest.java 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