Skip to content

Commit

Permalink
test: deflake test-blob-file-backed
Browse files Browse the repository at this point in the history
Avoid race conditions by using a different file for each subtest.

Fixes: #51860
PR-URL: #53920
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
  • Loading branch information
lpinca authored and targos committed Jul 28, 2024
1 parent 03f3532 commit c247582
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions test/parallel/test-blob-file-backed.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ const tmpdir = require('../common/tmpdir');
const testfile = tmpdir.resolve('test-file-backed-blob.txt');
const testfile2 = tmpdir.resolve('test-file-backed-blob2.txt');
const testfile3 = tmpdir.resolve('test-file-backed-blob3.txt');
const testfile4 = tmpdir.resolve('test-file-backed-blob4.txt');
const testfile5 = tmpdir.resolve('test-file-backed-blob5.txt');
tmpdir.refresh();

const data = `${'a'.repeat(1000)}${'b'.repeat(2000)}`;

writeFileSync(testfile, data);
writeFileSync(testfile2, data.repeat(100));
writeFileSync(testfile3, '');
writeFileSync(testfile2, data);
writeFileSync(testfile3, data.repeat(100));
writeFileSync(testfile4, '');
writeFileSync(testfile5, '');

(async () => {
const blob = await openAsBlob(testfile);
Expand Down Expand Up @@ -69,7 +73,7 @@ writeFileSync(testfile3, '');

(async () => {
// Refs: https://github.com/nodejs/node/issues/47683
const blob = await openAsBlob(testfile);
const blob = await openAsBlob(testfile2);
const res = blob.slice(10, 20);
const ab = await res.arrayBuffer();
strictEqual(res.size, ab.byteLength);
Expand All @@ -82,39 +86,41 @@ writeFileSync(testfile3, '');

const res1 = blob.slice(995, 1005);
strictEqual(await res1.text(), data.slice(995, 1005));
await unlink(testfile2);
})().then(common.mustCall());

(async () => {
const blob = await openAsBlob(testfile2);
const blob = await openAsBlob(testfile3);
const stream = blob.stream();
const read = async () => {
// eslint-disable-next-line no-unused-vars
for await (const _ of stream) {
writeFileSync(testfile2, data + 'abc');
writeFileSync(testfile3, data + 'abc');
}
};

await rejects(read(), { name: 'NotReadableError' });

await unlink(testfile2);
await unlink(testfile3);
})().then(common.mustCall());

(async () => {
const blob = await openAsBlob(testfile3);
const blob = await openAsBlob(testfile4);
strictEqual(blob.size, 0);
strictEqual(await blob.text(), '');
writeFileSync(testfile3, 'abc');
writeFileSync(testfile4, 'abc');
await rejects(blob.text(), { name: 'NotReadableError' });
await unlink(testfile3);
await unlink(testfile4);
})().then(common.mustCall());

(async () => {
const blob = await openAsBlob(testfile3);
const blob = await openAsBlob(testfile5);
strictEqual(blob.size, 0);
writeFileSync(testfile3, 'abc');
writeFileSync(testfile5, 'abc');
const stream = blob.stream();
const reader = stream.getReader();
await rejects(() => reader.read(), { name: 'NotReadableError' });
await unlink(testfile5);
})().then(common.mustCall());

(async () => {
Expand Down

0 comments on commit c247582

Please sign in to comment.