use of org.webpieces.httpclient.api.mocks.MockResponseListener in project webpieces by deanhiller.
the class TestHttp1Backpressure method testBasicBackpressureChunked.
@Test
public void testBasicBackpressureChunked() throws InterruptedException, ExecutionException, TimeoutException {
MockResponseListener listener = new MockResponseListener();
RequestStreamHandle handle = socket.openStream();
mockChannel.addWriteResponse(XFuture.completedFuture(null));
Http2Request request = Requests.createRequest();
StreamRef streamRef = handle.process(request, listener);
XFuture<StreamWriter> writer = streamRef.getWriter();
Assert.assertTrue(writer.isDone());
Assert.assertEquals(request, mockChannel.getLastWriteParam());
Http2Response response1 = Requests.createChunkedResponse(1);
HttpChunk response2 = Requests.createHttpChunk(250);
HttpLastChunk lastChunk = new HttpLastChunk();
List<ByteBuffer> buffers = create4BuffersWith3Messags(response1, response2, lastChunk);
DataListener dataListener = mockChannel.getConnectedListener();
XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
// resolved since it never made it to the client.
Assert.assertTrue(fut1.isDone());
// this keeps bytes in-memory that is less than bufPool.getSuggestedSize. ie. < 5k
XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
listener.addProcessResponse(requestFuture);
XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
// not resolved yet since client only has part of the data
Assert.assertFalse(fut2.isDone());
XFuture<Void> streamWriterFuture = new XFuture<Void>();
MockStreamWriter mockWriter = new MockStreamWriter();
mockWriter.addProcessResponse(streamWriterFuture);
// This releases the response BUT 'some' data was with response so can't ack yet
requestFuture.complete(mockWriter);
// not resolved yet since requestFuture AND streamWriterFutuer BOTH need to be resolved
Assert.assertFalse(fut2.isDone());
// This is BECAUSE 68 bytes resulted in TWO messages and both futures can ack the 68 bytes.
streamWriterFuture.complete(null);
// NOW it's resolved
fut2.get(2, TimeUnit.SECONDS);
// feed the rest of first chunk in and feed part of last chunk
XFuture<Void> firstChunkAck = new XFuture<Void>();
mockWriter.addProcessResponse(firstChunkAck);
XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
Assert.assertFalse(fut3.isDone());
// ack the http chunk packet
firstChunkAck.complete(null);
fut3.get(2, TimeUnit.SECONDS);
XFuture<Void> lastChunkAck = new XFuture<Void>();
mockWriter.addProcessResponse(lastChunkAck);
XFuture<Void> fut4 = dataListener.incomingData(mockChannel, buffers.get(3));
Assert.assertFalse(fut4.isDone());
lastChunkAck.complete(null);
fut4.get(2, TimeUnit.SECONDS);
}
Aggregations