Search in sources :

Example 1 with XFuture

use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.

the class ResponseCacher method run.

public XFuture<FullResponse> run(FullRequest request) {
    SingleResponseListener responseListener = new SingleResponseListener();
    RequestStreamHandle streamHandle = openStreamFunc.get();
    Http2Request req = request.getHeaders();
    if (request.getPayload() == null) {
        request.getHeaders().setEndOfStream(true);
        streamHandle.process(req, responseListener);
        return responseListener.fetchResponseFuture();
    } else if (request.getTrailingHeaders() == null) {
        request.getHeaders().setEndOfStream(false);
        DataFrame data = createData(request, true);
        StreamRef streamRef = streamHandle.process(request.getHeaders(), responseListener);
        return streamRef.getWriter().thenCompose(writer -> {
            data.setStreamId(req.getStreamId());
            return writer.processPiece(data);
        }).thenCompose(writer -> responseListener.fetchResponseFuture());
    }
    request.getHeaders().setEndOfStream(false);
    DataFrame data = createData(request, false);
    Http2Trailers trailers = request.getTrailingHeaders();
    trailers.setEndOfStream(true);
    StreamRef streamRef = streamHandle.process(request.getHeaders(), responseListener);
    return streamRef.getWriter().thenCompose(writer -> writeStuff(writer, req, data, trailers, responseListener));
}
Also used : FullRequest(org.webpieces.http2client.api.dto.FullRequest) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) XFuture(org.webpieces.util.futures.XFuture) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers) StreamRef(com.webpieces.http2.api.streaming.StreamRef) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataWrapper(org.webpieces.data.api.DataWrapper) FullResponse(org.webpieces.http2client.api.dto.FullResponse) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Supplier(java.util.function.Supplier) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame)

Example 2 with XFuture

use of org.webpieces.util.futures.XFuture 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());
}
Also used : StreamMsg(com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg) MockChannel(org.webpieces.webserver.test.sockets.MockChannel) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) ByteBuffer(java.nio.ByteBuffer) DataWrapper(org.webpieces.data.api.DataWrapper) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) DataListener(org.webpieces.nio.api.handlers.DataListener) AbstractWebpiecesTest(org.webpieces.webserver.test.sockets.AbstractWebpiecesTest) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 3 with XFuture

use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.

the class HttpsJsonClientInvokeHandler method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    TestCaseRecorder recorder = (TestCaseRecorder) Context.get(RECORDER_KEY);
    EndpointInfo recordingInfo = null;
    if (recorder != null) {
        Map<String, Object> ctxSnapshot = Context.copyContext();
        recordingInfo = new EndpointInfo(method, args, ctxSnapshot);
        recorder.addEndpointInfo(recordingInfo);
    }
    clientAssertions.throwIfCannotGoRemote();
    Path annotation = method.getAnnotation(Path.class);
    if (annotation == null) {
        throw new IllegalArgumentException("The @Path annotation is missing from method=" + method + " clazz=" + method.getDeclaringClass());
    }
    String path = getPath(method);
    HttpMethod httpMethod = getHttpMethod(method);
    Class<?> clazz = method.getReturnType();
    if (!(CompletableFuture.class.isAssignableFrom(clazz))) {
        throw new IllegalStateException("All api methods must return a XFuture");
    }
    ParameterizedType t = (ParameterizedType) method.getGenericReturnType();
    Class retType = (Class) t.getActualTypeArguments()[0];
    Map<String, Object> context = Context.getContext();
    if (context == null) {
        throw new IllegalStateException("Please call Current.setContext with the request context or create your own context");
    }
    Object header = context.get(Context.HEADERS);
    if (header == null) {
        throw new IllegalStateException("Context.HEADERS is missing from the Context.  please set that up first");
    } else if (!(header instanceof Map)) {
        throw new IllegalStateException("Context.HEADERS is not a Map<String, String> and is setup incorrectly");
    }
    log.info("Sending http request to: " + addr.getHostName() + ":" + addr.getPort() + path);
    Object body = args[0];
    if (hasUrlParams) {
        path = transformPath(path, method, args);
        body = findBody(method, args);
    }
    Endpoint endpoint = new Endpoint(addr, httpMethod.getCode(), path);
    XFuture<Object> xFuture = clientHelper.sendHttpRequest(method, body, endpoint, retType).thenApply(retVal -> {
        // Only needed by APIs/methods that return CompletableFuture :( not XFuture
        Context.restoreContext(context);
        return retVal;
    });
    if (recorder == null) {
        return xFuture;
    }
    EndpointInfo finalInfo = recordingInfo;
    return xFuture.handle((resp, exc1) -> addTestRecordingInfo(finalInfo, resp, exc1)).thenCompose(Function.identity());
}
Also used : Logger(org.slf4j.Logger) Context(org.webpieces.util.context.Context) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) Function(java.util.function.Function) TestCaseRecorder(org.webpieces.microsvc.impl.TestCaseRecorder) Inject(javax.inject.Inject) HttpMethod(org.webpieces.ctx.api.HttpMethod) XFuture(org.webpieces.util.futures.XFuture) javax.ws.rs(javax.ws.rs) ParameterizedType(java.lang.reflect.ParameterizedType) Current(org.checkerframework.checker.units.qual.Current) ClientAssertions(org.webpieces.util.context.ClientAssertions) Parameter(java.lang.reflect.Parameter) RECORDER_KEY(org.webpieces.microsvc.impl.TestCaseRecorder.RECORDER_KEY) Map(java.util.Map) EndpointInfo(org.webpieces.microsvc.impl.EndpointInfo) InvocationHandler(java.lang.reflect.InvocationHandler) Pattern(java.util.regex.Pattern) Method(java.lang.reflect.Method) TestCaseRecorder(org.webpieces.microsvc.impl.TestCaseRecorder) ParameterizedType(java.lang.reflect.ParameterizedType) EndpointInfo(org.webpieces.microsvc.impl.EndpointInfo) Map(java.util.Map) HttpMethod(org.webpieces.ctx.api.HttpMethod)

Example 4 with XFuture

use of org.webpieces.util.futures.XFuture 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());
}
Also used : XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) ByteBuffer(java.nio.ByteBuffer) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) Test(org.junit.Test)

Example 5 with XFuture

use of org.webpieces.util.futures.XFuture 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);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) ByteBuffer(java.nio.ByteBuffer) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) Test(org.junit.Test)

Aggregations

XFuture (org.webpieces.util.futures.XFuture)71 Test (org.junit.Test)21 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)20 ByteBuffer (java.nio.ByteBuffer)16 Logger (org.slf4j.Logger)15 LoggerFactory (org.slf4j.LoggerFactory)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Map (java.util.Map)12 DataWrapper (org.webpieces.data.api.DataWrapper)12 HttpFullRequest (org.webpieces.httpclient11.api.HttpFullRequest)12 HttpFullResponse (org.webpieces.httpclient11.api.HttpFullResponse)12 NotFoundException (org.webpieces.http.exception.NotFoundException)11 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)11 ResponseWrapper (org.webpieces.webserver.test.ResponseWrapper)11 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)10 PrivateWebserverForTest (org.webpieces.webserver.PrivateWebserverForTest)10 StreamRef (com.webpieces.http2.api.streaming.StreamRef)9 RequestContext (org.webpieces.ctx.api.RequestContext)9 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)8