activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject activemq git commit: https://issues.apache.org/jira/browse/AMQ-5621
Date Thu, 09 Jul 2015 19:19:11 GMT
Repository: activemq
Updated Branches:
  refs/heads/master 21c3ba358 -> 257a79de0


https://issues.apache.org/jira/browse/AMQ-5621

Remove reliance on fixed ports.

Project: http://git-wip-us.apache.org/repos/asf/activemq/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/257a79de
Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/257a79de
Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/257a79de

Branch: refs/heads/master
Commit: 257a79de076d68ae8e2b6d3b25106ee532a87462
Parents: 21c3ba3
Author: Timothy Bish <tabish121@gmail.com>
Authored: Thu Jul 9 15:18:53 2015 -0400
Committer: Timothy Bish <tabish121@gmail.com>
Committed: Thu Jul 9 15:18:53 2015 -0400

----------------------------------------------------------------------
 .../network/DuplexNetworkMBeanTest.java         | 23 ++++++-
 .../org/apache/activemq/util/TestUtils.java     | 68 ++++++++++++++++++++
 2 files changed, 88 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq/blob/257a79de/activemq-unit-tests/src/test/java/org/apache/activemq/network/DuplexNetworkMBeanTest.java
----------------------------------------------------------------------
diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/network/DuplexNetworkMBeanTest.java
b/activemq-unit-tests/src/test/java/org/apache/activemq/network/DuplexNetworkMBeanTest.java
index 213d4ae..13a94cb 100644
--- a/activemq-unit-tests/src/test/java/org/apache/activemq/network/DuplexNetworkMBeanTest.java
+++ b/activemq-unit-tests/src/test/java/org/apache/activemq/network/DuplexNetworkMBeanTest.java
@@ -20,12 +20,15 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assume.assumeNotNull;
 
 import java.net.MalformedURLException;
+import java.util.List;
 import java.util.Set;
 
 import javax.management.ObjectInstance;
 import javax.management.ObjectName;
 
 import org.apache.activemq.broker.BrokerService;
+import org.apache.activemq.util.TestUtils;
+import org.junit.Before;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -35,10 +38,22 @@ public class DuplexNetworkMBeanTest {
     protected static final Logger LOG = LoggerFactory.getLogger(DuplexNetworkMBeanTest.class);
     protected final int numRestarts = 3;
 
+    private int primaryBrokerPort;
+    private int secondaryBrokerPort;
+
+    @Before
+    public void setUp() throws Exception {
+        List<Integer> ports = TestUtils.findOpenPorts(2);
+
+        primaryBrokerPort = ports.get(0);
+        secondaryBrokerPort = ports.get(1);
+    }
+
     protected BrokerService createBroker() throws Exception {
         BrokerService broker = new BrokerService();
         broker.setBrokerName("broker");
-        broker.addConnector("tcp://localhost:61617?transport.reuseAddress=true");
+        broker.getManagementContext().setCreateConnector(false);
+        broker.addConnector("tcp://localhost:" + primaryBrokerPort + "?transport.reuseAddress=true");
 
         return broker;
     }
@@ -46,8 +61,10 @@ public class DuplexNetworkMBeanTest {
     protected BrokerService createNetworkedBroker() throws Exception {
         BrokerService broker = new BrokerService();
         broker.setBrokerName("networkedBroker");
-        broker.addConnector("tcp://localhost:62617?transport.reuseAddress=true");
-        NetworkConnector networkConnector = broker.addNetworkConnector("static:(tcp://localhost:61617?wireFormat.maxInactivityDuration=500)?useExponentialBackOff=false");
+        broker.addConnector("tcp://localhost:" + secondaryBrokerPort + "?transport.reuseAddress=true");
+        broker.getManagementContext().setCreateConnector(false);
+        NetworkConnector networkConnector =
+            broker.addNetworkConnector("static:(tcp://localhost:" + primaryBrokerPort + "?wireFormat.maxInactivityDuration=500)?useExponentialBackOff=false");
         networkConnector.setDuplex(true);
         return broker;
     }

http://git-wip-us.apache.org/repos/asf/activemq/blob/257a79de/activemq-unit-tests/src/test/java/org/apache/activemq/util/TestUtils.java
----------------------------------------------------------------------
diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/util/TestUtils.java b/activemq-unit-tests/src/test/java/org/apache/activemq/util/TestUtils.java
new file mode 100644
index 0000000..dd181ef
--- /dev/null
+++ b/activemq-unit-tests/src/test/java/org/apache/activemq/util/TestUtils.java
@@ -0,0 +1,68 @@
+/**
+ * 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.activemq.util;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.net.ServerSocketFactory;
+
+public class TestUtils {
+
+    public static final int DEFAULT_PORT = 61616;
+
+    public static int findOpenPort() {
+        return findOpenPorts(1).get(0);
+    }
+
+    public static List<Integer> findOpenPorts(int count) {
+        if (count <= 0) {
+            return Collections.emptyList();
+        }
+
+        List<ServerSocket> sockets = new ArrayList<ServerSocket>(count);
+        List<Integer> ports = new ArrayList<Integer>(count);
+        List<Integer> safeSet = new ArrayList<Integer>(count);
+
+        // Pre-fill with a sane default set.
+        for (int i = 0; i < count; ++i) {
+            safeSet.add(DEFAULT_PORT + i);
+        }
+
+        try {
+            for (int i = 0; i < count; ++i) {
+                ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(0);
+
+                sockets.add(socket);
+                ports.add(socket.getLocalPort());
+            }
+        } catch (IOException e) {
+            return safeSet;
+        } finally {
+            for (ServerSocket socket : sockets) {
+                try {
+                    socket.close();
+                } catch (IOException e) {}
+            }
+        }
+
+        return ports;
+    }
+}


Mime
View raw message