|
2 | 2 | const common = require('../common');
|
3 | 3 | const http = require('http');
|
4 | 4 | const assert = require('assert');
|
| 5 | +const { getEventListeners } = require('events'); |
5 | 6 |
|
6 | 7 | {
|
7 | 8 | // abort
|
@@ -71,23 +72,68 @@ const assert = require('assert');
|
71 | 72 |
|
72 | 73 |
|
73 | 74 | {
|
74 |
| - // Destroy with AbortSignal |
| 75 | + // Destroy post-abort sync with AbortSignal |
75 | 76 |
|
76 | 77 | const server = http.createServer(common.mustNotCall());
|
77 | 78 | const controller = new AbortController();
|
78 |
| - |
| 79 | + const { signal } = controller; |
79 | 80 | server.listen(0, common.mustCall(() => {
|
80 |
| - const options = { port: server.address().port, signal: controller.signal }; |
| 81 | + const options = { port: server.address().port, signal }; |
81 | 82 | const req = http.get(options, common.mustNotCall());
|
82 | 83 | req.on('error', common.mustCall((err) => {
|
83 | 84 | assert.strictEqual(err.code, 'ABORT_ERR');
|
84 | 85 | assert.strictEqual(err.name, 'AbortError');
|
85 | 86 | server.close();
|
86 | 87 | }));
|
| 88 | + assert.strictEqual(getEventListeners(signal, 'abort').length, 1); |
87 | 89 | assert.strictEqual(req.aborted, false);
|
88 | 90 | assert.strictEqual(req.destroyed, false);
|
89 | 91 | controller.abort();
|
90 | 92 | assert.strictEqual(req.aborted, false);
|
91 | 93 | assert.strictEqual(req.destroyed, true);
|
92 | 94 | }));
|
93 | 95 | }
|
| 96 | + |
| 97 | +{ |
| 98 | + // Use post-abort async AbortSignal |
| 99 | + const server = http.createServer(common.mustNotCall()); |
| 100 | + const controller = new AbortController(); |
| 101 | + const { signal } = controller; |
| 102 | + server.listen(0, common.mustCall(() => { |
| 103 | + const options = { port: server.address().port, signal }; |
| 104 | + const req = http.get(options, common.mustNotCall()); |
| 105 | + req.on('error', common.mustCall((err) => { |
| 106 | + assert.strictEqual(err.code, 'ABORT_ERR'); |
| 107 | + assert.strictEqual(err.name, 'AbortError'); |
| 108 | + })); |
| 109 | + |
| 110 | + req.on('close', common.mustCall(() => { |
| 111 | + assert.strictEqual(req.aborted, false); |
| 112 | + assert.strictEqual(req.destroyed, true); |
| 113 | + server.close(); |
| 114 | + })); |
| 115 | + |
| 116 | + assert.strictEqual(getEventListeners(signal, 'abort').length, 1); |
| 117 | + process.nextTick(() => controller.abort()); |
| 118 | + })); |
| 119 | +} |
| 120 | + |
| 121 | +{ |
| 122 | + // Use pre-aborted AbortSignal |
| 123 | + const server = http.createServer(common.mustNotCall()); |
| 124 | + const controller = new AbortController(); |
| 125 | + const { signal } = controller; |
| 126 | + server.listen(0, common.mustCall(() => { |
| 127 | + controller.abort(); |
| 128 | + const options = { port: server.address().port, signal }; |
| 129 | + const req = http.get(options, common.mustNotCall()); |
| 130 | + assert.strictEqual(getEventListeners(signal, 'abort').length, 0); |
| 131 | + req.on('error', common.mustCall((err) => { |
| 132 | + assert.strictEqual(err.code, 'ABORT_ERR'); |
| 133 | + assert.strictEqual(err.name, 'AbortError'); |
| 134 | + server.close(); |
| 135 | + })); |
| 136 | + assert.strictEqual(req.aborted, false); |
| 137 | + assert.strictEqual(req.destroyed, true); |
| 138 | + })); |
| 139 | +} |
0 commit comments