node: fix test-net-server-call-listen-multiple-times.js (#17785)

This commit is contained in:
Meghan Denny
2025-02-27 23:03:48 -08:00
committed by GitHub
parent 65b8b220d2
commit 11979f69eb
5 changed files with 55 additions and 0 deletions

View File

@@ -1443,6 +1443,8 @@ JSC_DEFINE_HOST_FUNCTION(Bun::jsFunctionMakeErrorWithCode, (JSC::JSGlobalObject
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_ILLEGAL_CONSTRUCTOR, "Illegal constructor"_s));
case ErrorCode::ERR_DIR_CLOSED:
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_DIR_CLOSED, "Directory handle was closed"_s));
case ErrorCode::ERR_SERVER_ALREADY_LISTEN:
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_SERVER_ALREADY_LISTEN, "Listen method has been called more than once without closing."_s));
default: {
break;

View File

@@ -83,6 +83,7 @@ const errors: ErrorCodeMapping = [
["ERR_ZLIB_INITIALIZATION_FAILED", Error],
["ERR_INVALID_CHAR", TypeError],
["MODULE_NOT_FOUND", Error],
["ERR_SERVER_ALREADY_LISTEN", Error],
// Bun-specific
["ERR_FORMDATA_PARSE_ERROR", TypeError],

View File

@@ -629,6 +629,7 @@ declare function $ERR_STREAM_UNSHIFT_AFTER_END_EVENT(): Error;
declare function $ERR_STREAM_PUSH_AFTER_EOF(): Error;
declare function $ERR_STREAM_UNABLE_TO_PIPE(): Error;
declare function $ERR_ILLEGAL_CONSTRUCTOR(): TypeError;
declare function $ERR_SERVER_ALREADY_LISTEN(): Error;
/**
* Convert a function to a class-like object.

View File

@@ -1403,6 +1403,10 @@ Server.prototype.listen = function listen(port, hostname, onListen) {
hostname = hostname || "::";
}
if (this._handle) {
throw $ERR_SERVER_ALREADY_LISTEN();
}
try {
var tls = undefined;
var TLSSocketClass = undefined;

View File

@@ -0,0 +1,47 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
// First test. Check that after error event you can listen right away.
{
const dummyServer = net.Server();
const server = net.Server();
// Run some server in order to simulate EADDRINUSE error.
dummyServer.listen(common.mustCall(() => {
// Try to listen used port.
server.listen(dummyServer.address().port);
}));
server.on('error', common.mustCall((e) => {
server.listen(common.mustCall(() => {
dummyServer.close();
server.close();
}));
}));
}
// Second test. Check that second listen call throws an error.
{
const server = net.Server();
server.listen(common.mustCall(() => server.close()));
assert.throws(() => server.listen(), {
code: 'ERR_SERVER_ALREADY_LISTEN',
name: 'Error'
});
}
// Third test.
// Check that after the close call you can run listen method just fine.
{
const server = net.Server();
server.listen(common.mustCall(() => {
server.close();
server.listen(common.mustCall(() => server.close()));
}));
}