2020-02-19 06:15:25 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:15:08 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:15:37 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
2024-11-08 05:21:56 -05:00
|
|
|
const modulePath = require('node:path').join(
|
2020-02-19 06:15:37 -05:00
|
|
|
__dirname,
|
2020-02-19 06:38:54 -05:00
|
|
|
'../../../app/js/DockerLockManager'
|
2020-02-19 06:15:37 -05:00
|
|
|
)
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('LockManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.LockManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-07-13 07:04:48 -04:00
|
|
|
'@overleaf/settings': (this.Settings = { clsi: { docker: {} } }),
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('runWithLock', function () {
|
|
|
|
describe('with a single lock', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback = sinon.stub()
|
|
|
|
return this.LockManager.runWithLock(
|
|
|
|
'lock-one',
|
2021-07-13 07:04:48 -04:00
|
|
|
releaseLock =>
|
2020-02-19 06:15:37 -05:00
|
|
|
setTimeout(() => releaseLock(null, 'hello', 'world'), 100),
|
|
|
|
|
|
|
|
(err, ...args) => {
|
|
|
|
this.callback(err, ...Array.from(args))
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, 'hello', 'world')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with two locks', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback1 = sinon.stub()
|
|
|
|
this.callback2 = sinon.stub()
|
|
|
|
this.LockManager.runWithLock(
|
|
|
|
'lock-one',
|
2021-07-13 07:04:48 -04:00
|
|
|
releaseLock =>
|
2020-02-19 06:15:37 -05:00
|
|
|
setTimeout(() => releaseLock(null, 'hello', 'world', 'one'), 100),
|
|
|
|
|
|
|
|
(err, ...args) => {
|
|
|
|
return this.callback1(err, ...Array.from(args))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return this.LockManager.runWithLock(
|
|
|
|
'lock-two',
|
2021-07-13 07:04:48 -04:00
|
|
|
releaseLock =>
|
2020-02-19 06:15:37 -05:00
|
|
|
setTimeout(() => releaseLock(null, 'hello', 'world', 'two'), 200),
|
|
|
|
|
|
|
|
(err, ...args) => {
|
|
|
|
this.callback2(err, ...Array.from(args))
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the first callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback1
|
|
|
|
.calledWith(null, 'hello', 'world', 'one')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the second callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback2
|
|
|
|
.calledWith(null, 'hello', 'world', 'two')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('with lock contention', function () {
|
|
|
|
describe('where the first lock is released quickly', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.LockManager.MAX_LOCK_WAIT_TIME = 1000
|
|
|
|
this.LockManager.LOCK_TEST_INTERVAL = 100
|
|
|
|
this.callback1 = sinon.stub()
|
|
|
|
this.callback2 = sinon.stub()
|
|
|
|
this.LockManager.runWithLock(
|
|
|
|
'lock',
|
2021-07-13 07:04:48 -04:00
|
|
|
releaseLock =>
|
2020-02-19 06:15:37 -05:00
|
|
|
setTimeout(() => releaseLock(null, 'hello', 'world', 'one'), 100),
|
|
|
|
|
|
|
|
(err, ...args) => {
|
|
|
|
return this.callback1(err, ...Array.from(args))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return this.LockManager.runWithLock(
|
|
|
|
'lock',
|
2021-07-13 07:04:48 -04:00
|
|
|
releaseLock =>
|
2020-02-19 06:15:37 -05:00
|
|
|
setTimeout(() => releaseLock(null, 'hello', 'world', 'two'), 200),
|
|
|
|
|
|
|
|
(err, ...args) => {
|
|
|
|
this.callback2(err, ...Array.from(args))
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the first callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback1
|
|
|
|
.calledWith(null, 'hello', 'world', 'one')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the second callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback2
|
|
|
|
.calledWith(null, 'hello', 'world', 'two')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('where the first lock is held longer than the waiting time', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
let doneTwo
|
|
|
|
this.LockManager.MAX_LOCK_HOLD_TIME = 10000
|
|
|
|
this.LockManager.MAX_LOCK_WAIT_TIME = 1000
|
|
|
|
this.LockManager.LOCK_TEST_INTERVAL = 100
|
|
|
|
this.callback1 = sinon.stub()
|
|
|
|
this.callback2 = sinon.stub()
|
|
|
|
let doneOne = (doneTwo = false)
|
2020-08-10 12:01:11 -04:00
|
|
|
const finish = function (key) {
|
2020-02-19 06:15:37 -05:00
|
|
|
if (key === 1) {
|
|
|
|
doneOne = true
|
|
|
|
}
|
|
|
|
if (key === 2) {
|
|
|
|
doneTwo = true
|
|
|
|
}
|
|
|
|
if (doneOne && doneTwo) {
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.LockManager.runWithLock(
|
|
|
|
'lock',
|
2021-07-13 07:04:48 -04:00
|
|
|
releaseLock =>
|
2020-02-19 06:15:37 -05:00
|
|
|
setTimeout(
|
|
|
|
() => releaseLock(null, 'hello', 'world', 'one'),
|
|
|
|
1100
|
|
|
|
),
|
|
|
|
|
|
|
|
(err, ...args) => {
|
|
|
|
this.callback1(err, ...Array.from(args))
|
|
|
|
return finish(1)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return this.LockManager.runWithLock(
|
|
|
|
'lock',
|
2021-07-13 07:04:48 -04:00
|
|
|
releaseLock =>
|
2020-02-19 06:15:37 -05:00
|
|
|
setTimeout(() => releaseLock(null, 'hello', 'world', 'two'), 100),
|
|
|
|
|
|
|
|
(err, ...args) => {
|
|
|
|
this.callback2(err, ...Array.from(args))
|
|
|
|
return finish(2)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the first callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback1
|
|
|
|
.calledWith(null, 'hello', 'world', 'one')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the second callback with an error', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
const error = sinon.match.instanceOf(Error)
|
|
|
|
return this.callback2.calledWith(error).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('where the first lock is held longer than the max holding time', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
let doneTwo
|
|
|
|
this.LockManager.MAX_LOCK_HOLD_TIME = 1000
|
|
|
|
this.LockManager.MAX_LOCK_WAIT_TIME = 2000
|
|
|
|
this.LockManager.LOCK_TEST_INTERVAL = 100
|
|
|
|
this.callback1 = sinon.stub()
|
|
|
|
this.callback2 = sinon.stub()
|
|
|
|
let doneOne = (doneTwo = false)
|
2020-08-10 12:01:11 -04:00
|
|
|
const finish = function (key) {
|
2020-02-19 06:15:37 -05:00
|
|
|
if (key === 1) {
|
|
|
|
doneOne = true
|
|
|
|
}
|
|
|
|
if (key === 2) {
|
|
|
|
doneTwo = true
|
|
|
|
}
|
|
|
|
if (doneOne && doneTwo) {
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.LockManager.runWithLock(
|
|
|
|
'lock',
|
2021-07-13 07:04:48 -04:00
|
|
|
releaseLock =>
|
2020-02-19 06:15:37 -05:00
|
|
|
setTimeout(
|
|
|
|
() => releaseLock(null, 'hello', 'world', 'one'),
|
|
|
|
1500
|
|
|
|
),
|
|
|
|
|
|
|
|
(err, ...args) => {
|
|
|
|
this.callback1(err, ...Array.from(args))
|
|
|
|
return finish(1)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return this.LockManager.runWithLock(
|
|
|
|
'lock',
|
2021-07-13 07:04:48 -04:00
|
|
|
releaseLock =>
|
2020-02-19 06:15:37 -05:00
|
|
|
setTimeout(() => releaseLock(null, 'hello', 'world', 'two'), 100),
|
|
|
|
|
|
|
|
(err, ...args) => {
|
|
|
|
this.callback2(err, ...Array.from(args))
|
|
|
|
return finish(2)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the first callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback1
|
|
|
|
.calledWith(null, 'hello', 'world', 'one')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the second callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback2
|
|
|
|
.calledWith(null, 'hello', 'world', 'two')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|