use of org.asynchttpclient.Response in project bullet-core by yahoo.
the class RESTSubscriber method getMessages.
@Override
public List<PubSubMessage> getMessages() throws PubSubException {
List<PubSubMessage> messages = new ArrayList<>();
long currentTime = System.currentTimeMillis();
if (currentTime - lastRequest <= minWait) {
return messages;
}
lastRequest = currentTime;
for (String url : urls) {
try {
log.debug("Getting messages from url: ", url);
Response response = client.prepareGet(url).execute().get();
int statusCode = response.getStatusCode();
if (statusCode == RESTPubSub.OK_200) {
messages.add(PubSubMessage.fromJSON(response.getResponseBody()));
} else if (statusCode != RESTPubSub.NO_CONTENT_204) {
// NO_CONTENT_204 indicates there are no new messages - anything else indicates a problem
log.error("Http call failed with status code {} and response {}.", statusCode, response);
}
} catch (Exception e) {
log.error("Http call failed with error: ", e);
}
}
return messages;
}
use of org.asynchttpclient.Response in project bullet-storm by yahoo.
the class DRPCQueryResultPubscriberTest method testReadingOkResponse.
@Test(timeOut = 5000L)
public void testReadingOkResponse() throws Exception {
PubSubMessage expected = new PubSubMessage("foo", "response");
CompletableFuture<Response> response = getOkFuture(getOkResponse(expected.asJSON()));
AsyncHttpClient mockClient = mockClientWith(response);
pubscriber.setClient(mockClient);
pubscriber.send(new PubSubMessage("foo", "bar"));
// This is async (but practically still very fast since we mocked the response), so need a timeout.
PubSubMessage actual = fetchAsync().get();
Assert.assertNotNull(actual);
Assert.assertEquals(actual.getId(), expected.getId());
Assert.assertEquals(actual.getContent(), expected.getContent());
}
use of org.asynchttpclient.Response in project bullet-storm by yahoo.
the class DRPCQueryResultPubscriberTest method testReadingNullResponse.
@Test(timeOut = 5000L)
public void testReadingNullResponse() throws Exception {
CompletableFuture<Response> response = getOkFuture(null);
AsyncHttpClient mockClient = mockClientWith(response);
pubscriber.setClient(mockClient);
pubscriber.send(new PubSubMessage("foo", "bar"));
// This is async (but practically still very fast since we mocked the response), so need a timeout.
PubSubMessage actual = fetchAsync().get();
Assert.assertNotNull(actual);
Assert.assertEquals(actual.getId(), "foo");
Assert.assertEquals(actual.getContent(), CANNOT_REACH_DRPC.asJSONClip());
}
use of org.asynchttpclient.Response in project bullet-storm by yahoo.
the class DRPCQueryResultPubscriberTest method testReadingNotOkResponse.
@Test(timeOut = 5000L)
public void testReadingNotOkResponse() throws Exception {
CompletableFuture<Response> response = getOkFuture(getNotOkResponse(500));
AsyncHttpClient mockClient = mockClientWith(response);
pubscriber.setClient(mockClient);
pubscriber.send(new PubSubMessage("foo", "bar"));
// This is async (but practically still very fast since we mocked the response), so need a timeout.
PubSubMessage actual = fetchAsync().get();
Assert.assertNotNull(actual);
Assert.assertEquals(actual.getId(), "foo");
Assert.assertEquals(actual.getContent(), CANNOT_REACH_DRPC.asJSONClip());
}
use of org.asynchttpclient.Response in project bullet-storm by yahoo.
the class DRPCQueryResultPubscriberTest method mockClientWith.
private AsyncHttpClient mockClientWith(CompletableFuture<Response> future) {
ListenableFuture<Response> mockListenable = (ListenableFuture<Response>) mock(ListenableFuture.class);
doReturn(future).when(mockListenable).toCompletableFuture();
BoundRequestBuilder mockBuilder = mock(BoundRequestBuilder.class);
doReturn(mockListenable).when(mockBuilder).execute();
// Return itself
doReturn(mockBuilder).when(mockBuilder).setBody(anyString());
AsyncHttpClient mockClient = mock(AsyncHttpClient.class);
doReturn(mockBuilder).when(mockClient).preparePost(anyString());
return mockClient;
}
Aggregations