Skip to content

Commit 5e71b75

Browse files
authored
test: listen on ::1 (#60)
<!-- 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 -->
1 parent a2cfe1d commit 5e71b75

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

test/demo/detect.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { detect } from '../../dist/esm/index.js';
2+
3+
detect(7001)
4+
.then(port => {
5+
console.log(port);
6+
})
7+
.catch(err => {
8+
console.error(err);
9+
});

test/demo/server.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createServer } from 'node:http';
2+
import { hostname } from 'node:os';
3+
4+
const server = createServer();
5+
6+
server.listen(7001, hostname(), () => {
7+
console.log('listening %s:7001, address: %o', hostname(), server.address());
8+
});

test/detect-port.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ describe('test/detect-port.test.ts', () => {
4444
});
4545
servers.push(server4);
4646

47+
const server5 = new net.Server();
48+
server5.listen(25500, '::1', cb);
49+
server5.on('error', err => {
50+
console.error('listen ::1 error:', err);
51+
});
52+
servers.push(server5);
53+
4754
for (let port = 27000; port < 27010; port++) {
4855
const server = new net.Server();
4956
if (port % 3 === 0) {
@@ -102,6 +109,13 @@ describe('test/detect-port.test.ts', () => {
102109
assert.equal(realPort, 25001);
103110
});
104111

112+
it('work with listening next port 25501 because 25500 was listened to ::1', async () => {
113+
const port = 25500;
114+
const realPort = await detectPort(port);
115+
assert(realPort);
116+
assert.equal(realPort, 25501);
117+
});
118+
105119
it('should listen next port 24001 when localhost is not binding', async () => {
106120
mm(dns, 'lookup', (...args: any[]) => {
107121
mm.restore();

0 commit comments

Comments
 (0)