Search in sources :

Example 11 with XFuture

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

the class TestHttp1Backpressure method testBasicBackpressure.

@Test
public void testBasicBackpressure() 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.createResponse(1, 250);
    HttpData response2 = Requests.createHttpData(250);
    List<ByteBuffer> buffers = create3BuffersWithTwoMessags(response1, response2);
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
    listener.addProcessResponse(requestFuture);
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // resolved since data is cached in parser (client doesn't receive this data)
    Assert.assertTrue(fut1.isDone());
    // This next one is confusing BUT in http1.1 parsing terms, data is data for content length so this results
    // in a full HttpData packet actually...
    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();
    XFuture<Void> streamWriterFuture = new XFuture<Void>();
    mockWriter.addProcessResponse(streamWriterFuture);
    // This releases BOTH packets above to be processed!!!(not just the one)
    requestFuture.complete(mockWriter);
    Assert.assertFalse(fut2.isDone());
    streamWriterFuture.complete(null);
    // NOW it's resolved
    fut2.get(2, TimeUnit.SECONDS);
    // feed the last buffer in
    XFuture<Void> future3 = new XFuture<Void>();
    mockWriter.addProcessResponse(future3);
    XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
    Assert.assertFalse(fut3.isDone());
    future3.complete(null);
    fut3.get(2, TimeUnit.SECONDS);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.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) HttpData(org.webpieces.httpparser.api.dto.HttpData) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) Test(org.junit.Test)

Example 12 with XFuture

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

the class MockChannel method write.

@SuppressWarnings("unchecked")
@Override
public XFuture<Void> write(ByteBuffer b) {
    DataWrapper wrapper = dataGen.wrapByteBuffer(b);
    List<HttpPayload> parsedData = parser.parse(wrapper);
    if (parsedData.size() != 1)
        throw new IllegalArgumentException("The impl should be writing out full single payloads each write call");
    HttpPayload payload = parsedData.get(0);
    Http2Msg http2 = Http11ToHttp2.translate(payload, false);
    return (XFuture<Void>) super.calledMethod(Method.WRITE, http2);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) XFuture(org.webpieces.util.futures.XFuture) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)

Example 13 with XFuture

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

the class TestConnecting method testConnecting.

@Test
public void testConnecting() {
    mockChannelMgr.addTCPChannelToReturn(mockChannel);
    Http2Socket socket = httpClient.createHttpSocket(new Http2CloseListener());
    XFuture<Void> future1 = new XFuture<Void>();
    mockChannel.setConnectFuture(future1);
    XFuture<Void> future = socket.connect(new InetSocketAddress(8080));
    Assert.assertFalse(future.isDone());
    future1.complete(null);
    Assert.assertTrue(future.isDone());
}
Also used : Http2CloseListener(org.webpieces.httpclient.Http2CloseListener) XFuture(org.webpieces.util.futures.XFuture) InetSocketAddress(java.net.InetSocketAddress) Http2Socket(org.webpieces.http2client.api.Http2Socket) Test(org.junit.Test)

Example 14 with XFuture

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

the class ElasticClient method performRequest.

public XFuture<Response> performRequest(String method, String endpoint, Map<String, String> params, Object jsonObj, Header... headers) {
    HttpEntity entity = null;
    String jsonString = null;
    if (jsonObj != null) {
        try {
            jsonString = objectMapper.writeValueAsString(jsonObj);
        } catch (IOException e) {
            throw new RuntimeException("failed to translate to json object to string: " + jsonObj, e);
        }
        entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
    }
    String jsonStr = jsonString;
    XFuture<Response> future = new XFuture<Response>();
    ResponseListener responseListener = new ToFutureListener(future);
    RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
    Header[] allHeaders = new Header[headers.length + 1];
    for (Header header : headers) {
        builder.addHeader(header.getName(), header.getValue());
    }
    builder.addHeader("Authorization", "ApiKey V0xKeEdIRUIyMGdKdjF0QlZoWmc6X0NvaU9seGZSZHFxd283SjIyYXhvdw==");
    Request requst = new Request(method, endpoint);
    requst.setEntity(entity);
    requst.setOptions(builder);
    requst.addParameters(params);
    client.performRequestAsync(requst, responseListener);
    return future.handle((r, e) -> {
        if (e != null) {
            XFuture<Response> f = new XFuture<Response>();
            f.completeExceptionally(new RuntimeException("json failed to be processed by elastic search=" + jsonStr, e));
            return f;
        }
        return XFuture.completedFuture(r);
    }).thenCompose(Function.identity());
}
Also used : AtomicActionList(org.webpieces.elasticsearch.actions.AtomicActionList) Logger(org.slf4j.Logger) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) LoggerFactory(org.slf4j.LoggerFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Singleton(javax.inject.Singleton) org.elasticsearch.client(org.elasticsearch.client) Function(java.util.function.Function) Header(org.apache.http.Header) ArrayList(java.util.ArrayList) AliasChange(org.webpieces.elasticsearch.actions.AliasChange) List(java.util.List) ElasticIndex(org.webpieces.elasticsearch.mapping.ElasticIndex) XFuture(org.webpieces.util.futures.XFuture) Map(java.util.Map) Action(org.webpieces.elasticsearch.actions.Action) NStringEntity(org.apache.http.nio.entity.NStringEntity) Collections(java.util.Collections) HttpHost(org.apache.http.HttpHost) HttpEntity(org.apache.http.HttpEntity) XFuture(org.webpieces.util.futures.XFuture) IOException(java.io.IOException) NStringEntity(org.apache.http.nio.entity.NStringEntity) Header(org.apache.http.Header)

Example 15 with XFuture

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

the class HibernateAsyncController method save.

public XFuture<Redirect> save() {
    EntityManager mgr = Em.get();
    XFuture<Integer> future = new XFuture<Integer>();
    kickOffAsyncResponse(future);
    return future.thenApply(intVal -> runSave(mgr));
}
Also used : EntityManager(javax.persistence.EntityManager) XFuture(org.webpieces.util.futures.XFuture)

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