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:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* 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')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('LockManager - getting 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
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2023-05-23 04:10:44 -04:00
|
|
|
'@overleaf/metrics': { inc() {} },
|
2020-05-06 06:11:36 -04:00
|
|
|
'./Profiler': (Profiler = (function () {
|
|
|
|
Profiler = class Profiler {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.log = sinon.stub().returns({ end: sinon.stub() })
|
|
|
|
this.prototype.end = sinon.stub()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Profiler.initClass()
|
|
|
|
return Profiler
|
2021-07-13 07:04:42 -04: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 (done) {
|
|
|
|
this.lockValue = 'mock-lock-value'
|
|
|
|
this.LockManager.tryLock = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, true, this.lockValue)
|
|
|
|
return this.LockManager.getLock(this.doc_id, (...args) => {
|
|
|
|
this.callback(...Array.from(args || []))
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should try to get the lock', function () {
|
|
|
|
return this.LockManager.tryLock.calledWith(this.doc_id).should.equal(true)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should only need to try once', function () {
|
|
|
|
return this.LockManager.tryLock.callCount.should.equal(1)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return it('should return the callback with the lock value', function () {
|
|
|
|
return this.callback.calledWith(null, 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 initially set', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.lockValue = 'mock-lock-value'
|
|
|
|
const startTime = Date.now()
|
|
|
|
let tries = 0
|
|
|
|
this.LockManager.LOCK_TEST_INTERVAL = 5
|
2023-03-21 08:06:13 -04:00
|
|
|
this.LockManager.tryLock = (docId, callback) => {
|
2020-05-06 06:11:36 -04:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
if (Date.now() - startTime < 20 || tries < 2) {
|
|
|
|
tries = tries + 1
|
|
|
|
return callback(null, false)
|
|
|
|
} else {
|
|
|
|
return callback(null, true, this.lockValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sinon.spy(this.LockManager, 'tryLock')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return this.LockManager.getLock(this.doc_id, (...args) => {
|
|
|
|
this.callback(...Array.from(args || []))
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should call tryLock multiple times until free', function () {
|
|
|
|
return (this.LockManager.tryLock.callCount > 1).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 the lock value', function () {
|
|
|
|
return this.callback.calledWith(null, this.lockValue).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return describe('when the lock times out', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
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.doc_id, (...args) => {
|
|
|
|
this.callback(...Array.from(args || []))
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
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)
|
|
|
|
.and(sinon.match.has('doc_id', this.doc_id))
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|