Skip to content

Commit

Permalink
Better behavior on parallel downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 11, 2019
1 parent b5fb483 commit 2314791
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const safeDownload = async function(version, outputDir, nodePath) {
await downloadRuntime(version, tmpFile)

await createOutputDir(outputDir)
await pRename(tmpFile, nodePath)
await moveFile(tmpFile, nodePath)
}

// Retrieve the Node binary from the Node website and persist it.
Expand All @@ -61,3 +61,12 @@ const createOutputDir = async function(outputDir) {

await makeDir(outputDir)
}

const moveFile = async function(tmpFile, nodePath) {
// Another parallel download might have been running
if (await pathExists(nodePath)) {
return
}

await pRename(tmpFile, nodePath)
}
12 changes: 12 additions & 0 deletions test/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ test('Can re-use same outputDir', async t => {
await removeOutputDir(nodePath)
})

test('Parallel downloads', async t => {
const outputDir = getOutputDir()
const [nodePath, nodePathA] = await Promise.all([
getNode(TEST_VERSION, outputDir),
getNode(TEST_VERSION, outputDir),
])

t.is(nodePath, nodePathA)

await removeOutput(nodePath)
})

test('Writes atomically', async t => {
const outputDir = getOutputDir()

Expand Down

0 comments on commit 2314791

Please sign in to comment.