Skip to content

Commit

Permalink
test: listen on ::1 (#60)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
	- Introduced a detection functionality for identifying available ports.
	- Created a new server instance listening on port 7001.
	- Added a test case to verify detection of the next available port.

- **Bug Fixes**
	- Implemented error handling for server startup issues.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Dec 11, 2024
1 parent a2cfe1d commit 5e71b75
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions test/demo/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { detect } from '../../dist/esm/index.js';

detect(7001)
.then(port => {
console.log(port);
})
.catch(err => {
console.error(err);
});
8 changes: 8 additions & 0 deletions test/demo/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createServer } from 'node:http';
import { hostname } from 'node:os';

const server = createServer();

server.listen(7001, hostname(), () => {
console.log('listening %s:7001, address: %o', hostname(), server.address());
});
14 changes: 14 additions & 0 deletions test/detect-port.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ describe('test/detect-port.test.ts', () => {
});
servers.push(server4);

const server5 = new net.Server();
server5.listen(25500, '::1', cb);
server5.on('error', err => {
console.error('listen ::1 error:', err);
});
servers.push(server5);

for (let port = 27000; port < 27010; port++) {
const server = new net.Server();
if (port % 3 === 0) {
Expand Down Expand Up @@ -102,6 +109,13 @@ describe('test/detect-port.test.ts', () => {
assert.equal(realPort, 25001);
});

it('work with listening next port 25501 because 25500 was listened to ::1', async () => {
const port = 25500;
const realPort = await detectPort(port);
assert(realPort);
assert.equal(realPort, 25501);
});

it('should listen next port 24001 when localhost is not binding', async () => {
mm(dns, 'lookup', (...args: any[]) => {
mm.restore();
Expand Down

0 comments on commit 5e71b75

Please sign in to comment.