use of org.apache.druid.java.util.emitter.service.UnitEvent 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.apache.druid.java.util.emitter.service.UnitEvent in project druid by druid-io.
the class ParametrizedUriEmitterTest method failEmitMalformedEvent.
@Test
public void failEmitMalformedEvent() throws Exception {
Emitter emitter = parametrizedEmmiter("http://example.com/{keyNotSetInEvents}");
Event event = new UnitEvent("test", 1);
httpClient.setGoHandler(GoHandlers.failingHandler());
try {
emitter.emit(event);
emitter.flush();
} catch (IllegalArgumentException e) {
Assert.assertEquals(e.getMessage(), StringUtils.format("ParametrizedUriExtractor with pattern http://example.com/{keyNotSetInEvents} requires keyNotSetInEvents to be set in event, but found %s", event.toMap()));
}
}
use of org.apache.druid.java.util.emitter.service.UnitEvent in project druid by druid-io.
the class EmitterTest method testTimeBasedEmission.
@Test
public void testTimeBasedEmission() throws Exception {
final int timeBetweenEmissions = 100;
emitter = timeBasedEmitter(timeBetweenEmissions);
final CountDownLatch latch = new CountDownLatch(1);
httpClient.setGoHandler(new GoHandler() {
@Override
protected ListenableFuture<Response> go(Request request) {
latch.countDown();
return GoHandlers.immediateFuture(okResponse());
}
}.times(1));
long emitTime = System.currentTimeMillis();
emitter.emit(new UnitEvent("test", 1));
latch.await();
long timeWaited = System.currentTimeMillis() - emitTime;
Assert.assertTrue(StringUtils.format("timeWaited[%s] !< %s", timeWaited, timeBetweenEmissions * 2), timeWaited < timeBetweenEmissions * 2);
waitForEmission(emitter, 1);
final CountDownLatch thisLatch = new CountDownLatch(1);
httpClient.setGoHandler(new GoHandler() {
@Override
protected ListenableFuture<Response> go(Request request) {
thisLatch.countDown();
return GoHandlers.immediateFuture(okResponse());
}
}.times(1));
emitTime = System.currentTimeMillis();
emitter.emit(new UnitEvent("test", 2));
thisLatch.await();
timeWaited = System.currentTimeMillis() - emitTime;
Assert.assertTrue(StringUtils.format("timeWaited[%s] !< %s", timeWaited, timeBetweenEmissions * 2), timeWaited < timeBetweenEmissions * 2);
waitForEmission(emitter, 2);
closeNoFlush(emitter);
Assert.assertTrue("httpClient.succeeded()", httpClient.succeeded());
}
use of org.apache.druid.java.util.emitter.service.UnitEvent in project druid by druid-io.
the class EmitterTest method testSanity.
@Test
public void testSanity() throws Exception {
final List<UnitEvent> events = Arrays.asList(new UnitEvent("test", 1), new UnitEvent("test", 2));
emitter = sizeBasedEmitter(2);
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(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);
}
waitForEmission(emitter, 1);
closeNoFlush(emitter);
Assert.assertTrue(httpClient.succeeded());
}
use of org.apache.druid.java.util.emitter.service.UnitEvent in project druid by druid-io.
the class EmitterTest method testGzipContentEncoding.
@Test
public void testGzipContentEncoding() throws Exception {
final List<UnitEvent> events = Arrays.asList(new UnitEvent("plain-text", 1), new UnitEvent("plain-text", 2));
emitter = sizeBasedEmitterWithContentEncoding(2, ContentEncoding.GZIP);
httpClient.setGoHandler(new GoHandler() {
@Override
protected ListenableFuture<Response> go(Request request) throws IOException {
Assert.assertEquals(TARGET_URL, request.getUrl());
Assert.assertEquals("application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE));
Assert.assertEquals(HttpHeaders.Values.GZIP, request.getHeaders().get(HttpHeaders.Names.CONTENT_ENCODING));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteBuffer data = request.getByteBufferData().slice();
byte[] dataArray = new byte[data.remaining()];
data.get(dataArray);
CompressionUtils.gunzip(new ByteArrayInputStream(dataArray), baos);
Assert.assertEquals(StringUtils.format("[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1))), baos.toString(StandardCharsets.UTF_8.name()));
return GoHandlers.immediateFuture(okResponse());
}
}.times(1));
for (UnitEvent event : events) {
emitter.emit(event);
}
waitForEmission(emitter, 1);
closeNoFlush(emitter);
Assert.assertTrue(httpClient.succeeded());
}
Aggregations