Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WFLY-2345] Port Offset 0 does not work #5307

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -111,7 +111,7 @@ public SocketBindingManager getSocketBindings() {

private int calculatePort() {
int port = this.port;
if (port > 0 && isFixedPort == false) {
if (!isFixedPort) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps port <= 0 would be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it ever can be negative. The model/schema should validate that's in a valid range.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ok, port 0 is valid, negative ones are not.

port += socketBindings.getPortOffset();
}
return port;
Expand Down
Expand Up @@ -35,6 +35,7 @@
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SOCKET_BINDING_GROUP;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SYSTEM_PROPERTY;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.VALUE;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.PORT_OFFSET;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -71,6 +72,7 @@ public class ControllerInitializer {
public static final String INTERFACE_NAME = "test-interface";
public static final String SOCKET_BINDING_GROUP_NAME = "test-socket-binding-group";
protected volatile String bindAddress = "localhost";
protected volatile String portOffset;
protected final Map<String, String> systemProperties = new HashMap<String, String>();
protected final Map<String, Integer> socketBindings = new HashMap<String, Integer>();
protected final Map<String, OutboundSocketBinding> outboundSocketBindings = new HashMap<String, OutboundSocketBinding>();
Expand Down Expand Up @@ -180,6 +182,15 @@ public void addPath(String name, String path, String relativeTo) {
paths.put(name, pathInfo);
}

/**
* Adds the port offset to the model (optional).
*
* @param portOffset the port offset ({@code null} means no offset will be added)
*/
public void setPortOffset(String portOffset) {
this.portOffset = portOffset;
}

/**
* Called by framework to set up the model
*
Expand Down Expand Up @@ -307,6 +318,9 @@ protected void initializeSocketBindingsOperations(List<ModelNode> ops) {
op.get(OP).set(ADD);
op.get(OP_ADDR).set(PathAddress.pathAddress(PathElement.pathElement(SOCKET_BINDING_GROUP, SOCKET_BINDING_GROUP_NAME)).toModelNode());
op.get(DEFAULT_INTERFACE).set(INTERFACE_NAME);
if (portOffset != null) {
op.get(PORT_OFFSET).set(portOffset);
}
ops.add(op);


Expand Down
Expand Up @@ -114,6 +114,44 @@ public void testSystemProperty() throws Exception {
Assert.assertEquals("testing123", model.require(SYSTEM_PROPERTY).require("test123").require(VALUE).asString());
}


/**
* Test that a port offset of 0 works correctly.
*/
@Test
public void testPortOffsetZero() throws Exception {
((OtherServicesSubsystemExtension)getMainExtension()).setAddHandler(SubsystemAddWithSocketBindingUserService.INSTANCE);

//Parse the subsystem xml and install into the controller
String subsystemXml =
"<subsystem xmlns=\"" + SimpleSubsystemExtension.NAMESPACE + "\">" +
"</subsystem>";
KernelServices services = createKernelServicesBuilder(new PortOffsetZeroInit())
.setSubsystemXml(subsystemXml)
.build();


//Read the whole model and make sure it looks as expected
ModelNode model = services.readWholeModel();
Assert.assertTrue(model.get(SUBSYSTEM).hasDefined(OtherServicesSubsystemExtension.SUBSYSTEM_NAME));

ModelNode group = model.require(SOCKET_BINDING_GROUP).require(ControllerInitializer.SOCKET_BINDING_GROUP_NAME);
Assert.assertEquals(8000, group.require(PORT_OFFSET).asInt());

ModelNode bindings = group.require(SOCKET_BINDING);
Assert.assertEquals(1, bindings.asList().size());
Assert.assertEquals(0, group.require(SOCKET_BINDING).require("test2").require(PORT).asInt());

ServiceController<?> controller = services.getContainer().getService(SocketBindingUserService.NAME);
Assert.assertNotNull(controller);
SocketBindingUserService service = (SocketBindingUserService)controller.getValue();
SocketBinding socketBinding = service.socketBindingValue.getValue();
Assert.assertEquals(8000, socketBinding.getSocketBindings().getPortOffset());
Assert.assertFalse("fixed port", socketBinding.isFixedPort());
Assert.assertEquals(0, socketBinding.getPort());
Assert.assertEquals(8000, socketBinding.getSocketAddress().getPort());
}

/**
* Test that socket binding got added properly
*/
Expand Down Expand Up @@ -205,6 +243,14 @@ protected void setupController(ControllerInitializer controllerInitializer) {
}
}

private static class PortOffsetZeroInit extends AdditionalInitialization {
@Override
protected void setupController(ControllerInitializer controllerInitializer) {
controllerInitializer.setPortOffset("8000");
controllerInitializer.addSocketBinding("test2", 0);
}
}

private static class SocketBindingInit extends AdditionalInitialization {
@Override
protected void setupController(ControllerInitializer controllerInitializer) {
Expand Down