use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestStreamingRaw method testAsyncJsonGet.
@Test
public void testAsyncJsonGet() throws InterruptedException, ExecutionException {
MockChannel channel = new MockChannel();
XFuture<DataListener> future = mgr.simulateHttpConnect(channel);
DataListener dataListener = future.get();
HttpFullRequest fullRequest = Requests.createJsonRequest(KnownHttpMethod.GET, "/json/streaming");
ByteBuffer buffer = parser.marshalToByteBuffer(fullRequest.getRequest());
DataWrapper data = fullRequest.getData();
byte[] headers = new byte[buffer.remaining()];
buffer.get(headers);
data.getReadableSize();
byte[] part1 = data.readBytesAt(0, 10);
String str = new String(part1);
byte[] part2 = data.readBytesAt(10, data.getReadableSize() - 10);
String str2 = new String(part2);
ByteBuffer firstPacket = ByteBuffer.allocate(headers.length + part1.length);
firstPacket.put(headers);
firstPacket.put(part1);
firstPacket.flip();
XFuture<Boolean> authFuture = new XFuture<Boolean>();
mockAuth.addValueToReturn(authFuture);
Assert.assertEquals(0, Context.getContext().size());
// Feed in request with content-length AND part of the body as well...
XFuture<Void> fut1 = dataListener.incomingData(channel, firstPacket);
Assert.assertEquals(0, Context.getContext().size());
// Feed in more BEFORE authFuture is complete(this was the bug, ie. race condition)
ByteBuffer buf2 = ByteBuffer.allocate(part2.length);
buf2.put(part2);
buf2.flip();
XFuture<Void> fut2 = dataListener.incomingData(channel, buf2);
Assert.assertEquals(0, Context.getContext().size());
XFuture<StreamWriter> streamWriterFuture = new XFuture<StreamWriter>();
mockStreamClient.addStreamWriter(streamWriterFuture);
// complete it
authFuture.complete(true);
Assert.assertFalse(fut1.isDone());
Assert.assertFalse(fut2.isDone());
MockStreamWriter2 mockStreamWriter = new MockStreamWriter2();
streamWriterFuture.complete(mockStreamWriter);
Assert.assertEquals(0, Context.getContext().size());
Assert.assertTrue(fut1.isDone());
Assert.assertTrue(fut2.isDone());
List<StreamMsg> frames = mockStreamWriter.getFrames();
Assert.assertEquals(2, frames.size());
DataFrame f1 = (DataFrame) frames.get(0);
DataFrame f2 = (DataFrame) frames.get(1);
String s1 = f1.getData().createStringFromUtf8(0, f1.getData().getReadableSize());
Assert.assertEquals(str, s1);
String s2 = f2.getData().createStringFromUtf8(0, f2.getData().getReadableSize());
Assert.assertEquals(str2, s2);
Assert.assertEquals(0, Context.getContext().size());
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestHttp11Basic method testFileDownloadWithChunking.
@Test
public void testFileDownloadWithChunking() throws InterruptedException, ExecutionException, TimeoutException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
mockChannel.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
HttpRequest req1 = Http2ToHttp11.translateRequest(in1.request);
Assert.assertEquals(req, req1);
HttpResponse resp = Requests.createResponse();
resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
Http2Response headers = Http11ToHttp2.responseToHeaders(resp);
XFuture<StreamWriter> future = in1.stream.process(headers);
HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
Assert.assertEquals(resp, respToClient);
StreamWriter writer = future.get(2, TimeUnit.SECONDS);
DataFrame dataFrame = new DataFrame();
dataFrame.setEndOfStream(true);
String bodyStr = "hi here and there";
DataWrapper data = DATA_GEN.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
dataFrame.setData(data);
writer.processPiece(dataFrame);
List<HttpPayload> frames = mockChannel.getFramesAndClear();
Assert.assertEquals(2, frames.size());
HttpData chunk = (HttpData) frames.get(0);
DataWrapper body = chunk.getBodyNonNull();
String result = body.createStringFromUtf8(0, body.getReadableSize());
Assert.assertEquals(bodyStr, result);
HttpLastData last = (HttpLastData) frames.get(1);
Assert.assertEquals(0, last.getBodyNonNull().getReadableSize());
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestHttp11Basic method testSendTwoRequestsStreamFirstResponse.
@Test
public void testSendTwoRequestsStreamFirstResponse() throws InterruptedException, ExecutionException, TimeoutException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
mockChannel.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
XFuture<Void> fut1 = mockChannel.sendToSvrAsync(req2);
Assert.assertFalse(fut1.isDone());
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
HttpResponse resp1 = Requests.createResponse(1);
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "10"));
Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
XFuture<StreamWriter> future = in1.stream.process(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
StreamWriter writer = future.get(2, TimeUnit.SECONDS);
Assert.assertFalse(fut1.isDone());
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
byte[] buf = new byte[10];
DataWrapper dataWrapper = DATA_GEN.wrapByteArray(buf);
HttpData data1 = new HttpData(dataWrapper, true);
DataFrame data = (DataFrame) Http11ToHttp2.translateData(data1);
writer.processPiece(data);
fut1.get(2, TimeUnit.SECONDS);
HttpData d = (HttpData) mockChannel.getFrameAndClear();
Assert.assertEquals(10, d.getBody().getReadableSize());
PassedIn in2 = mockListener.getSingleRequest();
HttpResponse resp2 = Requests.createResponse(2);
resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers2 = Http11ToHttp2.responseToHeaders(resp2);
in2.stream.process(headers2);
HttpPayload payload2 = mockChannel.getFrameAndClear();
Assert.assertEquals(resp2, payload2);
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestHttp11Backpressure method testInitializeThenPostWithChunkingBackpressure.
@Test
public void testInitializeThenPostWithChunkingBackpressure() throws InterruptedException, ExecutionException, TimeoutException {
// send first request to get past protocoal detection(ie. initialize) and send response
initialize();
List<ByteBuffer> buffers = create4SplitPayloads();
XFuture<Void> ackBytePayload1 = mockChannel.sendToSvr(buffers.get(0));
// not enough data. parser consumes and acks future for more data(no client ack needed right now)
Assert.assertTrue(ackBytePayload1.isDone());
XFuture<StreamWriter> ackRequest = new XFuture<StreamWriter>();
mockListener.addMockStreamToReturn(new MockStreamRef(ackRequest));
XFuture<Void> ackBytePayload2 = mockChannel.sendToSvr(buffers.get(1));
Assert.assertFalse(ackBytePayload2.isDone());
// This releases the response msg acking 10 bytes
ackRequest.complete(mockStreamWriter);
Assert.assertTrue(ackBytePayload2.isDone());
// feed the rest of first chunk in and feed part of last chunk
XFuture<Void> firstChunkAck = new XFuture<Void>();
mockStreamWriter.addProcessResponse(firstChunkAck);
XFuture<Void> ackBytePayload3 = mockChannel.sendToSvr(buffers.get(2));
Assert.assertFalse(ackBytePayload3.isDone());
// ack the http chunk packet
firstChunkAck.complete(null);
Assert.assertTrue(ackBytePayload3.isDone());
XFuture<Void> lastChunkAck = new XFuture<Void>();
mockStreamWriter.addProcessResponse(lastChunkAck);
XFuture<Void> ackBytePayload4 = mockChannel.sendToSvr(buffers.get(3));
Assert.assertFalse(ackBytePayload4.isDone());
lastChunkAck.complete(null);
Assert.assertTrue(ackBytePayload4.isDone());
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestHttp11Backpressure method testPostWithChunkingBackpressure.
@Test
public void testPostWithChunkingBackpressure() throws InterruptedException, ExecutionException, TimeoutException {
List<ByteBuffer> buffers = create4SplitPayloads();
XFuture<Void> ackBytePayload1 = mockChannel.sendToSvr(buffers.get(0));
ackBytePayload1.get(2, TimeUnit.SECONDS);
XFuture<StreamWriter> ackRequest = new XFuture<StreamWriter>();
mockListener.addMockStreamToReturn(new MockStreamRef(ackRequest));
XFuture<Void> ackBytePayload2 = mockChannel.sendToSvr(buffers.get(1));
// have to ack TWO...the stream writer AND the first HttpData fed in
Assert.assertFalse(ackBytePayload2.isDone());
XFuture<Void> firstChunkAck1 = new XFuture<Void>();
mockStreamWriter.addProcessResponse(firstChunkAck1);
ackRequest.complete(mockStreamWriter);
Assert.assertFalse(ackBytePayload2.isDone());
firstChunkAck1.complete(null);
Assert.assertTrue(ackBytePayload2.isDone());
// feed the rest of first chunk in and feed part of last chunk
XFuture<Void> firstChunkAck = new XFuture<Void>();
mockStreamWriter.addProcessResponse(firstChunkAck);
XFuture<Void> ackBytePayload3 = mockChannel.sendToSvr(buffers.get(2));
Assert.assertFalse(ackBytePayload3.isDone());
// ack the http chunk packet
firstChunkAck.complete(null);
Assert.assertTrue(ackBytePayload3.isDone());
XFuture<Void> lastChunkAck = new XFuture<Void>();
mockStreamWriter.addProcessResponse(lastChunkAck);
XFuture<Void> ackBytePayload4 = mockChannel.sendToSvr(buffers.get(3));
Assert.assertFalse(ackBytePayload4.isDone());
lastChunkAck.complete(null);
ackBytePayload4.get(2, TimeUnit.SECONDS);
}
Aggregations