Skip to content

Commit 7f66ae0

Browse files
committed
test for delay abort
1 parent 6139af6 commit 7f66ae0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

packages/compass-import-export/src/export/export-json.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,43 @@ describe('exportJSON', function () {
282282
}
283283
});
284284

285+
it('responds to abortSignal.aborted - with delayed abort', async function () {
286+
const abortController = new AbortController();
287+
288+
const resultPath = path.join(tmpdir, 'test-abort.exported.ejson');
289+
290+
const importedText = await fs.promises.readFile(
291+
fixtures.json.complex,
292+
'utf8'
293+
);
294+
const ejsonToInsert = EJSON.parse(importedText);
295+
await dataService.insertMany(testNS, ejsonToInsert);
296+
297+
const output = fs.createWriteStream(resultPath);
298+
const promise = exportJSONFromQuery({
299+
dataService,
300+
ns: testNS,
301+
output,
302+
variant: 'default',
303+
abortSignal: abortController.signal,
304+
});
305+
abortController.abort();
306+
307+
const result = await promise;
308+
const data = await fs.promises.readFile(resultPath, 'utf8');
309+
310+
try {
311+
JSON.parse(data);
312+
expect.fail('Expected file to not be valid JSON');
313+
} catch (err) {
314+
// With signal part of streams pipeline the file is created and if
315+
// the signal is aborted the stream is destroyed and file is not
316+
// writable anymore and as a result its not able to write trailing ] to the file.
317+
}
318+
expect(result.aborted).to.be.true;
319+
expect(result.docsWritten).to.equal(0);
320+
});
321+
285322
it('exports aggregations', async function () {
286323
const docs = ['pineapple', 'apple', 'orange', 'turtle'].map(
287324
(name, index) => ({

0 commit comments

Comments
 (0)