use of java.util.concurrent.CountDownLatch in project camel by apache.
the class DefaultProducerTemplateAsyncTest method testAsyncCallbackInOutProcessor.
public void testAsyncCallbackInOutProcessor() throws Exception {
ORDER.set(0);
final CountDownLatch latch = new CountDownLatch(1);
template.asyncCallback("direct:echo", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody("Hello");
exchange.setPattern(ExchangePattern.InOut);
}
}, new SynchronizationAdapter() {
@Override
public void onDone(Exchange exchange) {
assertEquals("HelloHello", exchange.getOut().getBody());
ORDER.addAndGet(2);
latch.countDown();
}
});
ORDER.addAndGet(1);
assertTrue(latch.await(10, TimeUnit.SECONDS));
ORDER.addAndGet(4);
assertEquals(7, ORDER.get());
}
use of java.util.concurrent.CountDownLatch in project camel by apache.
the class HdfsProducerConsumerIntegrationTest method testMultipleConsumers.
@Test
public // see https://issues.apache.org/jira/browse/CAMEL-7318
void testMultipleConsumers() throws Exception {
Path p = new Path("hdfs://localhost:9000/tmp/test/multiple-consumers");
FileSystem fs = FileSystem.get(p.toUri(), new Configuration());
fs.mkdirs(p);
for (int i = 1; i <= ITERATIONS; i++) {
FSDataOutputStream os = fs.create(new Path(p, String.format("file-%03d.txt", i)));
os.write(String.format("hello (%03d)\n", i).getBytes());
os.close();
}
final Set<String> fileNames = new HashSet<String>();
final CountDownLatch latch = new CountDownLatch(ITERATIONS);
MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
resultEndpoint.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
fileNames.add(exchange.getIn().getHeader(Exchange.FILE_NAME, String.class));
latch.countDown();
}
});
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
// difference in chunkSize only to allow multiple consumers
from("hdfs2://localhost:9000/tmp/test/multiple-consumers?pattern=*.txt&fileSystemType=HDFS&chunkSize=128").to("mock:result");
from("hdfs2://localhost:9000/tmp/test/multiple-consumers?pattern=*.txt&fileSystemType=HDFS&chunkSize=256").to("mock:result");
from("hdfs2://localhost:9000/tmp/test/multiple-consumers?pattern=*.txt&fileSystemType=HDFS&chunkSize=512").to("mock:result");
from("hdfs2://localhost:9000/tmp/test/multiple-consumers?pattern=*.txt&fileSystemType=HDFS&chunkSize=1024").to("mock:result");
}
});
context.start();
resultEndpoint.expectedMessageCount(ITERATIONS);
latch.await(30, TimeUnit.SECONDS);
resultEndpoint.assertIsSatisfied();
assertThat(fileNames.size(), equalTo(ITERATIONS));
}
use of java.util.concurrent.CountDownLatch in project camel by apache.
the class IgniteQueueTest method testBoundedQueueAndOtherOperations.
@Test
public void testBoundedQueueAndOtherOperations() throws Exception {
List<String> list = Lists.newArrayList();
// Fill data.
for (int i = 0; i < 100; i++) {
template.requestBody("ignite:queue:def?operation=ADD&capacity=100", "hello" + i);
list.add("hello" + i);
}
// NOTE: Unfortunately the behaviour of IgniteQueue doesn't adhere to the overridden ADD method. It should return an Exception.
assert_().that(template.requestBody("ignite:queue:def?operation=ADD&capacity=100", "hello101", boolean.class)).isFalse();
assert_().that(template.requestBody("ignite:queue:def?operation=OFFER&capacity=100", "hello101", boolean.class)).isFalse();
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
assert_().that(template.requestBody("ignite:queue:def?operation=PUT&capacity=100", "hello101", boolean.class)).isFalse();
latch.countDown();
}
});
t.start();
// Wait 2 seconds and check that the thread was blocked.
assert_().that(latch.await(2000, TimeUnit.MILLISECONDS)).isFalse();
t.interrupt();
// PEEK and ELEMENT.
assert_().that(template.requestBody("ignite:queue:def?operation=PEEK&capacity=100", null, String.class)).isEqualTo("hello0");
assert_().that(template.requestBody("ignite:queue:def?operation=ELEMENT&capacity=100", null, String.class)).isEqualTo("hello0");
// TAKE.
assert_().that(template.requestBody("ignite:queue:def?operation=TAKE&capacity=100", null, String.class)).isEqualTo("hello0");
assert_().that(template.requestBody("ignite:queue:def?operation=SIZE&capacity=100", null, int.class)).isEqualTo(99);
// Now drain.
assert_().that(template.requestBody("ignite:queue:def?operation=DRAIN&capacity=100", null, String[].class)).asList().hasSize(99);
assert_().that(template.requestBody("ignite:queue:def?operation=SIZE&capacity=100", null, int.class)).isEqualTo(0);
assert_().that(template.requestBody("ignite:queue:def?operation=POLL&capacity=100", null, String.class)).isNull();
// TAKE.
t = new Thread(new Runnable() {
@Override
public void run() {
assert_().that(template.requestBody("ignite:queue:def?operation=TAKE&capacity=100", null, String.class)).isEqualTo("hello102");
latch.countDown();
}
});
t.start();
// Element was returned.
assert_().that(template.requestBody("ignite:queue:def?operation=ADD&capacity=100", "hello102", boolean.class)).isTrue();
assert_().that(latch.await(1000, TimeUnit.MILLISECONDS)).isTrue();
// POLL with a timeout.
assert_().that(Executors.newSingleThreadExecutor().submit(new Callable<Long>() {
@Override
public Long call() throws Exception {
Stopwatch sw = Stopwatch.createStarted();
assert_().that(template.requestBody("ignite:queue:def?operation=POLL&timeoutMillis=1000&capacity=100", null, String.class)).isNull();
return sw.elapsed(TimeUnit.MILLISECONDS);
}
}).get()).isAtLeast(1000L);
}
use of java.util.concurrent.CountDownLatch in project camel by apache.
the class DataFormatConcurrentTest method testMarshallConcurrent.
@Test
public void testMarshallConcurrent() throws Exception {
template.setDefaultEndpointUri("direct:marshal");
final CountDownLatch latch = new CountDownLatch(warmupCount + testCycleCount);
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:marshal").marshal(new JaxbDataFormat("org.apache.camel.example")).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
latch.countDown();
}
});
}
});
marshal(latch);
}
use of java.util.concurrent.CountDownLatch in project camel by apache.
the class DataFormatConcurrentTest method testUnmarshalFallbackConcurrent.
@Test
public void testUnmarshalFallbackConcurrent() throws Exception {
template.setDefaultEndpointUri("direct:unmarshalFallback");
final CountDownLatch latch = new CountDownLatch(warmupCount + testCycleCount);
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:unmarshalFallback").convertBodyTo(Foo.class).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
latch.countDown();
}
});
}
});
unmarshal(latch);
}
Aggregations