use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testBasicDecodingFile.
/* ----------------- FileChannel Part testing --------------------------- */
@Test
public void testBasicDecodingFile() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff; ", "more stuff; ", "a lot more stuff!!!" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 36);
createTempFile();
try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
final FileChannel fchannel = testfile.getChannel();
long pos = 0;
while (!decoder.isCompleted()) {
final long bytesRead = decoder.transfer(fchannel, pos, 10);
if (bytesRead > 0) {
pos += bytesRead;
}
}
}
Assertions.assertEquals(this.tmpfile.length(), metrics.getBytesTransferred());
Assertions.assertEquals("stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
}
use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testDecodingFileWithBufferedSessionData.
@Test
public void testDecodingFileWithBufferedSessionData() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff; ", "more stuff; ", "a lot more stuff!!!" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 36);
final int i = inbuf.fill(channel);
Assertions.assertEquals(7, i);
createTempFile();
try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
final FileChannel fchannel = testfile.getChannel();
long pos = 0;
while (!decoder.isCompleted()) {
final long bytesRead = decoder.transfer(fchannel, pos, 10);
if (bytesRead > 0) {
pos += bytesRead;
}
}
}
Assertions.assertEquals(this.tmpfile.length() - 7, metrics.getBytesTransferred());
Assertions.assertEquals("stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
}
use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testDecodingFromSessionBuffer1.
@Test
public void testDecodingFromSessionBuffer1() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff;", "more stuff" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
inbuf.fill(channel);
Assertions.assertEquals(6, inbuf.length());
final LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 16);
final ByteBuffer dst = ByteBuffer.allocate(1024);
int bytesRead = decoder.read(dst);
Assertions.assertEquals(6, bytesRead);
Assertions.assertEquals("stuff;", CodecTestUtils.convert(dst));
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(0, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(10, bytesRead);
Assertions.assertEquals("more stuff", CodecTestUtils.convert(dst));
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(10, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(10, metrics.getBytesTransferred());
}
use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testWriteBeyondFileSize.
@Test
public void testWriteBeyondFileSize() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "a" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 1);
createTempFile();
try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
final FileChannel fchannel = testfile.getChannel();
Assertions.assertEquals(0, testfile.length());
Assertions.assertThrows(IOException.class, () -> decoder.transfer(fchannel, 5, 10));
}
}
use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testBasicDecodingSmallBuffer.
@Test
public void testBasicDecodingSmallBuffer() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff;", "more stuff" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 16);
final ByteBuffer dst = ByteBuffer.allocate(4);
int bytesRead = decoder.read(dst);
Assertions.assertEquals(4, bytesRead);
Assertions.assertEquals("stuf", CodecTestUtils.convert(dst));
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(4, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(2, bytesRead);
Assertions.assertEquals("f;", CodecTestUtils.convert(dst));
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(6, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(4, bytesRead);
Assertions.assertEquals("more", CodecTestUtils.convert(dst));
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(10, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(4, bytesRead);
Assertions.assertEquals(" stu", CodecTestUtils.convert(dst));
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(14, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(2, bytesRead);
Assertions.assertEquals("ff", CodecTestUtils.convert(dst));
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(16, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(16, metrics.getBytesTransferred());
}
Aggregations