Permalink
Browse files

Added testing with redis and the TCP sender

  • Loading branch information...
1 parent e77f52f commit 466b84ff4f3891e635c374706c04dcda2e106b4b @mp911de committed Jul 16, 2014
View
@@ -1,6 +1,7 @@
.idea/
*.iml
target
+work/
pom.xml.releaseBackup
release.properties
atlassian-ide-plugin.xml
View
@@ -1,5 +1,6 @@
language: java
-script: mvn clean cobertura:cobertura coveralls:cobertura
+install: make travis-install
+script: make test-coveralls
jdk:
- oraclejdk8
- oraclejdk7
View
@@ -0,0 +1,53 @@
+PATH := ./work/redis-git/src:${PATH}
+
+define REDIS1_CONF
+daemonize yes
+port 6479
+pidfile work/redis1-6479.pid
+logfile work/redis1-6479.log
+save ""
+appendonly no
+client-output-buffer-limit pubsub 256k 128k 5
+endef
+
+export REDIS1_CONF
+
+start: cleanup
+ echo "$$REDIS1_CONF" > work/redis1-6479.conf && redis-server work/redis1-6479.conf
+
+cleanup: stop
+ - mkdir -p work
+ rm -f work/redis-cluster-node*.conf 2>/dev/null
+ rm -f work/dump.rdb work/appendonly.aof work/*.conf work/*.log 2>/dev/null
+
+stop:
+ pkill redis-server || true
+ pkill redis-sentinel || true
+ sleep 2
+ rm -f work/dump.rdb work/appendonly.aof work/*.conf work/*.log || true
+ rm -f *.aof
+ rm -f *.rdb
+
+
+test-coveralls:
+ make start
+ sleep 2
+ mvn -B -Dtest.withRedis=true clean compile cobertura:cobertura coveralls:cobertura
+ make stop
+
+test:
+ make start
+ sleep 2
+ mvn -B -Dtest.withRedis=true clean compile test
+ make stop
+
+travis-install:
+ pkill redis-server || true
+ pkill redis-sentinel || true
+ [ ! -e work/redis-git ] && git clone https://github.com/antirez/redis.git --branch 3.0 --single-branch work/redis-git && cd work/redis-git|| true
+ [ -e work/redis-git ] && cd work/redis-git && git reset --hard && git pull && git checkout 3.0 || true
+ make -C work/redis-git clean
+ make -C work/redis-git -j4
+
+.PHONY: test
+
View
@@ -51,6 +51,7 @@
<logstash-gelf-release-version>1.4.5</logstash-gelf-release-version>
<github.site.upload.skip>true</github.site.upload.skip>
+ <test.withRedis>false</test.withRedis>
<!-- Versions -->
<com.googlecode.json-simple.version>1.1.1</com.googlecode.json-simple.version>
@@ -64,6 +65,7 @@
<site-plugin.version>3.3</site-plugin.version>
<maven-javadoc-plugin.version>2.7</maven-javadoc-plugin.version>
+ <maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<doxia-module-markdown.version>1.3</doxia-module-markdown.version>
<maven-project-info-reports-plugin.version>2.7</maven-project-info-reports-plugin.version>
<cobertura-maven-plugin.version>2.6</cobertura-maven-plugin.version>
@@ -122,6 +124,15 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>${maven-surefire-plugin.version}</version>
+ <configuration>
+ <redirectTestOutputToFile>true</redirectTestOutputToFile>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>${maven-release-plugin.version}</version>
<configuration>
@@ -140,7 +151,7 @@
<artifactId>cobertura-maven-plugin</artifactId>
<version>${cobertura-maven-plugin.version}</version>
<configuration>
- <format>xml</format>
+ <format>html</format>
<maxmem>256m</maxmem>
<aggregate>true</aggregate>
</configuration>
@@ -176,6 +176,7 @@ private void setupStaticFields(PropertyProvider propertyProvider) {
fieldNumber++;
}
+
}
public void addField(MessageField field) {
@@ -29,6 +29,10 @@
String PROPERTY_FACILITY = "facility";
String PROPERTY_MAX_MESSAGE_SIZE = "maximumMessageSize";
String PROPERTY_ADDITIONAL_FIELD = "additionalField.";
+ String PROPERTY_ADDITIONAL_FIELDS = "additionalFields";
+
+ String PROPERTY_FILTER = "filter";
+ String PROPERTY_LEVEL = "level";
String getProperty(String propertyName);
}
@@ -1,13 +1,13 @@
package biz.paluch.logging.gelf.intern.sender;
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.Socket;
-
import biz.paluch.logging.gelf.intern.ErrorReporter;
import biz.paluch.logging.gelf.intern.GelfMessage;
import biz.paluch.logging.gelf.intern.GelfSender;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.Socket;
+
/**
* (c) https://github.com/t0xa/gelfj
*/
@@ -21,7 +21,6 @@
public GelfTCPSender(String host, int port, ErrorReporter errorReporter) throws IOException {
this.host = InetAddress.getByName(host);
this.port = port;
- this.socket = new Socket(host, port);
this.errorReporter = errorReporter;
}
@@ -2,6 +2,7 @@
import biz.paluch.logging.gelf.GelfMessageAssembler;
import biz.paluch.logging.gelf.LogMessageField;
+import biz.paluch.logging.gelf.PropertyProvider;
import biz.paluch.logging.gelf.StaticMessageField;
import biz.paluch.logging.gelf.intern.ErrorReporter;
import biz.paluch.logging.gelf.intern.GelfMessage;
@@ -51,14 +52,19 @@ public GelfLogHandler() {
JulPropertyProvider propertyProvider = new JulPropertyProvider(GelfLogHandler.class);
gelfMessageAssembler.initialize(propertyProvider);
- final String level = propertyProvider.getProperty("level");
+ final String level = propertyProvider.getProperty(PropertyProvider.PROPERTY_LEVEL);
if (null != level) {
setLevel(Level.parse(level.trim()));
} else {
setLevel(Level.INFO);
}
- final String filter = propertyProvider.getProperty("filter");
+ final String additionalFields = propertyProvider.getProperty(PropertyProvider.PROPERTY_ADDITIONAL_FIELDS);
+ if (null != level) {
+ setAdditionalFields(additionalFields);
+ }
+
+ final String filter = propertyProvider.getProperty(PropertyProvider.PROPERTY_FILTER);
try {
if (null != filter) {
final Class clazz = ClassLoader.getSystemClassLoader().loadClass(filter);
@@ -1,5 +1,12 @@
package biz.paluch.logging.gelf;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.channel.socket.DatagramPacket;
+import io.netty.util.ReferenceCountUtil;
+import org.json.simple.JSONValue;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -13,14 +20,6 @@
import java.util.Map;
import java.util.zip.GZIPInputStream;
-import org.json.simple.JSONValue;
-
-import io.netty.buffer.ByteBuf;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.channel.ChannelInboundHandlerAdapter;
-import io.netty.channel.socket.DatagramPacket;
-import io.netty.util.ReferenceCountUtil;
-
/**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
* @since 10.11.13 10:35
@@ -31,12 +30,22 @@
private Map<ChunkId, List<Chunk>> chunks = new HashMap<ChunkId, List<Chunk>>();
private List<Object> values = new ArrayList<Object>();
+ private ByteArrayOutputStream intermediate;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
- DatagramPacket packet = (DatagramPacket) msg;
- ByteBuf buffer = packet.content();
+
+ boolean requireNullEnd = false;
+ ByteBuf buffer = null;
+
+ if (msg instanceof DatagramPacket) {
+ DatagramPacket packet = (DatagramPacket) msg;
+ buffer = packet.content();
+ } else if (msg instanceof ByteBuf) {
+ buffer = (ByteBuf) msg;
+ requireNullEnd = true;
+ }
ByteArrayOutputStream temporaryBuffer = new ByteArrayOutputStream();
while (buffer.readableBytes() != 0) {
@@ -77,15 +86,30 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
return;
}
}
+ }
+ if (requireNullEnd) {
+ if (bytes[bytes.length - 1] != 0) {
+ intermediate = new ByteArrayOutputStream();
+ intermediate.write(bytes, 0, bytes.length);
+ return;
+ } else {
+ intermediate.write(bytes, 0, bytes.length);
+ bytes = intermediate.toByteArray();
+ intermediate = null;
+ }
}
InputStream is = null;
if (startsWith(bytes, GZIP_ID)) {
is = new GZIPInputStream(new ByteArrayInputStream(bytes));
} else {
- is = new ByteArrayInputStream(bytes);
+ if (bytes[bytes.length - 1] == 0) {
+ is = new ByteArrayInputStream(bytes, 0, bytes.length - 1);
+ } else {
+ is = new ByteArrayInputStream(bytes);
+ }
}
Object parse = JSONValue.parse(new InputStreamReader(is, "UTF-8"));
@@ -1,13 +1,16 @@
package biz.paluch.logging.gelf;
-import java.util.List;
-
import io.netty.bootstrap.Bootstrap;
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.AdaptiveRecvByteBufAllocator;
+import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
+import io.netty.channel.ServerChannel;
import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.nio.NioDatagramChannel;
+
+import java.util.List;
/**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
@@ -18,19 +21,33 @@
private int port = 19392;
private EventLoopGroup group = new NioEventLoopGroup();
private GelfInboundHandler handler = new GelfInboundHandler();
+ private Class<? extends Channel> channelClass;
private ChannelFuture f;
- public NettyLocalServer() {
+ public NettyLocalServer(Class<? extends Channel> channelClass) {
+ this.channelClass = channelClass;
}
public void run() throws Exception {
- Bootstrap b = new Bootstrap();
- b.group(group);
- b.channel(NioDatagramChannel.class).handler(handler).option(ChannelOption.SO_BROADCAST, true);
+ if (ServerChannel.class.isAssignableFrom(channelClass)) {
+ ServerBootstrap b = new ServerBootstrap();
+ b.group(group);
+ b.channel((Class) channelClass)
+ .childHandler(handler)
+ .childOption(ChannelOption.RCVBUF_ALLOCATOR,
+ new AdaptiveRecvByteBufAllocator(8192, 8192, Integer.MAX_VALUE));
+
+ // Bind and start to accept incoming connections.
+ f = b.bind(port).sync();
+ } else {
+ Bootstrap b = new Bootstrap();
+ b.group(group);
+ b.channel((Class) channelClass).handler(handler);
- // Bind and start to accept incoming connections.
- f = b.bind(port).sync();
+ // Bind and start to accept incoming connections.
+ f = b.bind(port).sync();
+ }
}
@@ -2,18 +2,18 @@
import static biz.paluch.logging.gelf.jboss7.JBoss7LogTestUtil.getJBoss7GelfLogHandler;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
-
-import java.util.logging.Level;
-import java.util.logging.LogManager;
-import java.util.logging.Logger;
-
+import biz.paluch.logging.gelf.GelfTestSender;
+import biz.paluch.logging.gelf.intern.GelfMessage;
import org.apache.log4j.MDC;
import org.junit.Before;
import org.junit.Test;
-import biz.paluch.logging.gelf.GelfTestSender;
-import biz.paluch.logging.gelf.intern.GelfMessage;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
/**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
@@ -54,6 +54,21 @@ public void testSimple() throws Exception {
}
@Test
+ public void testEmptyMessage() throws Exception {
+
+ JBoss7GelfLogHandler handler = getJBoss7GelfLogHandler();
+
+ Logger logger = Logger.getLogger(getClass().getName());
+ logger.addHandler(handler);
+
+ logger.info("");
+ assertEquals(1, GelfTestSender.getMessages().size());
+ GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);
+ assertFalse(gelfMessage.isValid());
+
+ }
+
+ @Test
public void testSimpleWithMsgFormatSubstitution() throws Exception {
JBoss7GelfLogHandler handler = getJBoss7GelfLogHandler();
@@ -118,4 +133,13 @@ public void testFields() throws Exception {
}
+ @Test
+ public void testWrongConfig() throws Exception {
+ JBoss7GelfLogHandler handler = new JBoss7GelfLogHandler();
+
+ handler.setGraylogHost(null);
+ handler.setGraylogPort(0);
+ handler.createGelfMessage(new LogRecord(Level.ALL, LOG_MESSAGE));
+
+ }
}
Oops, something went wrong.

0 comments on commit 466b84f

Please sign in to comment.