forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: Expose uv_cancel() method on FSReq
This adds a FSReqBase::Cancel() method which calls uv_cancel(). This shows up in JS as a cancel() method on FSReqCallback and FSReqPromise. Refs: nodejs#31971
- Loading branch information
Showing
4 changed files
with
114 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
// Flags: --no-warnings --expose-internals | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { internalBinding } = require('internal/test/binding'); | ||
const path = require('path'); | ||
|
||
const { FSReqCallback, readdir } = internalBinding('fs'); | ||
|
||
{ | ||
// Return value of cancel() is undefined | ||
const req = new FSReqCallback(); | ||
req.oncomplete = () => {}; | ||
assert.strictEqual(req.cancel(), undefined); | ||
} | ||
|
||
|
||
{ | ||
// Callback is called with AbortError if request is canceled | ||
const req = new FSReqCallback(); | ||
req.oncomplete = common.mustCall((err, files) => { | ||
assert.strictEqual(files, undefined); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
}, 1); | ||
req.cancel(); | ||
readdir(path.toNamespacedPath('../'), 'utf8', false, req); | ||
} | ||
|
||
|
||
{ | ||
// Request is canceled if cancel() called before control returns to main loop | ||
const req = new FSReqCallback(); | ||
req.oncomplete = common.mustCall((err, files) => { | ||
assert.strictEqual(files, undefined); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
}, 1); | ||
readdir(path.toNamespacedPath('../'), 'utf8', false, req); | ||
req.cancel(); | ||
} | ||
|
||
|
||
{ | ||
// Request is still canceled on next tick | ||
const req = new FSReqCallback(); | ||
req.oncomplete = common.mustCall((err, files) => { | ||
assert.strictEqual(files, undefined); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
}, 1); | ||
readdir(path.toNamespacedPath('../'), 'utf8', false, req); | ||
process.nextTick(common.mustCall(() => req.cancel())); | ||
} | ||
|
||
|
||
{ | ||
// Callback is not called a second time if request canceled after it has | ||
// already completed | ||
const req = new FSReqCallback(); | ||
req.oncomplete = common.mustCall((err, files) => { | ||
assert.strictEqual(err, null); | ||
assert.notStrictEqual(files, undefined); | ||
req.cancel(); | ||
}, 1); | ||
readdir(path.toNamespacedPath('../'), 'utf8', false, req); | ||
} |