use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestProdRouter method testAsyncRouteAndMocking.
/**
* This test won't work in DevRoute right now as we need to do the following
* 1. create CompileOnDemand very early on
* 2. do a Thread.current().setContextClassLoader(compileOnDemand.getLatestClassloader())
*
* and this all needs to be done BEFORE TestModule is created and more importantly before
* the bind(SomeService.class) as SomeService will be loaded from one classloader and then
* when DEVrouter creates the controller, the compileOnDemand classloader is used resulting
* in a mismatch.
*/
@Test
public void testAsyncRouteAndMocking() {
Http2Request req = RequestCreation.createHttpRequest(HttpMethod.GET, "/async");
// setup returning a response
XFuture<Integer> future1 = new XFuture<Integer>();
overrides.mockService.addToReturn(future1);
MockStreamHandle mockStream = new MockStreamHandle();
StreamRef ref = server.incomingRequest(req, mockStream);
XFuture<StreamWriter> future = ref.getWriter();
Assert.assertFalse(future.isDone());
// no response yet...
Assert.assertNull(mockStream.getLastResponse());
// release controlleer
int id = 78888;
future1.complete(id);
Assert.assertTrue(future.isDone() && !future.isCompletedExceptionally());
Http2Response resp = mockStream.getLastResponse();
Assert.assertNull(resp.getSingleHeaderValue(Http2HeaderName.AUTHORITY));
Assert.assertEquals("http://" + req.getAuthority() + "/meeting/" + id, resp.getSingleHeaderValue(Http2HeaderName.LOCATION));
// We did not send a keep alive so it should close
Assert.assertTrue(mockStream.isWasClosed());
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class JsonController method streaming.
public StreamRef streaming(ResponseStreamHandle handle) {
XFuture<StreamRef> futureStream = new XFuture<>();
XFuture<Boolean> authFuture = svc.authenticate("bobsmith");
XFuture<StreamWriter> writer = authFuture.thenCompose(resp -> {
StreamRef streamRef = client.stream(handle);
futureStream.complete(streamRef);
return streamRef.getWriter();
});
return new StreamRefProxy(writer, futureStream);
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class JsonController method myStream.
// Method signature cannot have RequestContext since in microservices, we implement an api as the server
// AND a client implements the same api AND client does not have a RequestContext!!
@Override
public StreamRef myStream(ResponseStreamHandle handle2) {
RouterStreamHandle handle = (RouterStreamHandle) handle2;
RequestContext requestCtx = Current.getContext();
Http2Response response = handle.createBaseResponse(requestCtx.getRequest().originalRequest, "text/plain", 200, "Ok");
response.setEndOfStream(false);
XFuture<StreamWriter> responseWriter = handle.process(response);
return new RequestStreamEchoWriter(requestCtx, handle, responseWriter);
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestC5x1StreamStates method testSection5_1ReceiveValidFramesAfterSendRstStreamFrame.
/**
* If this state is reached as a result of sending a RST_STREAM frame, the
* peer that receives the RST_STREAM might have already sent — or enqueued for
* sending — frames on the stream that cannot be withdrawn. An endpoint MUST ignore
* frames that it receives on closed streams after it has sent a RST_STREAM frame. An
* endpoint MAY choose to limit the period over which it ignores frames and
* treat frames that arrive after this time as being in error.
* @throws TimeoutException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void testSection5_1ReceiveValidFramesAfterSendRstStreamFrame() throws InterruptedException, ExecutionException, TimeoutException {
MockResponseListener listener1 = new MockResponseListener();
listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(null));
Http2Request request1 = Requests.createRequest();
RequestStreamHandle stream = httpSocket.openStream();
StreamRef streamRef = httpSocket.openStream().process(request1, listener1);
XFuture<StreamWriter> future = streamRef.getWriter();
@SuppressWarnings("unused") StreamWriter writer = future.get(2, TimeUnit.SECONDS);
Http2Msg req = mockChannel.getFrameAndClear();
Assert.assertEquals(request1, req);
RstStreamFrame rst = new RstStreamFrame(request1.getStreamId(), Http2ErrorCode.CANCEL);
XFuture<Void> cancel = streamRef.cancel(rst);
cancel.get(2, TimeUnit.SECONDS);
Http2Msg svrRst = mockChannel.getFrameAndClear();
Assert.assertEquals(rst, svrRst);
// simulate server responding before receiving the cancel
Http2Response resp1 = Requests.createEosResponse(request1.getStreamId());
// endOfStream=true
mockChannel.write(resp1);
// Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
// Assert.assertFalse(mockChannel.isClosed());
//
// Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestC5x1StreamStates method testSection5_1ReceivePriorityAfterReceiveRstStreamFrame.
/**
* An endpoint MUST NOT send frames other than ----PRIORITY---- on a closed stream. An endpoint
* that receives any frame other than ----PRIORITY---- after receiving a ----RST_STREAM---- MUST
* treat that as a stream error (Section 5.4.2) of type STREAM_CLOSED. Similarly, an
* endpoint that receives any frames after receiving a frame with the
* END_STREAM flag set MUST treat that as a connection error (Section 5.4.1) of
* type STREAM_CLOSED, unless the frame is permitted as described below.
*/
@Test
public void testSection5_1ReceivePriorityAfterReceiveRstStreamFrame() {
MockResponseListener listener1 = new MockResponseListener();
listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(null));
Http2Request request = sendRequestToServer(listener1);
sendResetFromServer(listener1, request);
PriorityDetails details = new PriorityDetails();
details.setStreamDependency(3);
PriorityFrame dataFrame = new PriorityFrame(request.getStreamId(), details);
mockChannel.write(dataFrame);
// priority is ignored
Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
Assert.assertFalse(mockChannel.isClosed());
Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
}
Aggregations