logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1411917 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/net/ core/src/test/java/org/apache/logging/log4j/core/appender/ core/src/test/java/org/apache/logging/log4j/core/net/ core/src/test/resources/ src/c...
Date Tue, 20 Nov 2012 23:01:10 GMT
Author: rgoers
Date: Tue Nov 20 23:01:09 2012
New Revision: 1411917

URL: http://svn.apache.org/viewvc?rev=1411917&view=rev
Log:
LOG4J2-122 - Add unit test to verify exceptions are thrown when the socket connection fails.

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketMessageLossTest.java
    logging/log4j/log4j2/trunk/core/src/test/resources/log4j-socket2.xml
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/SocketServer.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SyslogAppenderTest.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/SocketServer.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/SocketServer.java?rev=1411917&r1=1411916&r2=1411917&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/SocketServer.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/SocketServer.java
Tue Nov 20 23:01:09 2012
@@ -125,6 +125,7 @@ public class SocketServer extends Abstra
             try {
                 // Accept incoming connections.
                 Socket clientSocket = server.accept();
+                clientSocket.setSoLinger(true, 0);
 
                 // accept() will block until a client connects to the server.
                 // If execution reaches this point, then it means that a client

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SyslogAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SyslogAppenderTest.java?rev=1411917&r1=1411916&r2=1411917&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SyslogAppenderTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SyslogAppenderTest.java
Tue Nov 20 23:01:09 2012
@@ -267,10 +267,11 @@ public class SyslogAppenderTest {
         @Override
         public void run() {
             this.thread = Thread.currentThread();
-            try {
-                byte[] buffer = new byte[4096];
-                while (!shutdown) {
+            while (!shutdown) {
+                try {
+                    byte[] buffer = new byte[4096];
                     Socket socket = sock.accept();
+                    socket.setSoLinger(true, 0);
                     StringBuilder sb = new StringBuilder();
                     if (socket != null) {
                         InputStream in = socket.getInputStream();
@@ -290,12 +291,13 @@ public class SyslogAppenderTest {
 
                         socket.close();
                     }
-                }
-            } catch (Exception ex) {
-                if (!shutdown) {
-                    throw new RuntimeException(ex);
+                } catch (Exception ex) {
+                    if (!shutdown) {
+                        System.out.println("Caught exception: " + ex.getMessage());
+                    }
                 }
             }
+
         }
     }
 

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketMessageLossTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketMessageLossTest.java?rev=1411917&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketMessageLossTest.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketMessageLossTest.java
Tue Nov 20 23:01:09 2012
@@ -0,0 +1,133 @@
+package org.apache.logging.log4j.core.net;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.appender.AppenderRuntimeException;
+import org.apache.logging.log4j.core.config.XMLConfigurationFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class SocketMessageLossTest {
+    private static final int SOCKET_PORT = 5514;
+
+    private static final String CONFIG = "log4j-socket2.xml";
+
+    @BeforeClass
+    public static void before() {
+        System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
+    }
+
+    @Test
+    public void testSocket() throws Exception {
+        TestSocketServer testServer;
+        ExecutorService executor = null;
+        Future<InputStream> futureIn;
+
+        try {
+            executor = Executors.newSingleThreadExecutor();
+            System.err.println("Initializing server");
+            testServer = new TestSocketServer();
+            futureIn = executor.submit(testServer);
+
+            //System.err.println("Initializing logger");
+            Logger logger = LogManager.getLogger(SocketMessageLossTest.class);
+
+            String message = "Log #1";
+            logger.error(message);
+
+            BufferedReader reader = new BufferedReader(new InputStreamReader(futureIn.get()));
+            assertEquals(message, reader.readLine());
+
+            //System.err.println("Closing server");
+            closeQuietly(testServer);
+            assertTrue("Server not shutdown", testServer.server.isClosed());
+
+            //System.err.println("Sleeping to ensure no race conditions");
+            Thread.sleep(1000);
+
+            message = "Log #2";
+            try {
+                logger.error(message);
+                fail("Expected exception not thrown");
+            } catch (AppenderRuntimeException e) {
+                // An exception is expected.
+            }
+
+            message = "Log #3";
+            try {
+                logger.error(message);
+                fail("Expected exception not thrown");
+            } catch (AppenderRuntimeException e) {
+                // An exception is expected.
+            }
+        } finally {
+            closeQuietly(executor);
+        }
+    }
+
+
+    private static class TestSocketServer implements Callable<InputStream> {
+        private ServerSocket server;
+        private Socket client;
+
+        public TestSocketServer() throws Exception {
+            server = new ServerSocket(SOCKET_PORT);
+        }
+
+        public InputStream call() throws Exception {
+            client = server.accept();
+            return client.getInputStream();
+        }
+
+        public void close() {
+            closeQuietly(client);
+            closeQuietly(server);
+        }
+
+        private void closeQuietly(ServerSocket socket) {
+            if (null != socket) {
+                try {
+                    socket.close();
+                } catch (IOException ignore) {
+                }
+            }
+        }
+
+        private void closeQuietly(Socket socket) {
+            if (null != socket) {
+                try {
+                    socket.setSoLinger(true, 0);
+                    socket.close();
+                } catch (IOException ignore) {
+                }
+            }
+        }
+    }
+
+    private static void closeQuietly(ExecutorService executor) {
+        if (null != executor) {
+            executor.shutdownNow();
+        }
+    }
+
+    private static void closeQuietly(TestSocketServer testServer) {
+        if (null != testServer) {
+            testServer.close();
+        }
+    }
+}

Added: logging/log4j/log4j2/trunk/core/src/test/resources/log4j-socket2.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/log4j-socket2.xml?rev=1411917&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/resources/log4j-socket2.xml (added)
+++ logging/log4j/log4j2/trunk/core/src/test/resources/log4j-socket2.xml Tue Nov 20 23:01:09
2012
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration status="debug" name="MyApp">
+  <appenders>
+    <Socket name="socket" host="localhost" port="5514" protocol="TCP" suppressExceptions="false"
+        reconnectionDelay="0">
+      <PatternLayout pattern="%msg%n"/>
+    </Socket>
+  </appenders>
+  <loggers>
+    <root level="debug">
+      <appender-ref ref="socket"/>
+    </root>
+  </loggers>
+</configuration>
\ No newline at end of file

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1411917&r1=1411916&r2=1411917&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Tue Nov 20 23:01:09 2012
@@ -23,6 +23,12 @@
 
   <body>
     <release version="2.0-beta4" date="TBD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-122" dev="rgoers" type="update">
+        Add unit test to verify exceptions are thrown when the socket connection fails.
+      </action>
+      <action issue="LOG4J2-125" dev="rgoers" type="fix">
+        JMSQueue and JMSTopic Appenders did not allow name to be specified.
+      </action>
       <action issue="LOG4J2-111" dev="rgoers" type="fix">
         Enhanced javadoc copyright statement.
       </action>



Mime
View raw message