Search in sources :

Example 21 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestLengthDelimitedEncoder method testCodingFragmentBufferingMultipleFragmentsBeyondContentLimit.

@Test
public void testCodingFragmentBufferingMultipleFragmentsBeyondContentLimit() throws Exception {
    final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
    final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 16, 32);
    Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff; and a lot more stuff")));
    Mockito.verify(channel, Mockito.never()).write(ArgumentMatchers.any());
    Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any());
    Mockito.verify(outbuf, Mockito.never()).flush(channel);
    Assertions.assertEquals(0, metrics.getBytesTransferred());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("stuff-more stuff", s);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) Test(org.junit.jupiter.api.Test)

Example 22 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestLengthDelimitedEncoder method testCodingFromFileFlushBuffer.

@Test
public void testCodingFromFileFlushBuffer() throws Exception {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 16);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.append("header");
    outbuf.writeLine(chbuffer);
    createTempFile();
    RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
    try {
        testfile.write("stuff;".getBytes(StandardCharsets.US_ASCII));
        testfile.write("more stuff".getBytes(StandardCharsets.US_ASCII));
    } finally {
        testfile.close();
    }
    testfile = new RandomAccessFile(this.tmpfile, "rw");
    try {
        final FileChannel fchannel = testfile.getChannel();
        encoder.transfer(fchannel, 0, 20);
    } finally {
        testfile.close();
    }
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertTrue(encoder.isCompleted());
    Assertions.assertEquals("header\r\nstuff;more stuff", s);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) RandomAccessFile(java.io.RandomAccessFile) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 23 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestLengthDelimitedEncoder method testCodingFragmentBufferingBufferFlush.

@Test
public void testCodingFragmentBufferingBufferFlush() throws Exception {
    final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
    final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 8);
    Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
    Assertions.assertEquals(6, encoder.write(CodecTestUtils.wrap("-stuff")));
    Mockito.verify(channel, Mockito.times(1)).write(ArgumentMatchers.any());
    Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any());
    Mockito.verify(outbuf, Mockito.times(1)).flush(channel);
    Assertions.assertEquals(8, metrics.getBytesTransferred());
    Assertions.assertEquals(3, outbuf.length());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("stuff-stuff", s);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) Test(org.junit.jupiter.api.Test)

Example 24 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestLengthDelimitedEncoder method testCodingFragmentBufferingChannelSaturated.

@Test
public void testCodingFragmentBufferingChannelSaturated() throws Exception {
    final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64, 8));
    final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 3);
    Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(0, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(0, encoder.write(CodecTestUtils.wrap("more stuff")));
    Mockito.verify(channel, Mockito.times(5)).write(ArgumentMatchers.any());
    Mockito.verify(outbuf, Mockito.times(6)).write(ArgumentMatchers.<ByteBuffer>any());
    Mockito.verify(outbuf, Mockito.times(4)).flush(channel);
    Assertions.assertEquals(8, metrics.getBytesTransferred());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("stuff---", s);
    Assertions.assertEquals(3, outbuf.length());
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) Test(org.junit.jupiter.api.Test)

Example 25 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestLengthDelimitedEncoder method testCodingFragmentBufferingTinyFragments3.

@Test
public void testCodingFragmentBufferingTinyFragments3() throws Exception {
    final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
    final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 3);
    Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(2, encoder.write(CodecTestUtils.wrap("--")));
    Assertions.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff")));
    Mockito.verify(channel, Mockito.times(4)).write(ArgumentMatchers.any());
    Mockito.verify(outbuf, Mockito.times(5)).write(ArgumentMatchers.<ByteBuffer>any());
    Mockito.verify(outbuf, Mockito.times(2)).flush(channel);
    Assertions.assertEquals(21, metrics.getBytesTransferred());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("stuff------more stuff", s);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)79 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)70 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)52 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)50 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)20 BasicDataStreamChannel (org.apache.hc.core5.http.nio.BasicDataStreamChannel)15 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)14 RandomAccessFile (java.io.RandomAccessFile)11 FileChannel (java.nio.channels.FileChannel)11 ByteBuffer (java.nio.ByteBuffer)10 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)10 WritableByteChannelMock (org.apache.hc.core5.http2.WritableByteChannelMock)5 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)5 Charset (java.nio.charset.Charset)3 Path (java.nio.file.Path)2 HttpStreamResetException (org.apache.hc.core5.http.HttpStreamResetException)2 ExecutorService (java.util.concurrent.ExecutorService)1 Header (org.apache.hc.core5.http.Header)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1 ReadableByteChannelMock (org.apache.hc.core5.http2.ReadableByteChannelMock)1