Search in sources :

Example 86 with Response

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;
}
Also used : Response(org.asynchttpclient.Response) PubSubMessage(com.yahoo.bullet.pubsub.PubSubMessage) ArrayList(java.util.ArrayList) PubSubException(com.yahoo.bullet.pubsub.PubSubException) IOException(java.io.IOException)

Example 87 with Response

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());
}
Also used : Response(org.asynchttpclient.Response) PubSubMessage(com.yahoo.bullet.pubsub.PubSubMessage) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 88 with Response

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());
}
Also used : Response(org.asynchttpclient.Response) PubSubMessage(com.yahoo.bullet.pubsub.PubSubMessage) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 89 with Response

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());
}
Also used : Response(org.asynchttpclient.Response) PubSubMessage(com.yahoo.bullet.pubsub.PubSubMessage) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 90 with Response

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;
}
Also used : Response(org.asynchttpclient.Response) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) ListenableFuture(org.asynchttpclient.ListenableFuture) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Aggregations

Response (org.asynchttpclient.Response)142 Test (org.testng.annotations.Test)85 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)79 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)55 HttpServletResponse (javax.servlet.http.HttpServletResponse)42 BoundRequestBuilder (org.asynchttpclient.BoundRequestBuilder)32 Request (org.asynchttpclient.Request)29 IOException (java.io.IOException)21 Test (org.junit.Test)20 ExecutionException (java.util.concurrent.ExecutionException)16 PubSubMessage (com.yahoo.bullet.pubsub.PubSubMessage)14 RESTPubSubTest.getOkResponse (com.yahoo.bullet.pubsub.rest.RESTPubSubTest.getOkResponse)11 TimeoutException (java.util.concurrent.TimeoutException)11 RequestBuilder (org.asynchttpclient.RequestBuilder)11 UnitEvent (org.apache.druid.java.util.emitter.service.UnitEvent)10 RESTPubSubTest.getNotOkResponse (com.yahoo.bullet.pubsub.rest.RESTPubSubTest.getNotOkResponse)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 Map (java.util.Map)9 AuthorizationException (org.apache.shiro.authz.AuthorizationException)9 TestUtils.createTempFile (org.asynchttpclient.test.TestUtils.createTempFile)9