Search in sources :

Example 1 with HttpTransportMetrics

use of org.apache.hc.core5.http.io.HttpTransportMetrics in project httpcomponents-core by apache.

the class TestSessionInOutBuffers method testBasicReadWriteLine.

@Test
public void testBasicReadWriteLine() throws Exception {
    final String[] teststrs = new String[5];
    teststrs[0] = "Hello";
    teststrs[1] = "This string should be much longer than the size of the output buffer " + "which is only 16 bytes for this test";
    final StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < 15; i++) {
        buffer.append("123456789 ");
    }
    buffer.append("and stuff like that");
    teststrs[2] = buffer.toString();
    teststrs[3] = "";
    teststrs[4] = "And goodbye";
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (final String teststr : teststrs) {
        chbuffer.clear();
        chbuffer.append(teststr);
        outbuffer.writeLine(chbuffer, outputStream);
    }
    // these write operations should have no effect
    outbuffer.writeLine(null, outputStream);
    outbuffer.flush(outputStream);
    HttpTransportMetrics tmetrics = outbuffer.getMetrics();
    final long bytesWritten = tmetrics.getBytesTransferred();
    long expected = 0;
    for (final String teststr : teststrs) {
        expected += (teststr.length() + 2);
    }
    Assertions.assertEquals(expected, bytesWritten);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    for (final String teststr : teststrs) {
        chbuffer.clear();
        inBuffer.readLine(chbuffer, inputStream);
        Assertions.assertEquals(teststr, chbuffer.toString());
    }
    chbuffer.clear();
    Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
    chbuffer.clear();
    Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
    tmetrics = inBuffer.getMetrics();
    final long bytesRead = tmetrics.getBytesTransferred();
    Assertions.assertEquals(expected, bytesRead);
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) HttpTransportMetrics(org.apache.hc.core5.http.io.HttpTransportMetrics) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)1 HttpTransportMetrics (org.apache.hc.core5.http.io.HttpTransportMetrics)1 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)1 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)1 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)1 Test (org.junit.jupiter.api.Test)1