Search in sources :

Example 51 with XFuture

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

the class TestBackpressure method testBasicBackpressureChunked.

@Test
public void testBasicBackpressureChunked() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = httpSocket.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.getFrameAndClear());
    List<ByteBuffer> buffers = create4BuffersWith3Messags();
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // consume since not enough data for client
    Assert.assertTrue(fut1.isDone());
    XFuture<StreamWriter> future = new XFuture<StreamWriter>();
    listener.addReturnValueIncomingResponse(future);
    XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
    // not resolved yet since client only has part of the data
    Assert.assertFalse(fut2.isDone());
    MockStreamWriter mockWriter = new MockStreamWriter();
    // This releases the response msg acking 10 bytes
    future.complete(mockWriter);
    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);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) ByteBuffer(java.nio.ByteBuffer) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) Test(org.junit.Test) AbstractTest(org.webpieces.http2client.AbstractTest)

Example 52 with XFuture

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

the class Level4PreconditionChecks method createExcepted.

public XFuture<Void> createExcepted(Http2Msg payload, String extra, ConnectionCancelled closedReason) {
    log.info("returning XFuture.exception since this socket is closed('" + extra + "' frame=" + payload + "):" + closedReason.getReasonCode());
    XFuture<Void> future = new XFuture<>();
    ConnectionClosedException exception = new ConnectionClosedException(closedReason, "Connection closed or closing:" + closedReason.getReasonCode());
    if (closedReason instanceof ConnectionFailure) {
        ConnectionFailure fail = (ConnectionFailure) closedReason;
        exception.initCause(fail.getCause());
    }
    future.completeExceptionally(exception);
    return future;
}
Also used : ConnectionFailure(com.webpieces.http2engine.api.error.ConnectionFailure) XFuture(org.webpieces.util.futures.XFuture) ConnectionClosedException(com.webpieces.http2engine.api.error.ConnectionClosedException)

Example 53 with XFuture

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

the class Level7MarshalAndPing method sendPing.

// public XFuture<Void> sendControlFrameToClient(Http2Msg msg) {
// return finalLayer.sendControlFrameToClient(msg);
// }
public XFuture<Void> sendPing() {
    PingFrame ping = new PingFrame();
    ping.setOpaqueData(8L);
    XFuture<Void> newFuture = new XFuture<>();
    boolean wasSet = pingFutureRef.compareAndSet(null, newFuture);
    if (!wasSet) {
        throw new IllegalStateException(key + "You must wait until the first ping you sent is complete.  2nd ping=" + ping);
    }
    return sendFrameToSocket(ping).thenCompose(c -> newFuture);
}
Also used : PingFrame(com.webpieces.http2.api.dto.lowlevel.PingFrame) XFuture(org.webpieces.util.futures.XFuture)

Example 54 with XFuture

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

the class AsyncSSLEngine3Impl method doHandshakeLoop.

private XFuture<Void> doHandshakeLoop() {
    SSLEngine engine = mem.getEngine();
    HandshakeStatus hsStatus = engine.getHandshakeStatus();
    List<XFuture<Void>> futures = new ArrayList<>();
    while ((hsStatus == HandshakeStatus.NEED_WRAP && !sslEngineIsFarting) || hsStatus == HandshakeStatus.NEED_TASK) {
        XFuture<Void> future = doHandshakeWork();
        futures.add(future);
        hsStatus = engine.getHandshakeStatus();
    }
    sslEngineIsFarting = false;
    XFuture<Void> futureAll = XFuture.allOf(futures.toArray(new XFuture[futures.size()]));
    return futureAll;
}
Also used : XFuture(org.webpieces.util.futures.XFuture) SSLEngine(javax.net.ssl.SSLEngine) AsyncSSLEngine(org.webpieces.ssl.api.AsyncSSLEngine) ArrayList(java.util.ArrayList) HandshakeStatus(javax.net.ssl.SSLEngineResult.HandshakeStatus)

Example 55 with XFuture

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

the class Http2ChannelCache method write.

@SuppressWarnings("unchecked")
@Override
public XFuture<Void> write(ByteBuffer b) {
    DataWrapper data = dataGen.wrapByteBuffer(b);
    parser.unmarshal(unmarshalState, data);
    List<Http2Msg> parsedFrames = unmarshalState.getParsedFrames();
    return (XFuture<Void>) super.calledMethod(Method.INCOMING_FRAME, parsedFrames);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) XFuture(org.webpieces.util.futures.XFuture) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)

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