2020-05-06 06:11:22 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:10:51 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:11:36 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = '../../../../app/js/LockManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2022-05-18 04:22:20 -04:00
|
|
|
const tk = require('timekeeper')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('LockManager - trying the lock', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
let Profiler
|
|
|
|
this.LockManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-11-10 06:32:04 -05:00
|
|
|
'@overleaf/redis-wrapper': {
|
2020-05-06 06:11:36 -04:00
|
|
|
createClient: () => {
|
|
|
|
return {
|
|
|
|
auth() {},
|
2021-07-13 07:04:42 -04:00
|
|
|
set: (this.set = sinon.stub()),
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2023-05-23 04:10:44 -04:00
|
|
|
'@overleaf/metrics': { inc() {} },
|
2021-07-12 12:47:15 -04:00
|
|
|
'@overleaf/settings': {
|
2020-05-06 06:11:36 -04:00
|
|
|
redis: {
|
|
|
|
lock: {
|
|
|
|
key_schema: {
|
2023-03-21 08:06:13 -04:00
|
|
|
blockingKey({ doc_id: docId }) {
|
|
|
|
return `Blocking:${docId}`
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'./Profiler':
|
|
|
|
(this.Profiler = Profiler =
|
|
|
|
(function () {
|
|
|
|
Profiler = class Profiler {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.log = sinon
|
|
|
|
.stub()
|
|
|
|
.returns({ end: sinon.stub() })
|
|
|
|
this.prototype.end = sinon.stub()
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
Profiler.initClass()
|
|
|
|
return Profiler
|
|
|
|
})()),
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2016-01-20 12:36:06 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback = sinon.stub()
|
|
|
|
return (this.doc_id = 'doc-id-123')
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('when the lock is not set', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.lockValue = 'mock-lock-value'
|
|
|
|
this.LockManager.randomLock = sinon.stub().returns(this.lockValue)
|
|
|
|
this.set.callsArgWith(5, null, 'OK')
|
|
|
|
return this.LockManager.tryLock(this.doc_id, this.callback)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should set the lock key with an expiry if it is not set', function () {
|
|
|
|
return this.set
|
|
|
|
.calledWith(`Blocking:${this.doc_id}`, this.lockValue, 'EX', 30, 'NX')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return it('should return the callback with true and the lock value', function () {
|
|
|
|
return this.callback
|
|
|
|
.calledWith(null, true, this.lockValue)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('when the lock is already set', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.set.callsArgWith(5, null, null)
|
|
|
|
return this.LockManager.tryLock(this.doc_id, this.callback)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return it('should return the callback with false', function () {
|
|
|
|
return this.callback.calledWith(null, false).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2017-07-12 05:45:44 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return describe('when it takes a long time for redis to set the lock', function () {
|
|
|
|
beforeEach(function () {
|
2022-05-18 04:22:20 -04:00
|
|
|
tk.freeze(Date.now())
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lockValue = 'mock-lock-value'
|
|
|
|
this.LockManager.randomLock = sinon.stub().returns(this.lockValue)
|
|
|
|
this.LockManager.releaseLock = sinon.stub().callsArgWith(2, null)
|
2022-05-18 04:22:20 -04:00
|
|
|
this.set.callsFake((_key, _v, _ex, _ttl, _nx, cb) => {
|
|
|
|
tk.freeze(Date.now() + 7000)
|
|
|
|
cb(null, 'OK')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
after(function () {
|
|
|
|
tk.reset()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2017-07-12 05:45:44 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('in all cases', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
return this.LockManager.tryLock(this.doc_id, this.callback)
|
|
|
|
})
|
2017-07-12 05:45:44 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should set the lock key with an expiry if it is not set', function () {
|
|
|
|
return this.set
|
|
|
|
.calledWith(`Blocking:${this.doc_id}`, this.lockValue, 'EX', 30, 'NX')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
2017-07-12 05:45:44 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return it('should try to release the lock', function () {
|
|
|
|
return this.LockManager.releaseLock
|
|
|
|
.calledWith(this.doc_id, this.lockValue)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2017-07-12 05:45:44 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('if the lock is released successfully', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.LockManager.releaseLock = sinon.stub().callsArgWith(2, null)
|
|
|
|
return this.LockManager.tryLock(this.doc_id, this.callback)
|
|
|
|
})
|
2017-07-12 05:45:44 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return it('should return the callback with false', function () {
|
|
|
|
return this.callback.calledWith(null, false).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2017-07-12 05:45:44 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return describe('if the lock has already timed out', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.LockManager.releaseLock = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, new Error('tried to release timed out lock'))
|
|
|
|
return this.LockManager.tryLock(this.doc_id, this.callback)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return the callback with an error', function () {
|
2020-05-15 14:29:49 -04:00
|
|
|
return this.callback
|
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
|
|
|
.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|