logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1480747 - in /logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net: AbstractSocketServerTest.java SocketServerTest.java TCPSocketServerTest.java UDPSocketServer.java UDPSocketServerTest.java
Date Thu, 09 May 2013 20:00:35 GMT
Author: ggregory
Date: Thu May  9 20:00:35 2013
New Revision: 1480747

URL: http://svn.apache.org/r1480747
Log:
Testing LOG4J2-246: UDP socket Appender error. Needing help with UDPSocketServerTest, it is
@Ignored for now.

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/AbstractSocketServerTest.java
      - copied, changed from r1480720, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketServerTest.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/TCPSocketServerTest.java
  (with props)
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServer.java
  (with props)
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServerTest.java
  (with props)
Removed:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketServerTest.java

Copied: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/AbstractSocketServerTest.java
(from r1480720, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketServerTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/AbstractSocketServerTest.java?p2=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/AbstractSocketServerTest.java&p1=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketServerTest.java&r1=1480720&r2=1480747&rev=1480747&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketServerTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/AbstractSocketServerTest.java
Thu May  9 20:00:35 2013
@@ -29,10 +29,10 @@ import org.apache.logging.log4j.core.fil
 import org.apache.logging.log4j.core.layout.PatternLayout;
 import org.apache.logging.log4j.test.appender.ListAppender;
 import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
+import java.io.Serializable;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
@@ -42,41 +42,38 @@ import static org.junit.Assert.assertTru
 /**
  *
  */
-public class SocketServerTest {
+public abstract class AbstractSocketServerTest {
 
-    private static final String MESSAGE_1 = "This is a test message";
-    private static final String MESSAGE_2 = "This is test message 2";
-    private static final String PORT = "8199";
-    private static final int PORTNUM = Integer.parseInt(PORT);
+    private class ThreadFilter extends AbstractFilter {
 
-    private static SocketServer tcp;
-    private static Thread thread;
+        public ThreadFilter(final Result onMatch, final Result onMismatch) {
+            super(onMatch, onMismatch);
+        }
 
-    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
-    Logger root = ctx.getLogger("SocketServerTest");
+        @Override
+        public Filter.Result filter(final LogEvent event) {
+            return event.getThreadName().equals(Thread.currentThread().getName()) ? onMatch
: onMismatch;
+        }
+    }
 
-    @BeforeClass
-    public static void setupClass() throws Exception {
-        ((LoggerContext) LogManager.getContext(false)).reconfigure();
-        tcp = new SocketServer(PORTNUM);
-        thread = new Thread(tcp);
-        thread.start();
+    private static final String MESSAGE_1 = "This is a test message";
+    private static final String MESSAGE_2 = "This is test message 2";
 
-    }
+    private final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
 
-    @AfterClass
-    public static void cleanupClass() {
-        tcp.shutdown();
-        try {
-            thread.join();
-        } catch (final InterruptedException iex) {
+    private final String port;
+    private final String protocol;
 
-        }
+    private final Logger root = ctx.getLogger(AbstractSocketServerTest.class.getSimpleName());
+
+    protected AbstractSocketServerTest(final String protocol, final String port) {
+        this.protocol = protocol;
+        this.port = port;
     }
 
     @After
-    public void teardown() {
-        final Map<String,Appender<?>> map = root.getAppenders();
+    public void tearDown() {
+        final Map<String, Appender<?>> map = root.getAppenders();
         for (final Map.Entry<String, Appender<?>> entry : map.entrySet()) {
             final Appender<?> app = entry.getValue();
             root.removeAppender(app);
@@ -85,16 +82,24 @@ public class SocketServerTest {
     }
 
     @Test
-    public void testServer() throws Exception {
+    public void test64KMessages() throws Exception {
+        final char[] a64K = new char[1024 * 64];
+        Arrays.fill(a64K, 'a');
+        final String m1 = new String(a64K);
+        final String m2 = MESSAGE_2 + m1;
+        testServer(m1, m2);
+    }
+
+    protected void testServer(final String message1, final String message2) throws Exception
{
         final Filter socketFilter = new ThreadFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
         final Filter serverFilter = new ThreadFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
-        final SocketAppender appender = SocketAppender.createAppender("localhost", PORT,
"tcp", "-1",
-            null, "Test", null, null, null, socketFilter, null, null);
+        final SocketAppender<Serializable> appender = SocketAppender.createAppender("localhost",
this.port, this.protocol, "-1", null, "Test", null,
+                null, null, socketFilter, null, null);
         appender.start();
         final ListAppender<LogEvent> listApp = new ListAppender<LogEvent>("Events",
serverFilter, null, false, false);
         listApp.start();
         final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null);
-        final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT",
"Console", "false", "true");
+        final ConsoleAppender<String> console = ConsoleAppender.createAppender(layout,
null, "SYSTEM_OUT", "Console", "false", "true");
         final Logger serverLogger = ctx.getLogger(SocketServer.class.getName());
         serverLogger.addAppender(console);
         serverLogger.setAdditive(false);
@@ -104,27 +109,20 @@ public class SocketServerTest {
         root.addAppender(listApp);
         root.setAdditive(false);
         root.setLevel(Level.DEBUG);
-        root.debug(MESSAGE_1);
-        root.debug(MESSAGE_2);
+        root.debug(message1);
+        root.debug(message2);
         Thread.sleep(100);
         final List<LogEvent> events = listApp.getEvents();
         assertNotNull("No event retrieved", events);
         assertTrue("No events retrieved", events.size() > 0);
-        assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals(MESSAGE_1));
-        assertTrue("Incorrect number of events received", events.size() == 2);
-        assertTrue("Incorrect event", events.get(1).getMessage().getFormattedMessage().equals(MESSAGE_2));
+        assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals(message1));
+        assertTrue("Incorrect number of events received: " + events.size(), events.size()
== 2);
+        assertTrue("Incorrect event", events.get(1).getMessage().getFormattedMessage().equals(message2));
     }
 
-    private class ThreadFilter extends AbstractFilter {
-
-        public ThreadFilter(final Result onMatch, final Result onMismatch) {
-            super(onMatch, onMismatch);
-        }
-
-        @Override
-        public Filter.Result filter(final LogEvent event) {
-            return event.getThreadName().equals(Thread.currentThread().getName()) ? onMatch
: onMismatch;
-        }
+    @Test
+    public void testShortMessages() throws Exception {
+        testServer(MESSAGE_1, MESSAGE_2);
     }
 
 }

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/TCPSocketServerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/TCPSocketServerTest.java?rev=1480747&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/TCPSocketServerTest.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/TCPSocketServerTest.java
Thu May  9 20:00:35 2013
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.net;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
+public class TCPSocketServerTest extends AbstractSocketServerTest {
+    private static final String PORT = "8198";
+    private static final int PORT_NUM = Integer.parseInt(PORT);
+    private static SocketServer udpSocketServer;
+
+    private static Thread thread;
+
+    @BeforeClass
+    public static void setupClass() throws Exception {
+        ((LoggerContext) LogManager.getContext(false)).reconfigure();
+        udpSocketServer = new SocketServer(PORT_NUM);
+        thread = new Thread(udpSocketServer);
+        thread.start();
+    }
+
+    @AfterClass
+    public static void tearDownClass() {
+        udpSocketServer.shutdown();
+        try {
+            thread.join();
+        } catch (final InterruptedException e) {
+            // ignore
+        }
+    }
+
+    public TCPSocketServerTest() {
+        super("tcp", PORT);
+    }
+
+}

Propchange: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/TCPSocketServerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/TCPSocketServerTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServer.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServer.java?rev=1480747&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServer.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServer.java
Thu May  9 20:00:35 2013
@@ -0,0 +1,247 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.net;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.EOFException;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.ObjectInputStream;
+import java.io.OptionalDataException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.AbstractServer;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.config.XMLConfiguration;
+import org.apache.logging.log4j.core.config.XMLConfigurationFactory;
+
+/**
+ * Listens for events over a socket connection.
+ */
+public class UDPSocketServer extends AbstractServer implements Runnable {
+
+    private static Logger logger;
+
+    private static final int MAX_PORT = 65534;
+
+    private volatile boolean isActive = true;
+
+    private final DatagramSocket server;
+
+    private final ConcurrentMap<Long, DatagramPacketHandler> handlers = new ConcurrentHashMap<Long,
DatagramPacketHandler>();
+
+    // max size so we only have to deal with one packet
+    private int maxBufferSize = 1024 * 65;
+
+    /**
+     * Constructor.
+     * 
+     * @param port
+     *            to listen on.
+     * @throws IOException
+     *             If an error occurs.
+     */
+    public UDPSocketServer(final int port) throws IOException {
+        server = new DatagramSocket(port);
+        if (logger == null) {
+            logger = LogManager.getLogger(this);
+            // logger = LogManager.getLogger(getClass().getName() + '.' + port);
+        }
+    }
+
+    /**
+     * Main startup for the server.
+     * 
+     * @param args
+     *            The command line arguments.
+     * @throws Exception
+     *             if an error occurs.
+     */
+    public static void main(final String[] args) throws Exception {
+        if (args.length < 1 || args.length > 2) {
+            System.err.println("Incorrect number of arguments");
+            printUsage();
+            return;
+        }
+        final int port = Integer.parseInt(args[0]);
+        if (port <= 0 || port >= MAX_PORT) {
+            System.err.println("Invalid port number");
+            printUsage();
+            return;
+        }
+        if (args.length == 2 && args[1].length() > 0) {
+            ConfigurationFactory.setConfigurationFactory(new ServerConfigurationFactory(args[1]));
+        }
+        logger = LogManager.getLogger(UDPSocketServer.class.getName());
+        final UDPSocketServer sserver = new UDPSocketServer(port);
+        final Thread server = new Thread(sserver);
+        server.start();
+        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+        while (true) {
+            final String line = reader.readLine();
+            if (line.equalsIgnoreCase("Quit") || line.equalsIgnoreCase("Stop") || line.equalsIgnoreCase("Exit"))
{
+                sserver.shutdown();
+                server.join();
+                break;
+            }
+        }
+    }
+
+    private static void printUsage() {
+        System.out.println("Usage: ServerSocket port configFilePath");
+    }
+
+    /**
+     * Shutdown the server.
+     */
+    public void shutdown() {
+        this.isActive = false;
+        Thread.currentThread().interrupt();
+    }
+
+    /**
+     * Accept incoming events and processes them.
+     */
+    public void run() {
+        while (isActive) {
+            try {
+                byte[] buf = new byte[maxBufferSize];
+                DatagramPacket packet = new DatagramPacket(buf, buf.length);
+                server.receive(packet);
+                final DatagramPacketHandler handler = new DatagramPacketHandler(packet);
+                handlers.put(handler.getId(), handler);
+                handler.start();
+            } catch (final IOException ioe) {
+                System.out.println("Exception encountered on accept. Ignoring. Stack Trace
:");
+                ioe.printStackTrace();
+            }
+        }
+        for (final Map.Entry<Long, DatagramPacketHandler> entry : handlers.entrySet())
{
+            final DatagramPacketHandler handler = entry.getValue();
+            handler.shutdown();
+            try {
+                handler.join();
+            } catch (final InterruptedException ie) {
+                // Ignore the exception
+            }
+        }
+    }
+
+    /**
+     * Thread that processes the events.
+     */
+    private class DatagramPacketHandler extends Thread {
+        private final ObjectInputStream ois;
+        private boolean shutdown = false;
+
+        public DatagramPacketHandler(final DatagramPacket packet) throws IOException {
+            this.ois = new ObjectInputStream(new ByteArrayInputStream(packet.getData(), packet.getOffset(),
packet.getLength()));
+        }
+
+        public void shutdown() {
+            this.shutdown = true;
+            interrupt();
+        }
+
+        @Override
+        public void run() {
+            boolean closed = false;
+            try {
+                try {
+                    while (!shutdown) {
+                        final LogEvent event = (LogEvent) ois.readObject();
+                        if (event != null) {
+                            log(event);
+                        }
+                    }
+                } catch (final EOFException eof) {
+                    closed = true;
+                } catch (final OptionalDataException opt) {
+                    logger.error("OptionalDataException eof=" + opt.eof + " length=" + opt.length,
opt);
+                } catch (final ClassNotFoundException cnfe) {
+                    logger.error("Unable to locate LogEvent class", cnfe);
+                } catch (final IOException ioe) {
+                    logger.error("IOException encountered while reading from socket", ioe);
+                }
+            } finally {
+                handlers.remove(getId());
+            }
+        }
+    }
+
+    /**
+     * Factory that creates a Configuration for the server.
+     */
+    private static class ServerConfigurationFactory extends XMLConfigurationFactory {
+
+        private final String path;
+
+        public ServerConfigurationFactory(final String path) {
+            this.path = path;
+        }
+
+        @Override
+        public Configuration getConfiguration(final String name, final URI configLocation)
{
+            if (path != null && path.length() > 0) {
+                File file = null;
+                ConfigurationSource source = null;
+                try {
+                    file = new File(path);
+                    final FileInputStream is = new FileInputStream(file);
+                    source = new ConfigurationSource(is, file);
+                } catch (final FileNotFoundException ex) {
+                    // Ignore this error
+                }
+                if (source == null) {
+                    try {
+                        final URL url = new URL(path);
+                        source = new ConfigurationSource(url.openStream(), path);
+                    } catch (final MalformedURLException mue) {
+                        // Ignore this error
+                    } catch (final IOException ioe) {
+                        // Ignore this error
+                    }
+                }
+
+                try {
+                    if (source != null) {
+                        return new XMLConfiguration(source);
+                    }
+                } catch (final Exception ex) {
+                    // Ignore this error.
+                }
+                System.err.println("Unable to process configuration at " + path + ", using
default.");
+            }
+            return super.getConfiguration(name, configLocation);
+        }
+    }
+}

Propchange: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServer.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServerTest.java?rev=1480747&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServerTest.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServerTest.java
Thu May  9 20:00:35 2013
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.net;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+
+@Ignore
+public class UDPSocketServerTest extends AbstractSocketServerTest {
+    private static final String PORT = "8199";
+    private static final int PORT_NUM = Integer.parseInt(PORT);
+    private static UDPSocketServer udpSocketServer;
+
+    private static Thread thread;
+
+    @BeforeClass
+    public static void setupClass() throws Exception {
+        ((LoggerContext) LogManager.getContext(false)).reconfigure();
+        udpSocketServer = new UDPSocketServer(PORT_NUM);
+        thread = new Thread(udpSocketServer);
+        thread.start();
+    }
+
+    @AfterClass
+    public static void tearDownClass() {
+        udpSocketServer.shutdown();
+        try {
+            thread.join();
+        } catch (final InterruptedException e) {
+            // ignore
+        }
+    }
+
+    public UDPSocketServerTest() {
+        super("udp", PORT);
+    }
+
+}

Propchange: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/UDPSocketServerTest.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message