-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: allow safely adding listener to abortSignal
- Loading branch information
1 parent
1936160
commit 9717ae4
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
test/parallel/test-util-add-safe-abort-signal-abort-listener.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as common from '../common/index.mjs'; | ||
import * as util from 'node:util'; | ||
import * as assert from 'node:assert'; | ||
import { describe, it } from 'node:test'; | ||
|
||
describe('util.addAbortListener', () => { | ||
it('should throw if signal not provided', () => { | ||
assert.throws(() => util.addAbortListener(), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
it('should throw if provided signal is invalid', () => { | ||
assert.throws(() => util.addAbortListener(undefined), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => util.addAbortListener(null), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => util.addAbortListener({}), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
it('should throw if listener is not a function', () => { | ||
const { signal } = new AbortController(); | ||
assert.throws(() => util.addAbortListener(signal), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => util.addAbortListener(signal, {}), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => util.addAbortListener(signal, undefined), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
it('should return a Disposable', () => { | ||
const { signal } = new AbortController(); | ||
const disposable = util.addAbortListener(signal, common.mustNotCall()); | ||
|
||
assert.strictEqual(typeof disposable[Symbol.dispose], 'function'); | ||
}); | ||
|
||
it('should execute the listener immediately for aborted runners', () => { | ||
const disposable = util.addAbortListener(AbortSignal.abort(), common.mustCall()); | ||
assert.strictEqual(typeof disposable[Symbol.dispose], 'function'); | ||
}); | ||
|
||
it('should execute the listener even when event propagation stopped', () => { | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
|
||
signal.addEventListener('abort', (e) => e.stopImmediatePropagation()); | ||
util.addAbortListener( | ||
signal, | ||
common.mustCall((e) => assert.strictEqual(e.target, signal)), | ||
); | ||
|
||
controller.abort(); | ||
}); | ||
|
||
it('should remove event listeners when disposed', () => { | ||
const controller = new AbortController(); | ||
const disposable = util.addAbortListener(controller.signal, common.mustNotCall()); | ||
disposable[Symbol.dispose](); | ||
controller.abort(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters