You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/09/24 13:49:21 UTC

svn commit: r818451 - in /camel/trunk/camel-core/src/test/java/org/apache/camel/language: XPathAnotherRouteConcurrentTest.java XPathRouteConcurrentTest.java

Author: davsclaus
Date: Thu Sep 24 11:49:20 2009
New Revision: 818451

URL: http://svn.apache.org/viewvc?rev=818451&view=rev
Log:
CAMEL-1930: Added unit test for concurrency.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathAnotherRouteConcurrentTest.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathRouteConcurrentTest.java   (with props)

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathAnotherRouteConcurrentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathAnotherRouteConcurrentTest.java?rev=818451&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathAnotherRouteConcurrentTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathAnotherRouteConcurrentTest.java Thu Sep 24 11:49:20 2009
@@ -0,0 +1,97 @@
+/**
+ * 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.language;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version $Revision$
+ */
+public class XPathAnotherRouteConcurrentTest extends ContextTestSupport {
+
+    public void testConcurrent() throws Exception {
+        doSendMessages(100, 10);
+    }
+
+    private void doSendMessages(int files, int poolSize) throws Exception {
+        getMockEndpoint("mock:claus").expectedMessageCount(files / 2);
+        getMockEndpoint("mock:james").expectedMessageCount(files / 2);
+        getMockEndpoint("mock:claus").assertNoDuplicates(body());
+        getMockEndpoint("mock:james").assertNoDuplicates(body());
+        getMockEndpoint("mock:other").expectedMessageCount(0);
+
+        ExecutorService executor = Executors.newFixedThreadPool(poolSize);
+        for (int i = 0; i < files; i++) {
+            final int index = i;
+            executor.submit(new Callable<Object>() {
+                public Object call() throws Exception {
+                    if (index % 2 == 0) {
+                        template.sendBody("seda:foo", createXmlBody(index, "Claus"));
+                    } else {
+                        template.sendBody("seda:foo", createXmlBody(index, "James"));
+                    }
+                    return null;
+                }
+            });
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    private String createXmlBody(int index, String name) {
+        StringBuilder sb = new StringBuilder();
+        sb.append("<persons>\n");
+        for (int i = 0; i < 100; i++) {
+            sb.append("<person>");
+            sb.append("<id>" + index + "-" + i + "</id>");
+            sb.append("<name>");
+            if (i == 95) {
+                sb.append(name);
+            } else {
+                sb.append("Foo");
+            }
+            sb.append("</name>");
+            sb.append("</person>'\n");
+        }
+        sb.append("\n</persons>");
+        return sb.toString();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("seda:foo?concurrentConsumers=10")
+                    .choice()
+                        .when().xpath("/persons/person/name = 'Claus'")
+                            .to("mock:claus")
+                        .when().xpath("/persons/person/name = 'James'")
+                            .to("mock:james")
+                        .otherwise()
+                            .to("mock:other")
+                    .end();
+
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathAnotherRouteConcurrentTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathAnotherRouteConcurrentTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathRouteConcurrentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathRouteConcurrentTest.java?rev=818451&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathRouteConcurrentTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathRouteConcurrentTest.java Thu Sep 24 11:49:20 2009
@@ -0,0 +1,106 @@
+/**
+ * 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.language;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version $Revision$
+ */
+public class XPathRouteConcurrentTest extends ContextTestSupport {
+
+    public void testXPathNotConcurrent() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:other").expectedMessageCount(0);
+
+        template.sendBody("seda:foo", "<person><name>Claus</name></person>");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testXPathTwoMessages() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:other").expectedMessageCount(1);
+
+        template.sendBody("seda:foo", "<person><name>Claus</name></person>");
+        template.sendBody("seda:foo", "<person><name>James</name></person>");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testXPathTwoMessagesNotSameTime() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:other").expectedMessageCount(1);
+
+        template.sendBody("seda:foo", "<person><name>Claus</name></person>");
+
+        Thread.sleep(1000);
+
+        template.sendBody("seda:foo", "<person><name>James</name></person>");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testNoConcurrent() throws Exception {
+        doSendMessages(1, 1);
+    }
+
+    public void testConcurrent() throws Exception {
+        doSendMessages(10, 5);
+    }
+
+    private void doSendMessages(int files, int poolSize) throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(files);
+        getMockEndpoint("mock:result").assertNoDuplicates(body());
+        getMockEndpoint("mock:other").expectedMessageCount(0);
+
+        ExecutorService executor = Executors.newFixedThreadPool(poolSize);
+        for (int i = 0; i < files; i++) {
+            final int index = i;
+            executor.submit(new Callable<Object>() {
+                public Object call() throws Exception {
+                    template.sendBody("seda:foo", "<person><id>" + index + "</id><name>Claus</name></person>");
+                    return null;
+                }
+            });
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("seda:foo?concurrentConsumers=10")
+                    .choice()
+                        .when().xpath("/person/name = 'Claus'")
+                            .to("mock:result")
+                        .otherwise()
+                            .to("mock:other")
+                    .end();
+
+            }
+        };
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathRouteConcurrentTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathRouteConcurrentTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date