Skip to content

Commit

Permalink
fix: Race condition on downloading the same file twice
Browse files Browse the repository at this point in the history
  • Loading branch information
wayfarer3130 committed May 6, 2024
1 parent 190b70f commit 830939c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion test/data-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ it("noCopy test_fragment_multiframe", async () => {
"https://github.com/dcmjs-org/data/releases/download/encapsulation/encapsulation-fragment-multiframe.dcm";
const dcmPath = await getTestDataset(
url,
"encapsulation-fragment-multiframe-b.dcm"
"encapsulation-fragment-multiframe.dcm"
);
const file = fs.readFileSync(dcmPath);

Expand Down
23 changes: 16 additions & 7 deletions test/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,31 @@ function ensureTestDataDir() {
}

async function getZippedTestDataset(url, filename, unpackDirectory) {
var dir = ensureTestDataDir();
var targetPath = path.join(dir, filename);
var unpackPath = path.join(dir, unpackDirectory);
const dir = ensureTestDataDir();
const targetPath = path.join(dir, filename);
const unpackPath = path.join(dir, unpackDirectory);
if (!fs.existsSync(unpackPath)) {
await downloadToFile(url, targetPath);
await unzip(targetPath, unpackPath);
}
return unpackPath;
}

/**
* Stores the required downloads to prevent async reading before download completed.
*/
const asyncDownloadMap = new Map();

async function getTestDataset(url, filename) {
var dir = ensureTestDataDir();
var targetPath = path.join(dir, filename);
if (!fs.existsSync(targetPath)) {
await downloadToFile(url, targetPath);
const dir = ensureTestDataDir();
const targetPath = path.join(dir, filename);
let filePromise = asyncDownloadMap.get(targetPath);
if (!filePromise && !fs.existsSync(targetPath)) {
filePromise = downloadToFile(url, targetPath);
asyncDownloadMap.set(targetPath,filePromise);
}
// This returns immediately if filePromise is undefined - eg if the file already downloaded.
await filePromise;
return targetPath;
}

Expand Down

0 comments on commit 830939c

Please sign in to comment.