git commit: [JGroups] Added JGroupsExpression helper class.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

git commit: [JGroups] Added JGroupsExpression helper class.

hekonsek-2
Updated Branches:
  refs/heads/master bfd4204d4 -> 09f389aa6


[JGroups] Added JGroupsExpression helper class.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/09f389aa
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/09f389aa
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/09f389aa

Branch: refs/heads/master
Commit: 09f389aa67a9a80f29abf87a1cc7a5c32e5f9012
Parents: bfd4204
Author: Henryk Konsek <[hidden email]>
Authored: Tue Jan 7 20:50:20 2014 +0100
Committer: Henryk Konsek <[hidden email]>
Committed: Tue Jan 7 20:50:20 2014 +0100

----------------------------------------------------------------------
 .../component/jgroups/JGroupsExpressions.java   |  37 +++++
 .../jgroups/JGroupsClusterRouteTest.java        | 140 +++++++++++++++++++
 2 files changed, 177 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/09f389aa/components/camel-jgroups/src/main/java/org/apache/camel/component/jgroups/JGroupsExpressions.java
----------------------------------------------------------------------
diff --git a/components/camel-jgroups/src/main/java/org/apache/camel/component/jgroups/JGroupsExpressions.java b/components/camel-jgroups/src/main/java/org/apache/camel/component/jgroups/JGroupsExpressions.java
new file mode 100644
index 0000000..223c9ad
--- /dev/null
+++ b/components/camel-jgroups/src/main/java/org/apache/camel/component/jgroups/JGroupsExpressions.java
@@ -0,0 +1,37 @@
+/**
+ * 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.jgroups;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.support.ExpressionAdapter;
+
+public final class JGroupsExpressions {
+
+    private JGroupsExpressions() {
+    }
+
+    public static Expression delayIfContextNotStarted(final long delay) {
+        return new ExpressionAdapter() {
+            @Override
+            public Object evaluate(Exchange exchange) {
+                return exchange.getContext().getStatus().isStarted() ? 0 : delay;
+            }
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/09f389aa/components/camel-jgroups/src/test/java/org/apache/camel/component/jgroups/JGroupsClusterRouteTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jgroups/src/test/java/org/apache/camel/component/jgroups/JGroupsClusterRouteTest.java b/components/camel-jgroups/src/test/java/org/apache/camel/component/jgroups/JGroupsClusterRouteTest.java
new file mode 100644
index 0000000..198d31a
--- /dev/null
+++ b/components/camel-jgroups/src/test/java/org/apache/camel/component/jgroups/JGroupsClusterRouteTest.java
@@ -0,0 +1,140 @@
+/**
+ * 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.jgroups;
+
+import static java.util.UUID.randomUUID;
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.apache.camel.component.jgroups.JGroupsExpressions.delayIfContextNotStarted;
+import static org.apache.camel.component.jgroups.JGroupsFilters.dropNonCoordinatorViews;
+
+public class JGroupsClusterRouteTest extends Assert {
+
+    // Routing fixtures
+
+    CamelContext firstCamelContext;
+
+    CamelContext secondCamelContext;
+
+    String clusterName = randomUUID().toString();
+
+    String masterMockUri = "mock:master?resultWaitTime=2m";
+
+    class Builder extends RouteBuilder {
+
+        @Override
+        public void configure() throws Exception {
+            from("jgroups:" + clusterName + "?enableViewMessages=true").
+                    filter(dropNonCoordinatorViews()).
+                    threads().delay(delayIfContextNotStarted(SECONDS.toMillis(15))).
+                    to("controlbus:route?routeId=masterRoute&action=start&async=true");
+
+            from("timer://master?repeatCount=1").routeId("masterRoute").autoStartup(false).to(masterMockUri);
+        }
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        firstCamelContext = new DefaultCamelContext();
+        firstCamelContext.addRoutes(new Builder());
+
+        secondCamelContext = new DefaultCamelContext();
+        secondCamelContext.addRoutes(new Builder());
+    }
+
+    // Tests
+
+    @Test
+    public void shouldElectSecondNode() throws Exception {
+        expectMasterIs(firstCamelContext);
+        firstCamelContext.start();
+        assertMasterIs(firstCamelContext);
+
+        expectMasterIsNot(secondCamelContext);
+        secondCamelContext.start();
+        assertMasterIsNot(secondCamelContext);
+
+        expectMasterIs(secondCamelContext);
+        firstCamelContext.stop();
+        assertMasterIs(secondCamelContext);
+    }
+
+    @Test
+    public void shouldKeepMaster() throws Exception {
+        expectMasterIs(firstCamelContext);
+        firstCamelContext.start();
+        assertMasterIs(firstCamelContext);
+
+        expectMasterIsNot(secondCamelContext);
+        secondCamelContext.start();
+        assertMasterIsNot(secondCamelContext);
+
+        expectMasterIs(firstCamelContext);
+        secondCamelContext.stop();
+        assertMasterIs(firstCamelContext);
+    }
+
+    @Test
+    public void shouldElectSecondNodeAndReturnToFirst() throws Exception {
+        expectMasterIs(firstCamelContext);
+        firstCamelContext.start();
+        assertMasterIs(firstCamelContext);
+
+        expectMasterIsNot(secondCamelContext);
+        secondCamelContext.start();
+        assertMasterIsNot(secondCamelContext);
+
+        expectMasterIsNot(firstCamelContext);
+        firstCamelContext.stop();
+        assertMasterIsNot(firstCamelContext);
+
+        expectMasterIsNot(firstCamelContext);
+        firstCamelContext.start();
+        assertMasterIsNot(firstCamelContext);
+
+        expectMasterIs(firstCamelContext);
+        secondCamelContext.stop();
+        assertMasterIs(firstCamelContext);
+    }
+
+    // Helpers
+
+    private void expectMasterIs(CamelContext camelContext) {
+        camelContext.getEndpoint(masterMockUri, MockEndpoint.class).expectedMessageCount(1);
+    }
+
+    private void expectMasterIsNot(CamelContext camelContext) {
+        camelContext.getEndpoint(masterMockUri, MockEndpoint.class).expectedMessageCount(0);
+    }
+
+    private void assertMasterIs(CamelContext camelContext) throws InterruptedException {
+        camelContext.getEndpoint(masterMockUri, MockEndpoint.class).assertIsSatisfied();
+    }
+
+    private void assertMasterIsNot(CamelContext camelContext) throws InterruptedException {
+        camelContext.getEndpoint(masterMockUri, MockEndpoint.class).assertIsSatisfied();
+    }
+
+}
\ No newline at end of file