Skip to content

Commit

Permalink
fixup! net: server add asyncDispose
Browse files Browse the repository at this point in the history
  • Loading branch information
atlowChemi committed Jul 9, 2023
1 parent 04ec986 commit e8206ae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
3 changes: 3 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -2244,6 +2244,9 @@ Server.prototype.close = function(cb) {
};

Server.prototype[SymbolAsyncDispose] = async function() {
if (!this._handle) {
return;
}
return FunctionPrototypeCall(promisify(this.close), this);
};

Expand Down
31 changes: 23 additions & 8 deletions test/parallel/test-net-server-async-dispose.mjs
Original file line number Diff line number Diff line change
@@ -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);
}));
});
});

0 comments on commit e8206ae

Please sign in to comment.