Skip to content

Adding logger statements if we get exception while closing socket connection #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/cloudbees/syslog/util/IoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
*/
public class IoUtils {
private static final InternalLogger logger = InternalLogger.getLogger(IoUtils.class);

private IoUtils() {

}
Expand All @@ -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);
}
}

Expand All @@ -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);
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/com/cloudbees/syslog/util/IoUtilsTest.java
Original file line number Diff line number Diff line change
@@ -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));

}
}
Loading