extract utility method
a method to send json over tcp can be used by several tests
This commit is contained in:
@@ -105,12 +105,12 @@ public class PlotSettings {
|
|||||||
|
|
||||||
public OffsetDateTime dateTo() {
|
public OffsetDateTime dateTo() {
|
||||||
|
|
||||||
final int period = Integer.parseInt(dateRange.split(" ")[0]);
|
|
||||||
final ChronoUnit unit = toChronoUnit(dateRange.split(" ")[1]);
|
|
||||||
|
|
||||||
if (StringUtils.isEmpty(dateRange)) {
|
if (StringUtils.isEmpty(dateRange)) {
|
||||||
return OffsetDateTime.ofInstant(Instant.ofEpochMilli(Long.MAX_VALUE), ZoneOffset.UTC);
|
return OffsetDateTime.ofInstant(Instant.ofEpochMilli(Long.MAX_VALUE), ZoneOffset.UTC);
|
||||||
} else {
|
} else {
|
||||||
|
final int period = Integer.parseInt(dateRange.split(" ")[0]);
|
||||||
|
final ChronoUnit unit = toChronoUnit(dateRange.split(" ")[1]);
|
||||||
|
|
||||||
return dateFrom().plus(period, unit);
|
return dateFrom().plus(period, unit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package org.lucares.performance.db.ingestor;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ConnectException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.SocketChannel;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingDeque;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.lucares.pdbui.TcpIngestor;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
public class PdbTestUtil {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(PdbTestUtil.class);
|
||||||
|
|
||||||
|
private static final Map<String, Object> POISON = new HashMap<>();
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public static final void send(final Map<String, Object>... entries) throws IOException, InterruptedException {
|
||||||
|
|
||||||
|
final LinkedBlockingDeque<Map<String, Object>> queue = new LinkedBlockingDeque<>(Arrays.asList(entries));
|
||||||
|
queue.put(POISON);
|
||||||
|
send(queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final void send(final BlockingQueue<Map<String, Object>> aEntriesSupplier) throws IOException {
|
||||||
|
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final SocketChannel channel = connect();
|
||||||
|
|
||||||
|
Map<String, Object> entry;
|
||||||
|
while ((entry = aEntriesSupplier.poll()) != POISON) {
|
||||||
|
|
||||||
|
final StringBuilder streamData = new StringBuilder();
|
||||||
|
streamData.append(mapper.writeValueAsString(entry));
|
||||||
|
streamData.append("\n");
|
||||||
|
|
||||||
|
final ByteBuffer src = ByteBuffer.wrap(streamData.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
|
channel.write(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// ugly workaround: the channel was closed too early and not all
|
||||||
|
// data was received
|
||||||
|
TimeUnit.MILLISECONDS.sleep(10);
|
||||||
|
} catch (final InterruptedException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
channel.close();
|
||||||
|
LOGGER.trace("closed sender connection");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SocketChannel connect() throws IOException {
|
||||||
|
|
||||||
|
SocketChannel result = null;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
result = SocketChannel.open();
|
||||||
|
result.configureBlocking(true);
|
||||||
|
result.connect(new InetSocketAddress("127.0.0.1", TcpIngestor.PORT));
|
||||||
|
break;
|
||||||
|
} catch (final ConnectException e) {
|
||||||
|
// server socket not yet ready, it should be ready any time soon
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,6 @@
|
|||||||
package org.lucares.performance.db.ingestor;
|
package org.lucares.performance.db.ingestor;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ConnectException;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.nio.channels.SocketChannel;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
@@ -16,7 +11,6 @@ import java.util.Collections;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import org.lucares.pdb.api.Entry;
|
import org.lucares.pdb.api.Entry;
|
||||||
import org.lucares.pdbui.TcpIngestor;
|
import org.lucares.pdbui.TcpIngestor;
|
||||||
@@ -29,8 +23,6 @@ import org.testng.annotations.AfterMethod;
|
|||||||
import org.testng.annotations.BeforeMethod;
|
import org.testng.annotations.BeforeMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
|
|
||||||
import liquibase.exception.LiquibaseException;
|
import liquibase.exception.LiquibaseException;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -72,7 +64,7 @@ public class TcpIngestorTest {
|
|||||||
entryB.put("host", host);
|
entryB.put("host", host);
|
||||||
entryB.put("tags", Collections.emptyList());
|
entryB.put("tags", Collections.emptyList());
|
||||||
|
|
||||||
send(entryA, entryB);
|
PdbTestUtil.send(entryA, entryB);
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
LOGGER.error("", e);
|
LOGGER.error("", e);
|
||||||
throw e;
|
throw e;
|
||||||
@@ -90,51 +82,6 @@ public class TcpIngestorTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SafeVarargs
|
|
||||||
private final void send(final Map<String, Object>... entries) throws IOException {
|
|
||||||
|
|
||||||
final StringBuilder streamData = new StringBuilder();
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
|
||||||
|
|
||||||
for (final Map<String, Object> entry : entries) {
|
|
||||||
|
|
||||||
streamData.append(mapper.writeValueAsString(entry));
|
|
||||||
streamData.append("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
final SocketChannel channel = connect();
|
|
||||||
final ByteBuffer src = ByteBuffer.wrap(streamData.toString().getBytes(StandardCharsets.UTF_8));
|
|
||||||
channel.write(src);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// ugly workaround: the channel was close too early and not all data
|
|
||||||
// was received
|
|
||||||
TimeUnit.MILLISECONDS.sleep(10);
|
|
||||||
} catch (final InterruptedException e) {
|
|
||||||
throw new IllegalStateException(e);
|
|
||||||
}
|
|
||||||
channel.close();
|
|
||||||
LOGGER.trace("closed sender connection");
|
|
||||||
}
|
|
||||||
|
|
||||||
private SocketChannel connect() throws IOException {
|
|
||||||
|
|
||||||
SocketChannel result = null;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
result = SocketChannel.open();
|
|
||||||
result.configureBlocking(true);
|
|
||||||
result.connect(new InetSocketAddress("127.0.0.1", TcpIngestor.PORT));
|
|
||||||
break;
|
|
||||||
} catch (final ConnectException e) {
|
|
||||||
// server socket not yet ready, it should be ready any time soon
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIngestionThreadDoesNotDieOnErrors() throws Exception {
|
public void testIngestionThreadDoesNotDieOnErrors() throws Exception {
|
||||||
final OffsetDateTime invalidDate = OffsetDateTime.ofInstant(Instant.ofEpochMilli(-1), ZoneOffset.UTC);
|
final OffsetDateTime invalidDate = OffsetDateTime.ofInstant(Instant.ofEpochMilli(-1), ZoneOffset.UTC);
|
||||||
@@ -157,7 +104,7 @@ public class TcpIngestorTest {
|
|||||||
entryB.put("host", host);
|
entryB.put("host", host);
|
||||||
entryB.put("tags", Collections.emptyList());
|
entryB.put("tags", Collections.emptyList());
|
||||||
|
|
||||||
send(entryA, entryB);
|
PdbTestUtil.send(entryA, entryB);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
|
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user