first commit
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/* eslint-disable
|
||||
no-unused-vars,
|
||||
*/
|
||||
// TODO: This file was created by bulk-decaffeinate.
|
||||
// Fix any style issues and re-enable lint.
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
const sinon = require('sinon')
|
||||
const assert = require('node:assert')
|
||||
const path = require('node:path')
|
||||
const modulePath = path.join(__dirname, '../../../../app/js/LockManager.js')
|
||||
const projectId = 1234
|
||||
const docId = 5678
|
||||
const blockingKey = `Blocking:${docId}`
|
||||
const SandboxedModule = require('sandboxed-module')
|
||||
|
||||
describe('LockManager - checking the lock', function () {
|
||||
let Profiler
|
||||
const existsStub = sinon.stub()
|
||||
|
||||
const mocks = {
|
||||
'@overleaf/redis-wrapper': {
|
||||
createClient() {
|
||||
return {
|
||||
auth() {},
|
||||
exists: existsStub,
|
||||
}
|
||||
},
|
||||
},
|
||||
'@overleaf/metrics': { inc() {} },
|
||||
'./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
|
||||
})()),
|
||||
}
|
||||
const LockManager = SandboxedModule.require(modulePath, { requires: mocks })
|
||||
|
||||
it('should return true if the key does not exists', function (done) {
|
||||
existsStub.yields(null, '0')
|
||||
return LockManager.checkLock(docId, (err, free) => {
|
||||
if (err) return done(err)
|
||||
free.should.equal(true)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
return it('should return false if the key does exists', function (done) {
|
||||
existsStub.yields(null, '1')
|
||||
return LockManager.checkLock(docId, (err, free) => {
|
||||
if (err) return done(err)
|
||||
free.should.equal(false)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
@@ -0,0 +1,94 @@
|
||||
/* eslint-disable
|
||||
no-return-assign,
|
||||
no-unused-vars,
|
||||
*/
|
||||
// TODO: This file was created by bulk-decaffeinate.
|
||||
// Fix any style issues and re-enable lint.
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
const sinon = require('sinon')
|
||||
const assert = require('node:assert')
|
||||
const path = require('node:path')
|
||||
const modulePath = path.join(__dirname, '../../../../app/js/LockManager.js')
|
||||
const projectId = 1234
|
||||
const docId = 5678
|
||||
const SandboxedModule = require('sandboxed-module')
|
||||
|
||||
describe('LockManager - releasing the lock', function () {
|
||||
beforeEach(function () {
|
||||
let Profiler
|
||||
this.client = {
|
||||
auth() {},
|
||||
eval: sinon.stub(),
|
||||
}
|
||||
const mocks = {
|
||||
'@overleaf/redis-wrapper': {
|
||||
createClient: () => this.client,
|
||||
},
|
||||
'@overleaf/settings': {
|
||||
redis: {
|
||||
lock: {
|
||||
key_schema: {
|
||||
blockingKey({ doc_id: docId }) {
|
||||
return `Blocking:${docId}`
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'@overleaf/metrics': { inc() {} },
|
||||
'./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
|
||||
})()),
|
||||
}
|
||||
this.LockManager = SandboxedModule.require(modulePath, { requires: mocks })
|
||||
this.lockValue = 'lock-value-stub'
|
||||
return (this.callback = sinon.stub())
|
||||
})
|
||||
|
||||
describe('when the lock is current', function () {
|
||||
beforeEach(function () {
|
||||
this.client.eval = sinon.stub().yields(null, 1)
|
||||
return this.LockManager.releaseLock(docId, this.lockValue, this.callback)
|
||||
})
|
||||
|
||||
it('should clear the data from redis', function () {
|
||||
return this.client.eval
|
||||
.calledWith(
|
||||
this.LockManager.unlockScript,
|
||||
1,
|
||||
`Blocking:${docId}`,
|
||||
this.lockValue
|
||||
)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
return it('should call the callback', function () {
|
||||
return this.callback.called.should.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
return describe('when the lock has expired', function () {
|
||||
beforeEach(function () {
|
||||
this.client.eval = sinon.stub().yields(null, 0)
|
||||
return this.LockManager.releaseLock(docId, this.lockValue, this.callback)
|
||||
})
|
||||
|
||||
return it('should return an error if the lock has expired', function () {
|
||||
return this.callback
|
||||
.calledWith(sinon.match.instanceOf(Error))
|
||||
.should.equal(true)
|
||||
})
|
||||
})
|
||||
})
|
@@ -0,0 +1,126 @@
|
||||
/* eslint-disable
|
||||
no-return-assign,
|
||||
no-unused-vars,
|
||||
*/
|
||||
// TODO: This file was created by bulk-decaffeinate.
|
||||
// Fix any style issues and re-enable lint.
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
const sinon = require('sinon')
|
||||
const modulePath = '../../../../app/js/LockManager.js'
|
||||
const SandboxedModule = require('sandboxed-module')
|
||||
|
||||
describe('LockManager - getting the lock', function () {
|
||||
beforeEach(function () {
|
||||
let Profiler
|
||||
this.LockManager = SandboxedModule.require(modulePath, {
|
||||
requires: {
|
||||
'@overleaf/redis-wrapper': {
|
||||
createClient: () => {
|
||||
return { auth() {} }
|
||||
},
|
||||
},
|
||||
'@overleaf/metrics': { inc() {} },
|
||||
'./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
|
||||
})()),
|
||||
},
|
||||
})
|
||||
this.callback = sinon.stub()
|
||||
return (this.doc_id = 'doc-id-123')
|
||||
})
|
||||
|
||||
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()
|
||||
})
|
||||
})
|
||||
|
||||
it('should try to get the lock', function () {
|
||||
return this.LockManager.tryLock.calledWith(this.doc_id).should.equal(true)
|
||||
})
|
||||
|
||||
it('should only need to try once', function () {
|
||||
return this.LockManager.tryLock.callCount.should.equal(1)
|
||||
})
|
||||
|
||||
return it('should return the callback with the lock value', function () {
|
||||
return this.callback.calledWith(null, this.lockValue).should.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
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
|
||||
this.LockManager.tryLock = (docId, callback) => {
|
||||
if (callback == null) {
|
||||
callback = function () {}
|
||||
}
|
||||
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')
|
||||
|
||||
return this.LockManager.getLock(this.doc_id, (...args) => {
|
||||
this.callback(...Array.from(args || []))
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should call tryLock multiple times until free', function () {
|
||||
return (this.LockManager.tryLock.callCount > 1).should.equal(true)
|
||||
})
|
||||
|
||||
return it('should return the callback with the lock value', function () {
|
||||
return this.callback.calledWith(null, this.lockValue).should.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
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()
|
||||
})
|
||||
})
|
||||
|
||||
return it('should return the callback with an error', function () {
|
||||
return this.callback
|
||||
.calledWith(
|
||||
sinon.match
|
||||
.instanceOf(Error)
|
||||
.and(sinon.match.has('doc_id', this.doc_id))
|
||||
)
|
||||
.should.equal(true)
|
||||
})
|
||||
})
|
||||
})
|
@@ -0,0 +1,155 @@
|
||||
/* eslint-disable
|
||||
no-return-assign,
|
||||
no-unused-vars,
|
||||
*/
|
||||
// TODO: This file was created by bulk-decaffeinate.
|
||||
// Fix any style issues and re-enable lint.
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
const sinon = require('sinon')
|
||||
const modulePath = '../../../../app/js/LockManager.js'
|
||||
const SandboxedModule = require('sandboxed-module')
|
||||
const tk = require('timekeeper')
|
||||
|
||||
describe('LockManager - trying the lock', function () {
|
||||
beforeEach(function () {
|
||||
let Profiler
|
||||
this.LockManager = SandboxedModule.require(modulePath, {
|
||||
requires: {
|
||||
'@overleaf/redis-wrapper': {
|
||||
createClient: () => {
|
||||
return {
|
||||
auth() {},
|
||||
set: (this.set = sinon.stub()),
|
||||
}
|
||||
},
|
||||
},
|
||||
'@overleaf/metrics': { inc() {} },
|
||||
'@overleaf/settings': {
|
||||
redis: {
|
||||
lock: {
|
||||
key_schema: {
|
||||
blockingKey({ doc_id: docId }) {
|
||||
return `Blocking:${docId}`
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'./Profiler':
|
||||
(this.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
|
||||
})()),
|
||||
},
|
||||
})
|
||||
|
||||
this.callback = sinon.stub()
|
||||
return (this.doc_id = 'doc-id-123')
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
return it('should return the callback with true and the lock value', function () {
|
||||
return this.callback
|
||||
.calledWith(null, true, this.lockValue)
|
||||
.should.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
return it('should return the callback with false', function () {
|
||||
return this.callback.calledWith(null, false).should.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
return describe('when it takes a long time for redis to set the lock', function () {
|
||||
beforeEach(function () {
|
||||
tk.freeze(Date.now())
|
||||
this.lockValue = 'mock-lock-value'
|
||||
this.LockManager.randomLock = sinon.stub().returns(this.lockValue)
|
||||
this.LockManager.releaseLock = sinon.stub().callsArgWith(2, null)
|
||||
this.set.callsFake((_key, _v, _ex, _ttl, _nx, cb) => {
|
||||
tk.freeze(Date.now() + 7000)
|
||||
cb(null, 'OK')
|
||||
})
|
||||
})
|
||||
after(function () {
|
||||
tk.reset()
|
||||
})
|
||||
|
||||
describe('in all cases', function () {
|
||||
beforeEach(function () {
|
||||
return this.LockManager.tryLock(this.doc_id, this.callback)
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
return it('should try to release the lock', function () {
|
||||
return this.LockManager.releaseLock
|
||||
.calledWith(this.doc_id, this.lockValue)
|
||||
.should.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
return it('should return the callback with false', function () {
|
||||
return this.callback.calledWith(null, false).should.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
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 () {
|
||||
return this.callback
|
||||
.calledWith(sinon.match.instanceOf(Error))
|
||||
.should.equal(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user