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());
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations