Search in sources :

Example 16 with Request

use of org.asynchttpclient.Request in project druid by druid-io.

the class EmitterTest method testFailedEmission.

@Test(timeout = 60_000L)
public void testFailedEmission() throws Exception {
    final UnitEvent event1 = new UnitEvent("test", 1);
    final UnitEvent event2 = new UnitEvent("test", 2);
    emitter = sizeBasedEmitter(1);
    Assert.assertEquals(0, emitter.getTotalEmittedEvents());
    Assert.assertEquals(0, emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount());
    Assert.assertEquals(0, emitter.getFailedSendingTimeCounter().getTimeSumAndCount());
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) {
            Response response = responseBuilder(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST).build();
            return GoHandlers.immediateFuture(response);
        }
    });
    emitter.emit(event1);
    emitter.flush();
    waitForEmission(emitter, 1);
    Assert.assertTrue(httpClient.succeeded());
    // Failed to emit the first event.
    Assert.assertEquals(0, emitter.getTotalEmittedEvents());
    Assert.assertEquals(0, emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount());
    Assert.assertTrue(emitter.getFailedSendingTimeCounter().getTimeSumAndCount() > 0);
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) {
            return GoHandlers.immediateFuture(okResponse());
        }
    }.times(2));
    emitter.emit(event2);
    emitter.flush();
    waitForEmission(emitter, 2);
    closeNoFlush(emitter);
    // Failed event is emitted inside emitter thread, there is no other way to wait for it other than joining the
    // emitterThread
    emitter.joinEmitterThread();
    // Succeed to emit both events.
    Assert.assertEquals(2, emitter.getTotalEmittedEvents());
    Assert.assertTrue(emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount() > 0);
    Assert.assertTrue(emitter.getFailedSendingTimeCounter().getTimeSumAndCount() > 0);
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) ListenableFuture(org.asynchttpclient.ListenableFuture) Test(org.junit.Test)

Example 17 with Request

use of org.asynchttpclient.Request in project druid by druid-io.

the class EmitterTest method testBatchSplitting.

@Test
public void testBatchSplitting() throws Exception {
    final byte[] big = new byte[500 * 1024];
    for (int i = 0; i < big.length; i++) {
        big[i] = 'x';
    }
    final String bigString = StringUtils.fromUtf8(big);
    final List<UnitEvent> events = Arrays.asList(new UnitEvent(bigString, 1), new UnitEvent(bigString, 2), new UnitEvent(bigString, 3), new UnitEvent(bigString, 4));
    final AtomicInteger counter = new AtomicInteger();
    emitter = manualFlushEmitterWithBatchSize(1024 * 1024);
    Assert.assertEquals(0, emitter.getTotalEmittedEvents());
    Assert.assertEquals(0, emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount());
    Assert.assertEquals(0, emitter.getFailedSendingTimeCounter().getTimeSumAndCount());
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) throws JsonProcessingException {
            Assert.assertEquals(TARGET_URL, request.getUrl());
            Assert.assertEquals("application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE));
            Assert.assertEquals(StringUtils.format("[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(counter.getAndIncrement())), JSON_MAPPER.writeValueAsString(events.get(counter.getAndIncrement()))), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString());
            return GoHandlers.immediateFuture(okResponse());
        }
    }.times(3));
    for (UnitEvent event : events) {
        emitter.emit(event);
    }
    waitForEmission(emitter, 1);
    Assert.assertEquals(2, emitter.getTotalEmittedEvents());
    Assert.assertTrue(emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount() > 0);
    Assert.assertEquals(0, emitter.getFailedSendingTimeCounter().getTimeSumAndCount());
    emitter.flush();
    waitForEmission(emitter, 2);
    Assert.assertEquals(4, emitter.getTotalEmittedEvents());
    Assert.assertTrue(emitter.getSuccessfulSendingTimeCounter().getTimeSumAndCount() > 0);
    Assert.assertEquals(0, emitter.getFailedSendingTimeCounter().getTimeSumAndCount());
    closeNoFlush(emitter);
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) Test(org.junit.Test)

