2020-02-17 12:35:01 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
mocha/no-nested-tests,
|
|
|
|
no-return-assign,
|
|
|
|
no-undef,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:50 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 12:35:16 -05:00
|
|
|
const sinon = require('sinon')
|
2021-03-23 15:08:32 -04:00
|
|
|
const { expect } = require('chai')
|
2020-02-17 12:35:16 -05:00
|
|
|
const modulePath = '../../../../app/js/LockManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('LockManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.Settings = {
|
|
|
|
redis: {
|
2021-07-13 07:04:43 -04:00
|
|
|
lock: {},
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
}
|
|
|
|
this.LockManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-11-10 06:32:05 -05:00
|
|
|
'@overleaf/redis-wrapper': {
|
2020-02-17 12:35:16 -05:00
|
|
|
createClient: () => {
|
|
|
|
return (this.rclient = { auth: sinon.stub() })
|
2021-07-13 07:04:43 -04:00
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
2021-07-13 07:04:43 -04:00
|
|
|
'@overleaf/settings': this.Settings,
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
this.key = 'lock-key'
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('checkLock', function () {
|
|
|
|
describe('when the lock is taken', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.rclient.exists = sinon.stub().callsArgWith(1, null, '1')
|
|
|
|
return this.LockManager.checkLock(this.key, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should check the lock in redis', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.rclient.exists.calledWith(this.key).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return the callback with false', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, false).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when the lock is free', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.rclient.exists = sinon.stub().callsArgWith(1, null, '0')
|
|
|
|
return this.LockManager.checkLock(this.key, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return the callback with true', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, true).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('tryLock', function () {
|
|
|
|
describe('when the lock is taken', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.rclient.set = sinon.stub().callsArgWith(5, null, null)
|
|
|
|
this.LockManager.randomLock = sinon
|
|
|
|
.stub()
|
|
|
|
.returns('locked-random-value')
|
|
|
|
return this.LockManager.tryLock(this.key, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should check the lock in redis', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.rclient.set
|
|
|
|
.calledWith(
|
|
|
|
this.key,
|
|
|
|
'locked-random-value',
|
|
|
|
'EX',
|
|
|
|
this.LockManager.LOCK_TTL,
|
|
|
|
'NX'
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return the callback with false', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, false).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when the lock is free', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.rclient.set = sinon.stub().callsArgWith(5, null, 'OK')
|
|
|
|
return this.LockManager.tryLock(this.key, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return the callback with true', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, true).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('deleteLock', function () {
|
|
|
|
return beforeEach(function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.rclient.del = sinon.stub().callsArg(1)
|
|
|
|
return this.LockManager.deleteLock(this.key, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should delete the lock in redis', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.rclient.del.calledWith(key).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getLock', function () {
|
|
|
|
describe('when the lock is not taken', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, true)
|
|
|
|
return this.LockManager.getLock(this.key, (...args) => {
|
|
|
|
this.callback(...Array.from(args || []))
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should try to get the lock', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.LockManager.tryLock.calledWith(this.key).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should only need to try once', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.LockManager.tryLock.callCount.should.equal(1)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when the lock is initially set', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-17 12:35:16 -05:00
|
|
|
const startTime = Date.now()
|
|
|
|
this.LockManager.LOCK_TEST_INTERVAL = 5
|
2020-06-04 04:24:21 -04:00
|
|
|
this.LockManager.tryLock = function (doc_id, callback) {
|
2020-02-17 12:35:16 -05:00
|
|
|
if (callback == null) {
|
2020-06-04 04:24:21 -04:00
|
|
|
callback = function (error, isFree) {}
|
2020-02-17 12:35:16 -05:00
|
|
|
}
|
|
|
|
if (Date.now() - startTime < 100) {
|
|
|
|
return callback(null, false)
|
|
|
|
} else {
|
|
|
|
return callback(null, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sinon.spy(this.LockManager, 'tryLock')
|
|
|
|
|
|
|
|
return this.LockManager.getLock(this.key, (...args) => {
|
|
|
|
this.callback(...Array.from(args || []))
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should call tryLock multiple times until free', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return (this.LockManager.tryLock.callCount > 1).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when the lock times out', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-17 12:35:16 -05:00
|
|
|
const time = Date.now()
|
|
|
|
this.LockManager.MAX_LOCK_WAIT_TIME = 5
|
|
|
|
this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, false)
|
|
|
|
return this.LockManager.getLock(this.key, (...args) => {
|
|
|
|
this.callback(...Array.from(args || []))
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return the callback with an error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('runWithLock', function () {
|
|
|
|
describe('with successful run', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.runner = function (releaseLock) {
|
2020-02-17 12:35:16 -05:00
|
|
|
if (releaseLock == null) {
|
2020-06-04 04:24:21 -04:00
|
|
|
releaseLock = function (error) {}
|
2020-02-17 12:35:16 -05:00
|
|
|
}
|
|
|
|
return releaseLock()
|
|
|
|
}
|
|
|
|
sinon.spy(this, 'runner')
|
|
|
|
this.LockManager.getLock = sinon.stub().callsArg(1)
|
|
|
|
this.LockManager.releaseLock = sinon.stub().callsArg(2)
|
|
|
|
return this.LockManager.runWithLock(
|
|
|
|
this.key,
|
|
|
|
this.runner,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the lock', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.LockManager.getLock.calledWith(this.key).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should run the passed function', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.runner.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should release the lock', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.LockManager.releaseLock
|
|
|
|
.calledWith(this.key)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when the runner function returns an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.error = new Error('oops')
|
2021-07-13 07:04:43 -04:00
|
|
|
this.runner = releaseLock => {
|
2020-02-17 12:35:16 -05:00
|
|
|
if (releaseLock == null) {
|
2020-06-04 04:24:21 -04:00
|
|
|
releaseLock = function (error) {}
|
2020-02-17 12:35:16 -05:00
|
|
|
}
|
|
|
|
return releaseLock(this.error)
|
|
|
|
}
|
|
|
|
sinon.spy(this, 'runner')
|
|
|
|
this.LockManager.getLock = sinon.stub().callsArg(1)
|
|
|
|
this.LockManager.releaseLock = sinon.stub().callsArg(2)
|
|
|
|
return this.LockManager.runWithLock(
|
|
|
|
this.key,
|
|
|
|
this.runner,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should release the lock', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.LockManager.releaseLock
|
|
|
|
.calledWith(this.key)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(this.error).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('releaseLock', function () {
|
|
|
|
describe('when the lock is current', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.rclient.eval = sinon.stub().yields(null, 1)
|
|
|
|
return this.LockManager.releaseLock(
|
|
|
|
this.key,
|
|
|
|
this.lockValue,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should clear the data from redis', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.rclient.eval
|
|
|
|
.calledWith(
|
|
|
|
this.LockManager.unlockScript,
|
|
|
|
1,
|
|
|
|
this.key,
|
|
|
|
this.lockValue
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when the lock has expired', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.rclient.eval = sinon.stub().yields(null, 0)
|
|
|
|
return this.LockManager.releaseLock(
|
|
|
|
this.key,
|
|
|
|
this.lockValue,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return an error if the lock has expired', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(
|
|
|
|
sinon.match.has('message', 'tried to release timed out lock')
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|