MCPcopy Index your code
hub / github.com/nodejs/node / doReadAndCancel

Function doReadAndCancel

test/parallel/test-fs-promises-file-handle-readFile.js:52–116  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

50}
51
52async function doReadAndCancel() {
53 // Signal aborted from the start
54 {
55 const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
56 await using fileHandle = await open(filePathForHandle, 'w+');
57 const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
58 fs.writeFileSync(filePathForHandle, buffer);
59 const signal = AbortSignal.abort();
60 await assert.rejects(readFile(fileHandle, common.mustNotMutateObjectDeep({ signal })), {
61 name: 'AbortError'
62 });
63 }
64
65 // Signal aborted on first tick
66 {
67 const filePathForHandle = path.resolve(tmpDir, 'dogs-running1.txt');
68 await using fileHandle = await open(filePathForHandle, 'w+');
69 const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
70 fs.writeFileSync(filePathForHandle, buffer);
71 const controller = new AbortController();
72 const { signal } = controller;
73 process.nextTick(() => controller.abort());
74 await assert.rejects(readFile(fileHandle, common.mustNotMutateObjectDeep({ signal })), {
75 name: 'AbortError'
76 }, 'tick-0');
77 }
78
79 // Signal aborted right before buffer read
80 {
81 const newFile = path.resolve(tmpDir, 'dogs-running2.txt');
82 const buffer = Buffer.from('Dogs running'.repeat(1000), 'utf8');
83 fs.writeFileSync(newFile, buffer);
84
85 await using fileHandle = await open(newFile, 'r');
86 const controller = new AbortController();
87 const { signal } = controller;
88 tick(1, () => controller.abort());
89 await assert.rejects(fileHandle.readFile(
90 common.mustNotMutateObjectDeep({ signal, encoding: 'utf8' })), {
91 name: 'AbortError'
92 }, 'tick-1');
93 }
94
95 // Validate file size is within range for reading
96 {
97 // Variable taken from https://github.com/nodejs/node/blob/1377163f3351/lib/internal/fs/promises.js#L5
98 const kIoMaxLength = 2 ** 31 - 1;
99
100 if (!tmpdir.hasEnoughSpace(kIoMaxLength)) {
101 // truncate() will fail with ENOSPC if there is not enough space.
102 common.printSkipMessage(`Not enough space in ${tmpDir}`);
103 } else {
104 const newFile = path.resolve(tmpDir, 'dogs-running3.txt');
105 await writeFile(newFile, Buffer.from('0'));
106 await truncate(newFile, kIoMaxLength + 1);
107
108 await using fileHandle = await open(newFile, 'r');
109

Callers

nothing calls this directly

Calls 11

abortMethod · 0.95
openFunction · 0.70
readFileFunction · 0.70
tickFunction · 0.70
writeFileFunction · 0.70
abortMethod · 0.65
truncateFunction · 0.50
resolveMethod · 0.45
fromMethod · 0.45
writeFileSyncMethod · 0.45
readFileMethod · 0.45

Tested by

no test coverage detected