Updated Branches: refs/heads/camel-2.11.x e3db2228a -> b1bfa17af
Added a test based on a user-forum issue. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b1bfa17a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b1bfa17a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b1bfa17a Branch: refs/heads/camel-2.11.x Commit: b1bfa17af10ba4441f511525f9e6a7e9f638b03a Parents: e3db222 Author: Babak Vahdat <bvah...@apache.org> Authored: Wed Aug 14 23:43:55 2013 +0200 Committer: Babak Vahdat <bvah...@apache.org> Committed: Wed Aug 14 23:47:38 2013 +0200 ---------------------------------------------------------------------- .../NettyMultipleSimultaneousClientsTest.java | 96 ++++++++++++++++++++ 1 file changed, 96 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/b1bfa17a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyMultipleSimultaneousClientsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyMultipleSimultaneousClientsTest.java b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyMultipleSimultaneousClientsTest.java new file mode 100644 index 0000000..c55b0a2 --- /dev/null +++ b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyMultipleSimultaneousClientsTest.java @@ -0,0 +1,96 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.netty; + +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +/** + * + */ +public class NettyMultipleSimultaneousClientsTest extends BaseNettyTest { + + private String uri = "netty:tcp://localhost:{{port}}?sync=true&reuseAddress=true&synchronous=false"; + private int clientCount = 20; + private CountDownLatch startLatch = new CountDownLatch(1); + private CountDownLatch finishLatch = new CountDownLatch(clientCount); + + @Test + public void testSimultaneousClients() throws Exception { + ExecutorService executorService = Executors.newFixedThreadPool(clientCount); + Future<?>[] replies = new Future[clientCount]; + + for (int i = 0; i < clientCount; i++) { + replies[i] = executorService.submit(new Callable<Object>() { + @Override + public Object call() throws Exception { + // wait until we're permitted to start + startLatch.await(); + + Object reply = template.requestBody(uri, "World"); + + // signal that we're now done + finishLatch.countDown(); + + return reply; + } + }); + } + + Object[] expectedReplies = new Object[clientCount]; + for (int i = 0; i < clientCount; i++) { + expectedReplies[i] = "Bye World"; + } + getMockEndpoint("mock:result").expectedMessageCount(clientCount); + getMockEndpoint("mock:result").expectedBodiesReceived(expectedReplies); + + // fire the simultaneous client calls + startLatch.countDown(); + + // and wait long enough until they're all done + assertTrue("Waiting on the latch ended up with a timeout!", finishLatch.await(5, TimeUnit.SECONDS)); + + // shutdown the thread pool as the finishLatch above has already guaranteed the completion of all the tasks + executorService.shutdown(); + + // assert on what we expect to receive + for (int i = 0; i < clientCount; i++) { + assertEquals("Bye World", replies[i].get()); + } + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from(uri) + .log("${body}") + .transform(body().prepend("Bye ")) + .to("mock:result"); + } + }; + } +}