Search in sources :

Example 1 with MockByteChannel

use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.

the class MimeUtilityFunctionTest method testLargePayload.

@Test(description = "When the payload exceeds 2MB check whether the response received back matches  " + "the original content length")
public void testLargePayload() {
    String path = "/test/largepayload";
    try {
        ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/fileThatExceeds2MB.txt");
        Channel channel = new MockByteChannel(byteChannel, 10);
        CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
        String responseValue = characterChannel.readAll();
        characterChannel.close();
        HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "POST", responseValue);
        HTTPCarbonMessage response = Services.invokeNew(serviceResult, "mockEP", cMsg);
        Assert.assertNotNull(response, "Response message not found");
        InputStream inputStream = new HttpMessageDataStreamer(response).getInputStream();
        Assert.assertNotNull(inputStream, "Inputstream is null");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MimeUtil.writeInputToOutputStream(inputStream, outputStream);
        Assert.assertEquals(outputStream.size(), 2323779);
    } catch (IOException | URISyntaxException e) {
        log.error("Error occurred in testLargePayload", e.getMessage());
    }
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) InputStream(java.io.InputStream) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) BString(org.ballerinalang.model.values.BString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) Test(org.testng.annotations.Test)

Example 2 with MockByteChannel

use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.

the class AsyncReadWriteTest method writeBytes.

@Test(description = "Write into a channel using async io framework")
public void writeBytes() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "write.txt");
    Channel channel = new MockByteChannel(byteChannel);
    byte[] bytes = "hello".getBytes();
    int numberOfBytesWritten = IOUtils.writeFull(channel, bytes, 0, new EventContext());
    Assert.assertEquals(numberOfBytesWritten, bytes.length);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 3 with MockByteChannel

use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.

the class BytesInputOutputBufferTest method excessBufferAllocation.

@Test(description = "Read all bytes from file with larger buffer size")
public void excessBufferAllocation() throws IOException, URISyntaxException {
    int initialReadLimit = 3;
    int secondLapReadLimit = 3;
    int thirdLapReadLimit = 3;
    // During the 3rd lap all the bytes were get
    int thirdLapReadLimitExpected = 0;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
    Channel channel = new MockByteChannel(byteChannel, IOConstants.CHANNEL_BUFFER_SIZE);
    byte[] readBytes = channel.readFull(initialReadLimit);
    // This should hold the number of bytes get
    Assert.assertEquals(readBytes.length, initialReadLimit);
    readBytes = channel.readFull(secondLapReadLimit);
    // This should hold the number of bytes get
    Assert.assertEquals(readBytes.length, secondLapReadLimit);
    readBytes = channel.readFull(thirdLapReadLimit);
    channel.close();
    // This should hold the number of bytes get
    Assert.assertEquals(readBytes.length, thirdLapReadLimitExpected);
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 4 with MockByteChannel

use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.

the class BytesInputOutputBufferTest method fixedBufferIterativeRead.

@Test(description = "Reads bytes which has a fixed buffer for multiple read iterations")
public void fixedBufferIterativeRead() throws IOException, URISyntaxException {
    final int numberOfBytesInFile = 7;
    final int fixedBufferSize = 15;
    int readByteLength = -1;
    int totalNumberOfBytesRead = 0;
    ByteBuffer content = ByteBuffer.allocate(fixedBufferSize);
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/sequenceOfChars");
    Channel channel = new MockByteChannel(byteChannel);
    while (readByteLength != 0) {
        ReadByteResult readByteResult = read(3, channel);
        byte[] readBytes = readByteResult.getContent();
        content.put(readBytes);
        readByteLength = readByteResult.getNumberOfBytesRead();
        totalNumberOfBytesRead = totalNumberOfBytesRead + readByteLength;
    }
    content.flip();
    Assert.assertEquals(totalNumberOfBytesRead, numberOfBytesInFile);
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) ByteBuffer(java.nio.ByteBuffer) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 5 with MockByteChannel

use of org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel in project ballerina by ballerina-lang.

the class BytesInputOutputBufferTest method varyingBufferSizeTest.

@Test(description = "Reads file which has varying buffer sizes")
public void varyingBufferSizeTest() throws IOException, URISyntaxException {
    final int numberOfBytesInFile = 7;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/sequenceOfChars");
    Channel channel = new MockByteChannel(byteChannel);
    int numberOfBytesRead = read(8, channel).getNumberOfBytesRead();
    Assert.assertEquals(numberOfBytesRead, numberOfBytesInFile);
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Aggregations

ByteChannel (java.nio.channels.ByteChannel)29 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)29 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)29 Test (org.testng.annotations.Test)29 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)16 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)7 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 InputStream (java.io.InputStream)3 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ByteBuffer (java.nio.ByteBuffer)1 BString (org.ballerinalang.model.values.BString)1 ReadCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent)1 WriteCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent)1 DelimitedRecordReadEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)1