use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityEncoder 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 IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 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);
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedEncoder method testCodingFragmentBufferingBufferFlush2.
@Test
public void testCodingFragmentBufferingBufferFlush2() 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(16, encoder.write(CodecTestUtils.wrap("-much more stuff")));
Mockito.verify(channel, Mockito.times(2)).write(ArgumentMatchers.any());
Mockito.verify(outbuf, Mockito.times(1)).write(ArgumentMatchers.<ByteBuffer>any());
Mockito.verify(outbuf, Mockito.times(1)).flush(channel);
Assertions.assertEquals(21, metrics.getBytesTransferred());
Assertions.assertEquals(0, outbuf.length());
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertEquals("stuff-much more stuff", s);
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedEncoder method testCodingEmptyFile.
@Test
public void testCodingEmptyFile() 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);
encoder.write(CodecTestUtils.wrap("stuff;"));
// Create an empty file
createTempFile();
RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
testfile.close();
testfile = new RandomAccessFile(this.tmpfile, "rw");
try {
final FileChannel fchannel = testfile.getChannel();
encoder.transfer(fchannel, 0, 20);
encoder.write(CodecTestUtils.wrap("more stuff"));
} finally {
testfile.close();
}
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertTrue(encoder.isCompleted());
Assertions.assertEquals("stuff;more stuff", s);
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedEncoder method testCodingFragmentBufferingTinyFragments2.
@Test
public void testCodingFragmentBufferingTinyFragments2() 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, 2);
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(10, encoder.write(CodecTestUtils.wrap("more stuff")));
Mockito.verify(channel, Mockito.times(4)).write(ArgumentMatchers.any());
Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any());
Mockito.verify(outbuf, Mockito.times(2)).flush(channel);
Assertions.assertEquals(18, metrics.getBytesTransferred());
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertEquals("stuff---more stuff", s);
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedEncoder method testCodingFragmentBuffering.
@Test
public void testCodingFragmentBuffering() 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 CharArrayBuffer chbuffer = new CharArrayBuffer(16);
chbuffer.append("header");
outbuf.writeLine(chbuffer);
final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 32);
Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
Mockito.verify(channel, Mockito.never()).write(ArgumentMatchers.any());
Mockito.verify(outbuf, Mockito.times(1)).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("header\r\nstuff", s);
}
Aggregations