Search in sources :

Example 16 with CountDownLatch

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());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) CountDownLatch(java.util.concurrent.CountDownLatch) RuntimeCamelException(org.apache.camel.RuntimeCamelException) SynchronizationAdapter(org.apache.camel.support.SynchronizationAdapter)

Example 17 with CountDownLatch

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));
}
Also used : Path(org.apache.hadoop.fs.Path) Processor(org.apache.camel.Processor) Configuration(org.apache.hadoop.conf.Configuration) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CountDownLatch(java.util.concurrent.CountDownLatch) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) FileSystem(org.apache.hadoop.fs.FileSystem) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with CountDownLatch

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);
}
Also used : Stopwatch(com.google.common.base.Stopwatch) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteQueueEndpoint(org.apache.camel.component.ignite.queue.IgniteQueueEndpoint) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 19 with CountDownLatch

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);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) JaxbDataFormat(org.apache.camel.converter.jaxb.JaxbDataFormat) Test(org.junit.Test)

Example 20 with CountDownLatch

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);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)5355 Test (org.junit.Test)2594 IOException (java.io.IOException)631 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)550 AtomicReference (java.util.concurrent.atomic.AtomicReference)501 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)475 ArrayList (java.util.ArrayList)471 QuickTest (com.hazelcast.test.annotation.QuickTest)375 ParallelTest (com.hazelcast.test.annotation.ParallelTest)355 ExecutorService (java.util.concurrent.ExecutorService)322 Test (org.testng.annotations.Test)310 HazelcastInstance (com.hazelcast.core.HazelcastInstance)251 List (java.util.List)212 HashMap (java.util.HashMap)207 HttpServletResponse (javax.servlet.http.HttpServletResponse)207 ExecutionException (java.util.concurrent.ExecutionException)203 HttpServletRequest (javax.servlet.http.HttpServletRequest)189 Ignite (org.apache.ignite.Ignite)188 ServletException (javax.servlet.ServletException)183 TimeoutException (java.util.concurrent.TimeoutException)168