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