Search in sources :

Example 11 with BasicHttpTransportMetrics

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));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) RandomAccessFile(java.io.RandomAccessFile) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Example 12 with BasicHttpTransportMetrics

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));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) RandomAccessFile(java.io.RandomAccessFile) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Example 13 with BasicHttpTransportMetrics

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());
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 14 with BasicHttpTransportMetrics

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));
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) RandomAccessFile(java.io.RandomAccessFile) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Example 15 with BasicHttpTransportMetrics

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());
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)99 Test (org.junit.jupiter.api.Test)98 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)51 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)50 ReadableByteChannel (java.nio.channels.ReadableByteChannel)44 ReadableByteChannelMock (org.apache.hc.core5.http.ReadableByteChannelMock)44 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)44 ByteBuffer (java.nio.ByteBuffer)34 RandomAccessFile (java.io.RandomAccessFile)23 FileChannel (java.nio.channels.FileChannel)23 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)11 OutputStream (java.io.OutputStream)3 Http1Config (org.apache.hc.core5.http.config.Http1Config)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 MalformedChunkCodingException (org.apache.hc.core5.http.MalformedChunkCodingException)2 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1