Search in sources :

Example 36 with ByteArrayOutputStream

use of org.apache.nifi.stream.io.ByteArrayOutputStream in project nifi by apache.

the class TestSocketClientTransaction method testReceiveZeroFlowFile.

@Test
public void testReceiveZeroFlowFile() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
    ResponseCode.NO_MORE_DATA.writeResponse(serverResponse);
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.RECEIVE);
    execReceiveZeroFlowFile(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient));
    assertEquals(-1, sentByClient.read());
}
Also used : ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 37 with ByteArrayOutputStream

use of org.apache.nifi.stream.io.ByteArrayOutputStream in project nifi by apache.

the class TestSocketClientTransaction method testReceiveTwoFlowFiles.

@Test
public void testReceiveTwoFlowFiles() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
    ResponseCode.MORE_DATA.writeResponse(serverResponse);
    codec.encode(createDataPacket("contents on server 1"), serverResponse);
    ResponseCode.CONTINUE_TRANSACTION.writeResponse(serverResponse);
    codec.encode(createDataPacket("contents on server 2"), serverResponse);
    ResponseCode.FINISH_TRANSACTION.writeResponse(serverResponse);
    ResponseCode.CONFIRM_TRANSACTION.writeResponse(serverResponse, "Checksum has been verified at server.");
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.RECEIVE);
    assertEquals(Transaction.TransactionState.TRANSACTION_STARTED, transaction.getState());
    execReceiveTwoFlowFiles(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient));
    Response confirmResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode());
    assertEquals("Checksum should be calculated at client", "2969091230", confirmResponse.getMessage());
    Response completeResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.TRANSACTION_FINISHED, completeResponse.getCode());
    assertEquals(-1, sentByClient.read());
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 38 with ByteArrayOutputStream

use of org.apache.nifi.stream.io.ByteArrayOutputStream in project nifi by apache.

the class TestSocketClientTransaction method testSendOneFlowFile.

@Test
public void testSendOneFlowFile() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
    ResponseCode.CONFIRM_TRANSACTION.writeResponse(serverResponse, "2946083981");
    ResponseCode.TRANSACTION_FINISHED.writeResponse(serverResponse);
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.SEND);
    execSendOneFlowFile(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.SEND_FLOWFILES, RequestType.readRequestType(sentByClient));
    DataPacket packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 1", readContents(packetByClient));
    Response endOfDataResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.FINISH_TRANSACTION, endOfDataResponse.getCode());
    Response confirmResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode());
    assertEquals(-1, sentByClient.read());
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) DataPacket(org.apache.nifi.remote.protocol.DataPacket) SiteToSiteTestUtils.createDataPacket(org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket) Test(org.junit.Test)

Example 39 with ByteArrayOutputStream

use of org.apache.nifi.stream.io.ByteArrayOutputStream in project nifi by apache.

the class TestLeakyBucketThrottler method testInputStreamInterface.

@Test(timeout = 10000)
public void testInputStreamInterface() throws IOException {
    final byte[] data = new byte[1024 * 1024 * 4];
    // throttle rate at 1 MB/sec
    try (final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024);
        final ByteArrayInputStream bais = new ByteArrayInputStream(data);
        final InputStream throttledIn = throttler.newThrottledInputStream(bais);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final byte[] buffer = new byte[4096];
        final long start = System.currentTimeMillis();
        int len;
        while ((len = throttledIn.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }
        final long millis = System.currentTimeMillis() - start;
        // should take 4 sec give or take
        assertTrue(millis > 3000);
        assertTrue(millis < 6000);
    }
}
Also used : ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) InputStream(java.io.InputStream) LeakyBucketStreamThrottler(org.apache.nifi.stream.io.LeakyBucketStreamThrottler) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 40 with ByteArrayOutputStream

use of org.apache.nifi.stream.io.ByteArrayOutputStream in project nifi by apache.

the class TestLeakyBucketThrottler method testOutputStreamInterface.

@Test(timeout = 10000)
public void testOutputStreamInterface() throws IOException {
    // throttle rate at 1 MB/sec
    final LeakyBucketStreamThrottler throttler = new LeakyBucketStreamThrottler(1024 * 1024);
    final byte[] data = new byte[1024 * 1024 * 4];
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (final OutputStream throttledOut = throttler.newThrottledOutputStream(baos)) {
        final long start = System.currentTimeMillis();
        throttledOut.write(data);
        throttler.close();
        final long millis = System.currentTimeMillis() - start;
        // should take 4 sec give or take
        assertTrue(millis > 3000);
        assertTrue(millis < 6000);
    }
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) LeakyBucketStreamThrottler(org.apache.nifi.stream.io.LeakyBucketStreamThrottler) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)71 Test (org.junit.Test)51 TestRunner (org.apache.nifi.util.TestRunner)27 MockFlowFile (org.apache.nifi.util.MockFlowFile)25 File (java.io.File)22 ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)22 Schema (org.apache.avro.Schema)21 IOException (java.io.IOException)15 Peer (org.apache.nifi.remote.Peer)15 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)14 GenericRecord (org.apache.avro.generic.GenericRecord)13 TransactionResultEntity (org.apache.nifi.web.api.entity.TransactionResultEntity)12 DataInputStream (java.io.DataInputStream)11 DataOutputStream (java.io.DataOutputStream)11 SiteToSiteRestApiClient (org.apache.nifi.remote.util.SiteToSiteRestApiClient)9 DataPacket (org.apache.nifi.remote.protocol.DataPacket)8 SiteToSiteTestUtils.createDataPacket (org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket)8 Response (org.apache.nifi.remote.protocol.Response)7 UnknownHostException (java.net.UnknownHostException)6 ApiOperation (io.swagger.annotations.ApiOperation)5