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);
}
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);
}
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());
}
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());
}
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));
}
Aggregations