Skip to content

Commit

Permalink
[#279] Generate unique port numbers for acceptors and connectors sepa…
Browse files Browse the repository at this point in the history
…rately

Updated the logic to generate unique port numbers for acceptors and connectors separately.
Updated unit test to verify the unique port assignment for both acceptors and connectors separately.
  • Loading branch information
Msarawan committed Aug 30, 2024
1 parent b7a6260 commit ca779e2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/reducers/7.12/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,29 +540,29 @@ describe('test the creation broker reducer', () => {
let newStateWithConnector = artemisCrReducer(initialState, {
operation: ArtemisReducerOperations.addConnector,
});
expect(newStateWithConnector.cr.spec.connectors[0].port).toBe(5559);
expect(newStateWithConnector.cr.spec.connectors[0].port).toBe(5555);

//Add second connector
newStateWithConnector = artemisCrReducer(initialState, {
operation: ArtemisReducerOperations.addConnector,
});
expect(newStateWithConnector.cr.spec.connectors[1].port).toBe(5560);
expect(newStateWithConnector.cr.spec.connectors[1].port).toBe(5556);

// Manually change the port of the second connector to 5561
// Manually change the port of the second connector to 5557
newStateWithConnector = artemisCrReducer(newStateWithConnector, {
operation: ArtemisReducerOperations.setConnectorPort,
payload: {
name: 'connectors1',
port: 5561,
port: 5557,
},
});
expect(newStateWithConnector.cr.spec.connectors[1].port).toBe(5561);
expect(newStateWithConnector.cr.spec.connectors[1].port).toBe(5557);

//Add third connector
newStateWithConnector = artemisCrReducer(newStateWithConnector, {
operation: ArtemisReducerOperations.addConnector,
});
expect(newStateWithConnector.cr.spec.connectors[2].port).toBe(5562);
expect(newStateWithConnector.cr.spec.connectors[2].port).toBe(5558);
});

it('test setConnectorProtocols', () => {
Expand Down
30 changes: 21 additions & 9 deletions src/reducers/7.12/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1125,20 +1125,15 @@ const generateUniqueName = (prefix: string, existing: Set<string>): string => {
return newName;
};

const generateUniquePort = (cr: BrokerCR): number => {
const generateUniqueAcceptorPort = (cr: BrokerCR): number => {
const acceptorSet = listConfigs(
ConfigType.acceptors,
cr,
'set',
) as Set<string>;
const connectorSet = listConfigs(
ConfigType.connectors,
cr,
'set',
) as Set<string>;

const basePort = 5555;
if (acceptorSet.size === 0 && connectorSet.size === 0) {
if (acceptorSet.size === 0) {
return basePort;
}

Expand All @@ -1151,6 +1146,23 @@ const generateUniquePort = (cr: BrokerCR): number => {
}
});

return maxPort + 1;
};

const generateUniqueConnectorPort = (cr: BrokerCR): number => {
const connectorSet = listConfigs(
ConfigType.connectors,
cr,
'set',
) as Set<string>;

const basePort = 5555;
if (connectorSet.size === 0) {
return basePort;
}

let maxPort = basePort;

connectorSet.forEach((name) => {
const port = getConfigPort(cr, ConfigType.connectors, name);
if (port > maxPort) {
Expand All @@ -1171,7 +1183,7 @@ const addConfig = (cr: BrokerCR, configType: ConfigType) => {
name: newName,
protocols: 'ALL',
host: 'localhost',
port: generateUniquePort(cr),
port: generateUniqueConnectorPort(cr),
};
if (!cr.spec.connectors) {
cr.spec.connectors = [connector];
Expand All @@ -1182,7 +1194,7 @@ const addConfig = (cr: BrokerCR, configType: ConfigType) => {
const acceptor = {
name: newName,
protocols: 'ALL',
port: generateUniquePort(cr),
port: generateUniqueAcceptorPort(cr),
};
if (!cr.spec.acceptors) {
cr.spec.acceptors = [acceptor];
Expand Down

0 comments on commit ca779e2

Please sign in to comment.