Skip to content

Commit 0f24a18

Browse files
authored
Merge pull request #394 from dcmjs-org/fix/race-condition-test
test: Race condition test
2 parents 8f75e74 + b9d0c3d commit 0f24a18

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

test/testUtils.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,31 @@ function ensureTestDataDir() {
5555
}
5656

5757
async function getZippedTestDataset(url, filename, unpackDirectory) {
58-
var dir = ensureTestDataDir();
59-
var targetPath = path.join(dir, filename);
60-
var unpackPath = path.join(dir, unpackDirectory);
58+
const dir = ensureTestDataDir();
59+
const targetPath = path.join(dir, filename);
60+
const unpackPath = path.join(dir, unpackDirectory);
6161
if (!fs.existsSync(unpackPath)) {
6262
await downloadToFile(url, targetPath);
6363
await unzip(targetPath, unpackPath);
6464
}
6565
return unpackPath;
6666
}
6767

68+
/**
69+
* Stores the required downloads to prevent async reading before download completed.
70+
*/
71+
const asyncDownloadMap = new Map();
72+
6873
async function getTestDataset(url, filename) {
69-
var dir = ensureTestDataDir();
70-
var targetPath = path.join(dir, filename);
71-
if (!fs.existsSync(targetPath)) {
72-
await downloadToFile(url, targetPath);
74+
const dir = ensureTestDataDir();
75+
const targetPath = path.join(dir, filename);
76+
let filePromise = asyncDownloadMap.get(targetPath);
77+
if (!filePromise && !fs.existsSync(targetPath)) {
78+
filePromise = downloadToFile(url, targetPath);
79+
asyncDownloadMap.set(targetPath,filePromise);
7380
}
81+
// This returns immediately if filePromise is undefined - eg if the file already downloaded.
82+
await filePromise;
7483
return targetPath;
7584
}
7685

0 commit comments

Comments
 (0)