Example 18 with Request

use of org.asynchttpclient.Request in project druid by druid-io.

the class EmitterTest method testBasicAuthenticationAndNewlineSeparating.

@Test
public void testBasicAuthenticationAndNewlineSeparating() throws Exception {
    final List<UnitEvent> events = Arrays.asList(new UnitEvent("test", 1), new UnitEvent("test", 2));
    emitter = manualFlushEmitterWithBasicAuthenticationAndNewlineSeparating(new DefaultPasswordProvider("foo:bar"));
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) throws JsonProcessingException {
            Assert.assertEquals(TARGET_URL, request.getUrl());
            Assert.assertEquals("application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE));
            Assert.assertEquals("Basic " + StringUtils.encodeBase64String(StringUtils.toUtf8("foo:bar")), request.getHeaders().get(HttpHeaders.Names.AUTHORIZATION));
            Assert.assertEquals(StringUtils.format("%s\n%s\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1))), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString());
            return GoHandlers.immediateFuture(okResponse());
        }
    }.times(1));
    for (UnitEvent event : events) {
        emitter.emit(event);
    }
    emitter.flush();
    waitForEmission(emitter, 1);
    closeNoFlush(emitter);
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) DefaultPasswordProvider(org.apache.druid.metadata.DefaultPasswordProvider) Test(org.junit.Test)

Example 19 with Request

use of org.asynchttpclient.Request in project druid by druid-io.

the class ParametrizedUriEmitterTest method testEmitterWithMultipleFeeds.

@Test
public void testEmitterWithMultipleFeeds() throws Exception {
    Emitter emitter = parametrizedEmmiter("http://example.com/{feed}");
    final List<UnitEvent> events = Arrays.asList(new UnitEvent("test1", 1), new UnitEvent("test2", 2));
    final Map<String, String> results = new HashMap<>();
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) {
            results.put(request.getUrl(), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString());
            return GoHandlers.immediateFuture(EmitterTest.okResponse());
        }
    }.times(2));
    for (UnitEvent event : events) {
        emitter.emit(event);
    }
    emitter.flush();
    Assert.assertTrue(httpClient.succeeded());
    Map<String, String> expected = ImmutableMap.of("http://example.com/test1", StringUtils.format("[%s]\n", JSON_MAPPER.writeValueAsString(events.get(0))), "http://example.com/test2", StringUtils.format("[%s]\n", JSON_MAPPER.writeValueAsString(events.get(1))));
    Assert.assertEquals(expected, results);
}
Also used : Response(org.asynchttpclient.Response) HashMap(java.util.HashMap) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) Test(org.junit.Test)

Example 20 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class OAuthSignatureCalculatorTest method testSignatureBaseStringWithProperlyEncodedUri.

@Test
public void testSignatureBaseStringWithProperlyEncodedUri() throws NoSuchAlgorithmException {
    Request request = post("http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b").addFormParam("c2", "").addFormParam("a3", "2 q").build();
    testSignatureBaseString(request);
    testSignatureBaseStringWithEncodableOAuthToken(request);
}
Also used : Request(org.asynchttpclient.Request) Test(org.testng.annotations.Test)

Aggregations

Request (org.asynchttpclient.Request)67 Test (org.testng.annotations.Test)32 Response (org.asynchttpclient.Response)31 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)18 RequestBuilder (org.asynchttpclient.RequestBuilder)17 Test (org.junit.Test)16 UnitEvent (org.apache.druid.java.util.emitter.service.UnitEvent)10 HttpRequest (io.netty.handler.codec.http.HttpRequest)8 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)8 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)7 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)6 ListenableFuture (org.asynchttpclient.ListenableFuture)6 Uri (org.asynchttpclient.uri.Uri)5 Realm (org.asynchttpclient.Realm)4 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 HttpResponse (io.netty.handler.codec.http.HttpResponse)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3