From e8206ae3dce96e07f27bd83cbd4ce1c1bf7d9b1b Mon Sep 17 00:00:00 2001 From: atlowChemi Date: Mon, 10 Jul 2023 00:05:01 +0300 Subject: [PATCH] fixup! net: server add `asyncDispose` --- lib/net.js | 3 ++ .../test-net-server-async-dispose.mjs | 31 ++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/net.js b/lib/net.js index 4e4610aff54b6a..efc813b2d74d25 100644 --- a/lib/net.js +++ b/lib/net.js @@ -2244,6 +2244,9 @@ Server.prototype.close = function(cb) { }; Server.prototype[SymbolAsyncDispose] = async function() { + if (!this._handle) { + return; + } return FunctionPrototypeCall(promisify(this.close), this); }; diff --git a/test/parallel/test-net-server-async-dispose.mjs b/test/parallel/test-net-server-async-dispose.mjs index 99e782fc702fbc..08765e9c719ca9 100644 --- a/test/parallel/test-net-server-async-dispose.mjs +++ b/test/parallel/test-net-server-async-dispose.mjs @@ -1,15 +1,30 @@ import * as common from '../common/index.mjs'; import assert from 'node:assert'; import net from 'node:net'; -import { test } from 'node:test'; +import { describe, it } from 'node:test'; -test('net.Server[Symbol.asyncDispose]()', () => { - const server = net.createServer(); +describe('net.Server[Symbol.asyncDispose]()', () => { + it('should close the server', async () => { + const server = net.createServer(); + const timeoutRef = setTimeout(common.mustNotCall(), 2 ** 31 - 1); - server.listen(0, common.mustCall(async () => { - await server[Symbol.asyncDispose]().then(common.mustCall()); - assert.strictEqual(server.address(), null); - })); + server.listen(0, common.mustCall(async () => { + await server[Symbol.asyncDispose]().then(common.mustCall()); + assert.strictEqual(server.address(), null); + clearTimeout(timeoutRef); + })); - server.on('close', common.mustCall()); + server.on('close', common.mustCall()); + }); + + it('should resolve even if the server is already closed', async () => { + const server = net.createServer(); + const timeoutRef = setTimeout(common.mustNotCall(), 2 ** 31 - 1); + + server.listen(0, common.mustCall(async () => { + await server[Symbol.asyncDispose]().then(common.mustCall()); + await server[Symbol.asyncDispose]().then(common.mustCall(), common.mustNotCall()); + clearTimeout(timeoutRef); + })); + }); });