From 830939c15c1837c656193008b0e3d6f2f795e822 Mon Sep 17 00:00:00 2001 From: Bill Wallace Date: Mon, 6 May 2024 16:33:11 -0400 Subject: [PATCH] fix: Race condition on downloading the same file twice --- test/data-options.test.js | 2 +- test/testUtils.js | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/test/data-options.test.js b/test/data-options.test.js index 6b22eec8..c475d7d4 100644 --- a/test/data-options.test.js +++ b/test/data-options.test.js @@ -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); diff --git a/test/testUtils.js b/test/testUtils.js index 2a92bbb3..7353077d 100644 --- a/test/testUtils.js +++ b/test/testUtils.js @@ -55,9 +55,9 @@ 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); @@ -65,12 +65,21 @@ async function getZippedTestDataset(url, filename, unpackDirectory) { 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; }