use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityEncoder 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 IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 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);
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityEncoder method testCodingFragmentBufferingChannelSaturated2.
@Test
public void testCodingFragmentBufferingChannelSaturated2() 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 IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 8);
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("much more stuff")));
Mockito.verify(channel, Mockito.times(3)).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());
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertEquals("stuff--m", s);
Assertions.assertEquals(0, outbuf.length());
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityEncoder method testCodingEmptySrcBuffer.
@Test
public void testCodingEmptySrcBuffer() throws Exception {
final WritableByteChannelMock channel = new WritableByteChannelMock(64);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
encoder.write(CodecTestUtils.wrap("stuff"));
final ByteBuffer empty = ByteBuffer.allocate(100);
empty.flip();
encoder.write(empty);
encoder.write(null);
encoder.complete();
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertTrue(encoder.isCompleted());
Assertions.assertEquals("stuff", s);
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityEncoder method testCodingFromFileChannelSaturated.
@Test
public void testCodingFromFileChannelSaturated() throws Exception {
final WritableByteChannelMock channel = new WritableByteChannelMock(64, 4);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
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));
} finally {
testfile.close();
}
testfile = new RandomAccessFile(this.tmpfile, "rw");
try {
final FileChannel fchannel = testfile.getChannel();
encoder.transfer(fchannel, 0, 20);
encoder.transfer(fchannel, 0, 20);
} finally {
testfile.close();
}
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertFalse(encoder.isCompleted());
Assertions.assertEquals("head", s);
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityEncoder 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 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(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());
}
Aggregations