Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Allow TCP and UDP sender creation for unresolvable hostnames #252
Browse files Browse the repository at this point in the history
We now allow sender creation even if the hostname cannot be resolved during application startup in anticipation that things resolve eventually.
  • Loading branch information
mp911de committed Jan 21, 2022
1 parent de96016 commit ae4bb85
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ protected ByteBuffer initialValue() {
protected AbstractNioSender(ErrorReporter errorReporter, String host, int port) throws UnknownHostException {

// validate first address succeeds.
InetAddress.getByName(host);
try {
InetAddress.getByName(host);
} catch (UnknownHostException e) {
errorReporter.reportError("Cannot resolve " + host, e);
}
this.errorReporter = errorReporter;
this.host = host;
this.port = port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.UnresolvedAddressException;

import biz.paluch.logging.gelf.intern.Closer;
import biz.paluch.logging.gelf.intern.ErrorReporter;
Expand Down Expand Up @@ -93,7 +93,7 @@ protected void connect() throws IOException {
try {
DatagramChannel connect = channel().connect(inetSocketAddress);
setChannel(connect);
} catch (SocketException e) {
} catch (UnresolvedAddressException | IOException e) {
reportError(e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import biz.paluch.logging.gelf.GelfMessageAssembler;

/**
* Unit tests for {@link GelfSenderFactory}.
*
* @author Mark Paluch
*/
@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -72,7 +75,8 @@ void testCreateSender() throws Exception {
void testCreateSenderFailUdp() throws Exception {

GelfSender result = GelfSenderFactory.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
assertThat(result).isNull();
assertThat(result).isNotNull();
verify(errorReporter).reportError(anyString(), ArgumentMatchers.<Exception> any());
}

@Test
Expand All @@ -81,7 +85,8 @@ void testCreateSenderFailTcp() throws Exception {
reset(assembler);
when(assembler.getHost()).thenReturn("tcp:" + THE_HOST);
GelfSender result = GelfSenderFactory.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
assertThat(result).isNull();
assertThat(result).isNotNull();
verify(errorReporter).reportError(anyString(), ArgumentMatchers.<Exception> any());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package biz.paluch.logging.gelf.intern.sender;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
Expand All @@ -11,7 +11,6 @@
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.util.Random;
Expand All @@ -28,6 +27,8 @@
import biz.paluch.logging.gelf.intern.GelfMessage;

/**
* Unit tests for {@link GelfTCPSender}.
*
* @author Mark Paluch
*/
@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -79,14 +80,10 @@ void connectionTimeoutShouldApply() throws Exception {
}

@Test
void unknownHostShouldThrowException() throws Exception {
void unknownHostShouldReportError() throws Exception {

try {
new GelfTCPSender("unknown.host.unknown", 65534, 100, 100, errorReporter);
fail("Missing UnknownHostException");
} catch (UnknownHostException e) {
assertThat(e).isInstanceOf(UnknownHostException.class);
}
new GelfTCPSender("unknown.host.unknown", 65534, 100, 100, errorReporter);
verify(errorReporter).reportError(anyString(), any(Exception.class));
}

@Test
Expand Down Expand Up @@ -210,5 +207,7 @@ protected boolean isConnected() throws IOException {
protected void write(ByteBuffer buffer) throws IOException {
this.buffer = buffer;
}

}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package biz.paluch.logging.gelf.intern.sender;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

import java.net.DatagramSocket;
import java.net.UnknownHostException;
import java.util.Random;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.function.Executable;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
Expand All @@ -19,6 +16,8 @@
import biz.paluch.logging.gelf.intern.GelfMessage;

/**
* Unit tests for {@link GelfUDPSender}.
*
* @author Mark Paluch
*/
@ExtendWith(MockitoExtension.class)
Expand All @@ -43,12 +42,8 @@ void unreachablePacketsShouldBeDiscardedSilently() throws Exception {
@Test
void unknownHostShouldThrowException() throws Exception {

assertThrows(UnknownHostException.class, new Executable() {
@Override
public void execute() throws Throwable {
new GelfUDPSender("unknown.host.unknown", 65534, errorReporter);
}
});
new GelfUDPSender("unknown.host.unknown", 65534, errorReporter);
verify(errorReporter).reportError(anyString(), any(Exception.class));
}

@Test
Expand Down Expand Up @@ -106,4 +101,5 @@ int randomPort() {
Random random = new Random();
return random.nextInt(50000) + 1024;
}

}

0 comments on commit ae4bb85

Please sign in to comment.