11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst net = require('net'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst server = net.createServer(); 61cb0ef41Sopenharmony_ciconst { getEventListeners, once } = require('events'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst liveConnections = new Set(); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(async () => { 111cb0ef41Sopenharmony_ci const port = server.address().port; 121cb0ef41Sopenharmony_ci const host = 'localhost'; 131cb0ef41Sopenharmony_ci const socketOptions = (signal) => ({ port, host, signal }); 141cb0ef41Sopenharmony_ci server.on('connection', (connection) => { 151cb0ef41Sopenharmony_ci liveConnections.add(connection); 161cb0ef41Sopenharmony_ci connection.on('close', () => { 171cb0ef41Sopenharmony_ci liveConnections.delete(connection); 181cb0ef41Sopenharmony_ci }); 191cb0ef41Sopenharmony_ci }); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci const assertAbort = async (socket, testName) => { 221cb0ef41Sopenharmony_ci try { 231cb0ef41Sopenharmony_ci await once(socket, 'close'); 241cb0ef41Sopenharmony_ci assert.fail(`close ${testName} should have thrown`); 251cb0ef41Sopenharmony_ci } catch (err) { 261cb0ef41Sopenharmony_ci assert.strictEqual(err.name, 'AbortError'); 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci }; 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci async function postAbort() { 311cb0ef41Sopenharmony_ci const ac = new AbortController(); 321cb0ef41Sopenharmony_ci const { signal } = ac; 331cb0ef41Sopenharmony_ci const socket = net.connect(socketOptions(signal)); 341cb0ef41Sopenharmony_ci assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 351cb0ef41Sopenharmony_ci ac.abort(); 361cb0ef41Sopenharmony_ci await assertAbort(socket, 'postAbort'); 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci async function preAbort() { 401cb0ef41Sopenharmony_ci const ac = new AbortController(); 411cb0ef41Sopenharmony_ci const { signal } = ac; 421cb0ef41Sopenharmony_ci ac.abort(); 431cb0ef41Sopenharmony_ci const socket = net.connect(socketOptions(signal)); 441cb0ef41Sopenharmony_ci assert.strictEqual(getEventListeners(signal, 'abort').length, 0); 451cb0ef41Sopenharmony_ci await assertAbort(socket, 'preAbort'); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci async function tickAbort() { 491cb0ef41Sopenharmony_ci const ac = new AbortController(); 501cb0ef41Sopenharmony_ci const { signal } = ac; 511cb0ef41Sopenharmony_ci setImmediate(() => ac.abort()); 521cb0ef41Sopenharmony_ci const socket = net.connect(socketOptions(signal)); 531cb0ef41Sopenharmony_ci assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 541cb0ef41Sopenharmony_ci await assertAbort(socket, 'tickAbort'); 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci async function testConstructor() { 581cb0ef41Sopenharmony_ci const ac = new AbortController(); 591cb0ef41Sopenharmony_ci const { signal } = ac; 601cb0ef41Sopenharmony_ci ac.abort(); 611cb0ef41Sopenharmony_ci const socket = new net.Socket(socketOptions(signal)); 621cb0ef41Sopenharmony_ci assert.strictEqual(getEventListeners(signal, 'abort').length, 0); 631cb0ef41Sopenharmony_ci await assertAbort(socket, 'testConstructor'); 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci async function testConstructorPost() { 671cb0ef41Sopenharmony_ci const ac = new AbortController(); 681cb0ef41Sopenharmony_ci const { signal } = ac; 691cb0ef41Sopenharmony_ci const socket = new net.Socket(socketOptions(signal)); 701cb0ef41Sopenharmony_ci assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 711cb0ef41Sopenharmony_ci ac.abort(); 721cb0ef41Sopenharmony_ci await assertAbort(socket, 'testConstructorPost'); 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci async function testConstructorPostTick() { 761cb0ef41Sopenharmony_ci const ac = new AbortController(); 771cb0ef41Sopenharmony_ci const { signal } = ac; 781cb0ef41Sopenharmony_ci const socket = new net.Socket(socketOptions(signal)); 791cb0ef41Sopenharmony_ci assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 801cb0ef41Sopenharmony_ci setImmediate(() => ac.abort()); 811cb0ef41Sopenharmony_ci await assertAbort(socket, 'testConstructorPostTick'); 821cb0ef41Sopenharmony_ci } 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci await postAbort(); 851cb0ef41Sopenharmony_ci await preAbort(); 861cb0ef41Sopenharmony_ci await tickAbort(); 871cb0ef41Sopenharmony_ci await testConstructor(); 881cb0ef41Sopenharmony_ci await testConstructorPost(); 891cb0ef41Sopenharmony_ci await testConstructorPostTick(); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci // Killing the net.socket without connecting hangs the server. 921cb0ef41Sopenharmony_ci for (const connection of liveConnections) { 931cb0ef41Sopenharmony_ci connection.destroy(); 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci server.close(common.mustCall()); 961cb0ef41Sopenharmony_ci})); 97