Skip to content

Commit

Permalink
fix: catch eexists in moveOperations promise
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed May 16, 2023
1 parent 747320f commit ede8b2f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
25 changes: 14 additions & 11 deletions lib/content/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,21 @@ async function moveToDestination (tmp, cache, sri, opts) {
moveOperations.set(
destination,
fs.mkdir(destDir, { recursive: true })
.then(() => moveFile(tmp.target, destination, { overwrite: false }))
.then(async () => {
await moveFile(tmp.target, destination, { overwrite: false })
tmp.moved = true
return tmp.moved
})
.catch(err => {
if (!err.message.startsWith('The destination file exists')) {
throw Object.assign(err, { code: 'EEXIST' })
}
}).finally(() => {
moveOperations.delete(destination)
})

)
try {
await moveOperations.get(destination)
tmp.moved = true
} catch (err) {
if (!err.message.startsWith('The destination file exists')) {
throw Object.assign(err, { code: 'EEXIST' })
}
} finally {
moveOperations.delete(destination)
}
return moveOperations.get(destination)
}

function sizeError (expected, found) {
Expand Down
9 changes: 7 additions & 2 deletions test/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,21 @@ t.test('signals error if error writing to cache', async t => {
t.equal(streamErr.code, 'EBADSIZE', 'got error from stream write')
})

t.test('concurrent puts', async t => {
t.test('concurrent puts with identical content', async t => {
const CACHE = t.testdir()
await Promise.all([
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, `${KEY}2`, CONTENT),
])
const tmpFiles = await fs.readdir(path.join(CACHE, 'tmp'))
t.strictSame(tmpFiles, [], 'Nothing left in tmp')
Expand Down

0 comments on commit ede8b2f

Please sign in to comment.