make tests more robust

1. we are now using a random port for listening
2. TcpIngestor.start() waits until the socket is established.
This commit is contained in:
2020-10-31 09:46:12 +01:00
parent af4151bc81
commit de6ffe5097
4 changed files with 75 additions and 46 deletions

View File

@@ -36,14 +36,14 @@ public class PdbTestUtil {
static final Map<String, Object> POISON = new HashMap<>();
public static final void send(final String format, final Collection<Map<String, Object>> entries)
public static final void send(final String format, final Collection<Map<String, Object>> entries, final int port)
throws IOException, InterruptedException {
switch (format) {
case "csv":
sendAsCsv(entries);
sendAsCsv(entries, port);
break;
case "json":
sendAsJson(entries);
sendAsJson(entries, port);
break;
default:
throw new IllegalStateException("unhandled format: " + format);
@@ -51,20 +51,21 @@ public class PdbTestUtil {
}
@SafeVarargs
public static final void sendAsCsv(final Map<String, Object>... entries) throws IOException, InterruptedException {
sendAsCsv(Arrays.asList(entries));
public static final void sendAsCsv(final int port, final Map<String, Object>... entries)
throws IOException, InterruptedException {
sendAsCsv(Arrays.asList(entries), port);
}
public static final void sendAsCsv(final Collection<Map<String, Object>> entries)
public static final void sendAsCsv(final Collection<Map<String, Object>> entries, final int port)
throws IOException, InterruptedException {
final Set<String> keys = entries.stream().map(Map::keySet).flatMap(Set::stream).collect(Collectors.toSet());
sendAsCsv(keys, entries);
sendAsCsv(keys, entries, port);
}
public static final void sendAsCsv(final Collection<String> keys, final Collection<Map<String, Object>> entries)
throws IOException, InterruptedException {
public static final void sendAsCsv(final Collection<String> keys, final Collection<Map<String, Object>> entries,
final int port) throws IOException, InterruptedException {
final StringBuilder csv = new StringBuilder();
@@ -81,26 +82,28 @@ public class PdbTestUtil {
csv.append("\n");
}
System.out.println("sending: " + csv);
send(csv.toString());
send(csv.toString(), port);
}
@SafeVarargs
public static final void sendAsJson(final Map<String, Object>... entries) throws IOException, InterruptedException {
public static final void sendAsJson(final int port, final Map<String, Object>... entries)
throws IOException, InterruptedException {
sendAsJson(Arrays.asList(entries));
sendAsJson(Arrays.asList(entries), port);
}
public static final void sendAsJson(final Collection<Map<String, Object>> entries)
public static final void sendAsJson(final Collection<Map<String, Object>> entries, final int port)
throws IOException, InterruptedException {
final LinkedBlockingDeque<Map<String, Object>> queue = new LinkedBlockingDeque<>(entries);
queue.put(POISON);
sendAsJson(queue);
sendAsJson(queue, port);
}
public static final void sendAsJson(final BlockingQueue<Map<String, Object>> aEntriesSupplier) throws IOException {
public static final void sendAsJson(final BlockingQueue<Map<String, Object>> aEntriesSupplier, final int port)
throws IOException {
final ObjectMapper mapper = new ObjectMapper();
final SocketChannel channel = connect();
final SocketChannel channel = connect(port);
Map<String, Object> entry;
while ((entry = aEntriesSupplier.poll()) != POISON) {
@@ -124,9 +127,9 @@ public class PdbTestUtil {
LOGGER.trace("closed sender connection");
}
public static final void send(final String data) throws IOException {
public static final void send(final String data, final int port) throws IOException {
final SocketChannel channel = connect();
final SocketChannel channel = connect(port);
final StringBuilder streamData = new StringBuilder();
streamData.append(data);
@@ -145,8 +148,8 @@ public class PdbTestUtil {
LOGGER.trace("closed sender connection");
}
public static void send(final Path file) throws IOException {
final SocketChannel outputChannel = connect();
public static void send(final Path file, final int port) throws IOException {
final SocketChannel outputChannel = connect(port);
try (final FileChannel inputChannel = FileChannel.open(file, StandardOpenOption.READ)) {
inputChannel.transferTo(0, Long.MAX_VALUE, outputChannel);
@@ -163,7 +166,7 @@ public class PdbTestUtil {
LOGGER.trace("closed sender connection");
}
private static SocketChannel connect() throws IOException {
private static SocketChannel connect(final int port) throws IOException {
SocketChannel result = null;
@@ -171,7 +174,7 @@ public class PdbTestUtil {
try {
result = SocketChannel.open();
result.configureBlocking(true);
result.connect(new InetSocketAddress("127.0.0.1", TcpIngestor.PORT));
result.connect(new InetSocketAddress("127.0.0.1", port));
break;
} catch (final ConnectException e) {
// server socket not yet ready, it should be ready any time soon