use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.
the class TestHttpClient method writeOutgoingPacket.
private static void writeOutgoingPacket(OutputStream outputStream) throws IOException {
final DataPacket packet = new DataPacketBuilder().contents("Example contents from server.").attr("Server attr 1", "Server attr 1 value").attr("Server attr 2", "Server attr 2 value").build();
new StandardFlowFileCodec().encode(packet, outputStream);
outputStream.flush();
}
use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.
the class TestHttpClient method testSendTimeoutAfterDataExchange.
@Test
public void testSendTimeoutAfterDataExchange() throws Exception {
// skip on windows
assumeFalse(isWindowsEnvironment());
System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote.protocol.http.HttpClientTransaction", "INFO");
try (SiteToSiteClient client = getDefaultBuilder().idleExpiration(500, TimeUnit.MILLISECONDS).timeout(500, TimeUnit.MILLISECONDS).portName("input-timeout-data-ex").build()) {
final Transaction transaction = client.createTransaction(TransferDirection.SEND);
assertNotNull(transaction);
DataPacket packet = new DataPacketBuilder().contents("Example contents from client.").attr("Client attr 1", "Client attr 1 value").attr("Client attr 2", "Client attr 2 value").build();
for (int i = 0; i < 100; i++) {
transaction.send(packet);
if (i % 10 == 0) {
logger.info("Sent {} packets...", i);
}
}
try {
confirmShouldFail(transaction);
fail("Should be timeout.");
} catch (IOException e) {
logger.info("Exception message: {}", e.getMessage());
assertTrue(e.getMessage().contains("TimeoutException"));
}
completeShouldFail(transaction);
}
}
use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.
the class TestSiteToSiteClient method testSend.
@Test
@Ignore("For local testing only; not really a unit test but a manual test")
public void testSend() throws IOException {
System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote", "DEBUG");
final SiteToSiteClient client = new SiteToSiteClient.Builder().url("http://localhost:8080/nifi").portName("input").build();
try {
final Transaction transaction = client.createTransaction(TransferDirection.SEND);
Assert.assertNotNull(transaction);
final Map<String, String> attrs = new HashMap<>();
attrs.put("site-to-site", "yes, please!");
final byte[] bytes = "Hello".getBytes();
final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
final DataPacket packet = new StandardDataPacket(attrs, bais, bytes.length);
transaction.send(packet);
transaction.confirm();
transaction.complete();
} finally {
client.close();
}
}
use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.
the class TestSiteToSiteClient method testReceive.
@Test
@Ignore("For local testing only; not really a unit test but a manual test")
public void testReceive() throws IOException {
System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote", "DEBUG");
final SiteToSiteClient client = new SiteToSiteClient.Builder().url("http://localhost:8080/nifi").portName("cba").requestBatchCount(10).build();
try {
for (int i = 0; i < 1000; i++) {
final Transaction transaction = client.createTransaction(TransferDirection.RECEIVE);
Assert.assertNotNull(transaction);
DataPacket packet;
while (true) {
packet = transaction.receive();
if (packet == null) {
break;
}
final InputStream in = packet.getData();
final long size = packet.getSize();
final byte[] buff = new byte[(int) size];
StreamUtils.fillBuffer(in, buff);
}
transaction.confirm();
transaction.complete();
}
} finally {
client.close();
}
}
use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.
the class TestHttpClientTransaction method testSendButDestinationFull.
@Test
public void testSendButDestinationFull() throws IOException {
SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
doNothing().when(apiClient).openConnectionForSend(eq("portId"), any(Peer.class));
// Emulate that server returns correct checksum.
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
HttpCommunicationsSession commSession = (HttpCommunicationsSession) invocation.getArguments()[0];
commSession.setChecksum("3359812065");
return null;
}
}).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class));
TransactionResultEntity resultEntity = new TransactionResultEntity();
resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED_BUT_DESTINATION_FULL.getCode());
doReturn(resultEntity).when(apiClient).commitTransferFlowFiles(eq(transactionUrl), eq(CONFIRM_TRANSACTION));
ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
ByteArrayInputStream serverResponse = new ByteArrayInputStream(serverResponseBos.toByteArray());
ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.SEND, transactionUrl);
execSendButDestinationFull(transaction);
InputStream sentByClient = new ByteArrayInputStream(clientRequest.toByteArray());
DataPacket packetByClient = codec.decode(sentByClient);
assertEquals("contents on client 1", readContents(packetByClient));
packetByClient = codec.decode(sentByClient);
assertEquals("contents on client 2", readContents(packetByClient));
assertEquals(-1, sentByClient.read());
verify(apiClient).commitTransferFlowFiles(transactionUrl, CONFIRM_TRANSACTION);
}
Aggregations