@hashgraph/solo
Version:
An opinionated CLI tool to deploy and manage private Hedera Networks.
55 lines • 2.72 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
import { expect } from 'chai';
import { describe, it } from 'mocha';
import net from 'node:net';
import { PortUtilities } from '../../../../src/business/utils/port-utilities.js';
import { SoloPinoLogger } from '../../../../src/core/logging/solo-pino-logger.js';
import sinon from 'sinon';
// Mock logger for testing
const mockLogger = sinon.createStubInstance(SoloPinoLogger);
describe('Port Utils', () => {
describe('findAvailablePort', () => {
it('should find the next available port when the initial port is in use', async () => {
// Create a server to occupy a port
const server = net.createServer();
const basePort = 8000; // Use a port that's likely to be available for testing
// Start the server on the base port
await new Promise(resolve => {
server.listen(basePort, '127.0.0.1', () => {
resolve();
});
});
try {
// Verify the port is actually in use
const isBasePortAvailable = await PortUtilities.isPortAvailable(basePort);
expect(isBasePortAvailable).to.be.false;
// Call findAvailablePort with the occupied port
const availablePort = await PortUtilities.findAvailablePort(basePort, 5000, mockLogger);
// Verify that the returned port is the next port (basePort + 1)
expect(availablePort).to.equal(basePort + 1);
// Verify that the returned port is actually available
const isNextPortAvailable = await PortUtilities.isPortAvailable(basePort + 1);
expect(isNextPortAvailable).to.be.true;
}
finally {
// Clean up: close the server
await new Promise(resolve => {
server.close(() => {
resolve();
});
});
}
});
it('should return the initial port if it is available', async () => {
const basePort = 9000; // Use a different port for this test
// Verify the port is available first
const isBasePortAvailable = await PortUtilities.isPortAvailable(basePort);
expect(isBasePortAvailable).to.be.true;
// Call findAvailablePort with an available port
const availablePort = await PortUtilities.findAvailablePort(basePort, 5000, mockLogger);
// Verify that the returned port is the same as the input port
expect(availablePort).to.equal(basePort);
});
});
});
//# sourceMappingURL=port-utilities.test.js.map