|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const net = require('net'); |
| 4 | +const assert = require('assert'); |
| 5 | +const server = net.createServer(); |
| 6 | +const { getEventListeners } = require('events'); |
| 7 | + |
| 8 | +const liveConnections = new Set(); |
| 9 | + |
| 10 | +server.listen(0, common.mustCall(async () => { |
| 11 | + const port = server.address().port; |
| 12 | + const host = 'localhost'; |
| 13 | + const socketOptions = (signal) => ({ port, host, signal }); |
| 14 | + server.on('connection', (connection) => { |
| 15 | + liveConnections.add(connection); |
| 16 | + connection.on('close', () => { |
| 17 | + liveConnections.delete(connection); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + const attachHandlers = (socket, res) => { |
| 22 | + socket.on('error', common.mustCall((err) => { |
| 23 | + assert.strictEqual(err.name, 'AbortError'); |
| 24 | + })); |
| 25 | + socket.on('close', () => { |
| 26 | + res(); |
| 27 | + }); |
| 28 | + }; |
| 29 | + |
| 30 | + function postAbort() { |
| 31 | + return new Promise((res) => { |
| 32 | + const ac = new AbortController(); |
| 33 | + const { signal } = ac; |
| 34 | + const socket = net.connect(socketOptions(signal)); |
| 35 | + attachHandlers(socket, res); |
| 36 | + assert.strictEqual(getEventListeners(signal, 'abort').length, 1); |
| 37 | + ac.abort(); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + function preAbort() { |
| 42 | + return new Promise((res) => { |
| 43 | + const ac = new AbortController(); |
| 44 | + const { signal } = ac; |
| 45 | + ac.abort(); |
| 46 | + const socket = net.connect(socketOptions(signal)); |
| 47 | + attachHandlers(socket, res); |
| 48 | + assert.strictEqual(getEventListeners(signal, 'abort').length, 0); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + function tickAbort() { |
| 53 | + return new Promise((res) => { |
| 54 | + const ac = new AbortController(); |
| 55 | + const { signal } = ac; |
| 56 | + setImmediate(() => ac.abort()); |
| 57 | + const socket = net.connect(socketOptions(signal)); |
| 58 | + attachHandlers(socket, res); |
| 59 | + assert.strictEqual(getEventListeners(signal, 'abort').length, 1); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + function testConstructor() { |
| 64 | + return new Promise((res) => { |
| 65 | + const ac = new AbortController(); |
| 66 | + const { signal } = ac; |
| 67 | + ac.abort(); |
| 68 | + const socket = new net.Socket(socketOptions(signal)); |
| 69 | + assert.strictEqual(getEventListeners(signal, 'abort').length, 0); |
| 70 | + attachHandlers(socket, res); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + function testConstructorPost() { |
| 75 | + return new Promise((res) => { |
| 76 | + const ac = new AbortController(); |
| 77 | + const { signal } = ac; |
| 78 | + const socket = new net.Socket(socketOptions(signal)); |
| 79 | + attachHandlers(socket, res); |
| 80 | + assert.strictEqual(getEventListeners(signal, 'abort').length, 1); |
| 81 | + ac.abort(); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + function testConstructorPostTick() { |
| 86 | + return new Promise((res) => { |
| 87 | + const ac = new AbortController(); |
| 88 | + const { signal } = ac; |
| 89 | + const socket = new net.Socket(socketOptions(signal)); |
| 90 | + attachHandlers(socket, res); |
| 91 | + assert.strictEqual(getEventListeners(signal, 'abort').length, 1); |
| 92 | + setImmediate(() => { |
| 93 | + ac.abort(); |
| 94 | + }); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + await postAbort(); |
| 99 | + await preAbort(); |
| 100 | + await tickAbort(); |
| 101 | + await testConstructor(); |
| 102 | + await testConstructorPost(); |
| 103 | + await testConstructorPostTick(); |
| 104 | + |
| 105 | + // Killing the net.socket without connecting hangs the server. |
| 106 | + for (const connection of liveConnections) { |
| 107 | + connection.destroy(); |
| 108 | + } |
| 109 | + server.close(common.mustCall()); |
| 110 | +})); |
0 commit comments