Skip to content
Permalink
Browse files
also improve jmx port allocation in tests
to fix another observed test failure,
at https://builds.apache.org/job/incubator-brooklyn-pull-requests/org.apache.brooklyn$brooklyn-software-base/2439/testReport/junit/org.apache.brooklyn.feed.jmx/JmxFeedTest/setUp/

Port already in use: 40125; nested exception is:
 java.net.BindException: Address already in use
  • Loading branch information
ahgittin committed Jan 22, 2016
1 parent cfd54d5 commit e77f62a09cc0b256387e564862682d11c809861f
Showing 4 changed files with 28 additions and 8 deletions.
@@ -21,7 +21,6 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanNotificationInfo;
@@ -47,6 +46,7 @@
import org.apache.brooklyn.core.entity.Attributes;
import org.apache.brooklyn.entity.java.UsesJmx;
import org.apache.brooklyn.feed.jmx.JmxHelper;
import org.apache.brooklyn.test.NetworkingTestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -68,8 +68,10 @@ public class JmxService {
private String url;

public JmxService() throws Exception {
this("localhost", 28000 + (int)Math.floor(new Random().nextDouble() * 1000));
logger.warn("use of deprecated default host and port in JmxService");
this("localhost", NetworkingTestUtils.randomPortAround(28000));

// TODO why this message if the constructor is not actually deprecated, and it seems useful?
//logger.warn("use of deprecated default host and port in JmxService");
}

/**
@@ -151,10 +153,12 @@ public GeneralisedDynamicMBean registerMBean(String name) throws InstanceAlready
* @throws MBeanRegistrationException
* @throws InstanceAlreadyExistsException
*/
@SuppressWarnings({ "rawtypes" })
public GeneralisedDynamicMBean registerMBean(Map initialAttributes, String name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, MalformedObjectNameException, NullPointerException {
return registerMBean(initialAttributes, ImmutableMap.of(), name);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public GeneralisedDynamicMBean registerMBean(Map initialAttributes, Map operations, String name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, MalformedObjectNameException, NullPointerException {
GeneralisedDynamicMBean mbean = new GeneralisedDynamicMBean(initialAttributes, operations);
server.registerMBean(mbean, new ObjectName(name));
@@ -65,6 +65,7 @@
import org.apache.brooklyn.entity.software.base.test.jmx.JmxService;
import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation;
import org.apache.brooklyn.test.Asserts;
import org.apache.brooklyn.test.NetworkingTestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterMethod;
@@ -114,7 +115,13 @@ public static class TestEntityWithJmx extends TestEntityImpl {
@Override public void init() {
sensors().set(Attributes.HOSTNAME, "localhost");
sensors().set(UsesJmx.JMX_PORT,
LocalhostMachineProvisioningLocation.obtainPort(PortRanges.fromString("40123+")));
LocalhostMachineProvisioningLocation.obtainPort(PortRanges.fromString(
// just doing "40123+" was not enough to avoid collisions (on 40125),
// observed on jenkins, not sure why but
// maybe something else had a UDP connection we weren't detected,
// or the static lock our localhost uses was being bypassed;
// this should improve things (2016-01)
NetworkingTestUtils.randomPortAround(40000)+"+")));
// only supports no-agent, at the moment
config().set(UsesJmx.JMX_AGENT_MODE, JmxAgentModes.NONE);
sensors().set(UsesJmx.RMI_REGISTRY_PORT, -1); // -1 means to use the JMX_PORT only
@@ -22,13 +22,13 @@

import java.util.Map;

import org.apache.brooklyn.test.Asserts;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.net.Networking;
import org.apache.brooklyn.util.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableMap;

public class NetworkingTestUtils {
@@ -65,4 +65,14 @@ public static void assertPortsAvailable(final Map<String, Integer> ports) {
assertTrue(Networking.isPortAvailable(entry.getValue()), errmsg);
}
}

/** Returns a port not in use somewhere around the seed;
* this is not a foolproof way to prevent collisions,
* but strikes a good balance of traceability (different callers will use different ranges)
* and collision avoidance, esp when combined with <code>Localhost...obtain(thisResult+"+");</code>.
*/
@Beta
public static int randomPortAround(int seed) {
return Networking.nextAvailablePort( seed + (int)Math.floor(Math.random() * 1000) );
}
}
@@ -18,6 +18,8 @@
*/
package org.apache.brooklyn.util.net;

import static com.google.common.base.Preconditions.checkArgument;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
@@ -26,7 +28,6 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Enumeration;
@@ -48,8 +49,6 @@
import com.google.common.net.HostAndPort;
import com.google.common.primitives.UnsignedBytes;

import static com.google.common.base.Preconditions.checkArgument;

public class Networking {

private static final Logger log = LoggerFactory.getLogger(Networking.class);

0 comments on commit e77f62a

Please sign in to comment.