Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Oct 15, 2023
1 parent 3b8a6ba commit 7ba9788
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 198 deletions.
76 changes: 40 additions & 36 deletions src/test/java/de/siegmar/logbackgelf/GelfTcpAppenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.Closeable;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

Expand All @@ -46,14 +46,15 @@ class GelfTcpAppenderTest {

private static final String LOGGER_NAME = GelfTcpAppenderTest.class.getCanonicalName();

private int port;
private Future<byte[]> future;
private final TcpServer server;

@BeforeEach
void before() throws IOException {
final TcpServer server = new TcpServer();
port = server.getPort();
future = Executors.newSingleThreadExecutor().submit(server);
GelfTcpAppenderTest() throws IOException {
server = new TcpServer();
}

@AfterEach
void after() throws IOException {
server.close();
}

@Test
Expand All @@ -72,14 +73,14 @@ void defaultValues() {
}

@Test
void simple() {
void simple() throws ExecutionException, InterruptedException, TimeoutException {
final Logger logger = setupLogger();

logger.error("Test message");

stopLogger(logger);

final String json = receiveMessage();
final String json = awaitMessage();
assertThatJson(json).and(
j -> j.node("version").isString().isEqualTo("1.1"),
j -> j.node("host").isEqualTo("localhost"),
Expand Down Expand Up @@ -112,55 +113,58 @@ private GelfTcpAppender buildAppender(final LoggerContext lc, final GelfEncoder
gelfAppender.setName("GELF");
gelfAppender.setEncoder(gelfEncoder);
gelfAppender.setGraylogHost("localhost");
gelfAppender.setGraylogPort(port);
gelfAppender.setGraylogPort(server.getPort());
gelfAppender.start();
return gelfAppender;
}

private String receiveMessage() {
return new String(receive(), StandardCharsets.UTF_8);
}

private void stopLogger(final Logger logger) {
final GelfTcpAppender gelfAppender = (GelfTcpAppender) logger.getAppender("GELF");
gelfAppender.stop();
}

private byte[] receive() {
try {
final byte[] bytes = future.get(5, TimeUnit.SECONDS);
if (bytes[bytes.length - 1] != 0) {
throw new IllegalStateException("Data stream is not terminated by 0");
}
return Arrays.copyOf(bytes, bytes.length - 1);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new IllegalStateException(e);
private String awaitMessage() throws ExecutionException, InterruptedException, TimeoutException {
final byte[] data = server.receiveMessage().get(5, TimeUnit.SECONDS);
if (data[data.length - 1] != 0) {
throw new IllegalStateException("Data stream is not terminated by 0");
}

return new String(data, 0, data.length - 1, StandardCharsets.UTF_8);
}

private static final class TcpServer implements Callable<byte[]> {
private static final class TcpServer implements Closeable {

private final ServerSocket server;
private final ServerSocket socket;
private final Future<byte[]> receivedMessage;

TcpServer() throws IOException {
server = new ServerSocket(0);
socket = new ServerSocket(0);

receivedMessage = Executors.newSingleThreadExecutor()
.submit(this::receive);
}

int getPort() {
return server.getLocalPort();
return socket.getLocalPort();
}

@Override
public byte[] call() throws Exception {
final byte[] ret;
Future<byte[]> receiveMessage() {
return receivedMessage;
}

try (server; Socket socket = server.accept()) {
private byte[] receive() {
try (Socket socket = this.socket.accept()) {
try (DataInputStream in = new DataInputStream(socket.getInputStream())) {
ret = in.readAllBytes();
return in.readAllBytes();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

return ret;
@Override
public void close() throws IOException {
socket.close();
}

}
Expand Down
86 changes: 41 additions & 45 deletions src/test/java/de/siegmar/logbackgelf/GelfTcpTlsAppenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.Closeable;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand All @@ -37,7 +36,6 @@
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

Expand All @@ -48,21 +46,10 @@ class GelfTcpTlsAppenderTest {

private static final String LOGGER_NAME = GelfTcpTlsAppenderTest.class.getCanonicalName();

private int port;
private Future<byte[]> future;
private final TlsServer server;

GelfTcpTlsAppenderTest() {
final String mySrvKeystore =
GelfTcpTlsAppenderTest.class.getResource("/mySrvKeystore").getFile();
System.setProperty("javax.net.ssl.keyStore", mySrvKeystore);
System.setProperty("javax.net.ssl.keyStorePassword", "secret");
}

@BeforeEach
void before() throws IOException {
final TcpServer server = new TcpServer();
port = server.getPort();
future = Executors.newSingleThreadExecutor().submit(server);
GelfTcpTlsAppenderTest() throws IOException {
server = new TlsServer();
}

@Test
Expand All @@ -72,14 +59,14 @@ void defaultValues() {
}

@Test
void simple() {
void simple() throws ExecutionException, InterruptedException, TimeoutException {
final Logger logger = setupLogger();

logger.error("Test message");

stopLogger(logger);

final String json = receiveMessage();
final String json = awaitMessage();
assertThatJson(json).and(
j -> j.node("version").isString().isEqualTo("1.1"),
j -> j.node("host").isEqualTo("localhost"),
Expand Down Expand Up @@ -112,58 +99,67 @@ private GelfTcpTlsAppender buildAppender(final LoggerContext lc, final GelfEncod
gelfAppender.setName("GELF");
gelfAppender.setEncoder(encoder);
gelfAppender.setGraylogHost("localhost");
gelfAppender.setGraylogPort(port);
gelfAppender.setGraylogPort(server.getPort());
gelfAppender.setInsecure(true);
gelfAppender.start();
return gelfAppender;
}

private String receiveMessage() {
return new String(receive(), StandardCharsets.UTF_8);
}

private void stopLogger(final Logger logger) {
final GelfTcpTlsAppender gelfAppender = (GelfTcpTlsAppender) logger.getAppender("GELF");
gelfAppender.stop();
}

private byte[] receive() {
try {
final byte[] bytes = future.get(5, TimeUnit.SECONDS);
if (bytes[bytes.length - 1] != 0) {
throw new IllegalStateException("Data stream is not terminated by 0");
}
return Arrays.copyOf(bytes, bytes.length - 1);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new IllegalStateException(e);
private String awaitMessage() throws ExecutionException, InterruptedException, TimeoutException {
final byte[] data = server.receiveMessage().get(5, TimeUnit.SECONDS);
if (data[data.length - 1] != 0) {
throw new IllegalStateException("Data stream is not terminated by 0");
}

return new String(data, 0, data.length - 1, StandardCharsets.UTF_8);
}

private static final class TcpServer implements Callable<byte[]> {
private static final class TlsServer implements Closeable {

private final SSLServerSocket server;
private final SSLServerSocket socket;
private final Future<byte[]> receivedMessage;

TcpServer() throws IOException {
final SSLServerSocketFactory sslserversocketfactory =
TlsServer() throws IOException {
socket = initSocket();

receivedMessage = Executors.newSingleThreadExecutor()
.submit(this::receive);
}

private SSLServerSocket initSocket() throws IOException {
System.setProperty("javax.net.ssl.keyStore", "src/test/resources/mySrvKeystore");
System.setProperty("javax.net.ssl.keyStorePassword", "secret");

final SSLServerSocketFactory socketFactory =
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
server = (SSLServerSocket) sslserversocketfactory.createServerSocket(0);

return (SSLServerSocket) socketFactory.createServerSocket(0);
}

int getPort() {
return server.getLocalPort();
return socket.getLocalPort();
}

@Override
public byte[] call() throws Exception {
final byte[] ret;
Future<byte[]> receiveMessage() {
return receivedMessage;
}

try (server; Socket socket = server.accept()) {
private byte[] receive() throws IOException {
try (Socket socket = this.socket.accept()) {
try (DataInputStream in = new DataInputStream(socket.getInputStream())) {
ret = in.readAllBytes();
return in.readAllBytes();
}
}
}

return ret;
@Override
public void close() throws IOException {
socket.close();
}

}
Expand Down
Loading

0 comments on commit 7ba9788

Please sign in to comment.