Skip to content

Memlock throws error when lock exists #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/errors/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
'use strict'

/**
* Error raised when there is lock already in place when repo is being opened.
*/
class LockExistsError extends Error {
constructor (message) {
super(message)
this.name = 'LockExistsError'
this.code = 'ERR_LOCK_EXISTS'
this.message = message
}
}

LockExistsError.code = 'ERR_LOCK_EXISTS'
exports.LockExistsError = LockExistsError

/**
* Error raised when requested item is not found.
*/
6 changes: 6 additions & 0 deletions src/lock-memory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const errors = require('./errors')
const debug = require('debug')

const log = debug('repo:lock')
@@ -17,6 +18,11 @@ const LOCKS = {}
exports.lock = async (dir) => { // eslint-disable-line require-await
const file = dir + '/' + lockFile
log('locking %s', file)

if (LOCKS[file] === true) {
throw new errors.LockExistsError(`Lock already being held for file: ${file}`)
}

LOCKS[file] = true
const closer = {
async close () { // eslint-disable-line require-await
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
@@ -23,4 +23,5 @@ describe('IPFS Repo Tests on the Browser', () => {
require('./keystore-test')(repo)
require('./config-test')(repo)
require('./api-addr-test')(repo)
require('./lock-test')(repo)
})
8 changes: 1 addition & 7 deletions test/lock-test.js
Original file line number Diff line number Diff line change
@@ -41,16 +41,10 @@ module.exports = (repo) => {
})

describe('lock-memory', () => {
it('should lock a dir', async () => {
it('should lock and unlock dir', async () => {
const dir = '/foo/bar'
expect(await lockMemory.locked(dir)).to.be.false()

await lockMemory.lock(dir)
expect(await lockMemory.locked(dir)).to.be.true()
})

it('should unlock a dir', async () => {
const dir = '/foo/bar'
const closer = await lockMemory.lock(dir)
expect(await lockMemory.locked(dir)).to.be.true()