overleaf/services/document-updater/test/unit/coffee/LockManager/tryLockTests.coffee

87 lines
2.9 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:40:42 -05:00
sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../../app/js/LockManager.js"
SandboxedModule = require('sandboxed-module')
describe 'LockManager - trying the lock', ->
beforeEach ->
@LockManager = SandboxedModule.require modulePath, requires:
"logger-sharelatex": log:->
2016-07-06 06:50:02 -04:00
"redis-sharelatex":
2014-02-12 05:40:42 -05:00
createClient : () =>
auth:->
set: @set = sinon.stub()
"./Metrics": {inc: () ->}
2017-05-08 11:02:40 -04:00
"settings-sharelatex": {
redis:
lock:
key_schema:
blockingKey: ({doc_id}) -> "Blocking:#{doc_id}"
}
2017-07-12 05:45:44 -04:00
"./Profiler": @Profiler = class Profiler
log: sinon.stub().returns { end: sinon.stub() }
end: sinon.stub()
2014-02-12 05:40:42 -05:00
@callback = sinon.stub()
@doc_id = "doc-id-123"
describe "when the lock is not set", ->
beforeEach ->
@lockValue = "mock-lock-value"
@LockManager.randomLock = sinon.stub().returns @lockValue
2014-02-12 05:40:42 -05:00
@set.callsArgWith(5, null, "OK")
@LockManager.tryLock @doc_id, @callback
it "should set the lock key with an expiry if it is not set", ->
@set.calledWith("Blocking:#{@doc_id}", @lockValue, "EX", 30, "NX")
2014-02-12 05:40:42 -05:00
.should.equal true
it "should return the callback with true and the lock value", ->
@callback.calledWith(null, true, @lockValue).should.equal true
2014-02-12 05:40:42 -05:00
describe "when the lock is already set", ->
beforeEach ->
@set.callsArgWith(5, null, null)
@LockManager.tryLock @doc_id, @callback
it "should return the callback with false", ->
@callback.calledWith(null, false).should.equal true
2017-07-12 05:45:44 -04:00
describe "when it takes a long time for redis to set the lock", ->
beforeEach ->
@Profiler.prototype.end = () -> 7000 # take a long time
@Profiler.prototype.log = sinon.stub().returns { end: @Profiler.prototype.end }
@lockValue = "mock-lock-value"
@LockManager.randomLock = sinon.stub().returns @lockValue
@LockManager.releaseLock = sinon.stub().callsArgWith(2,null)
@set.callsArgWith(5, null, "OK")
describe "in all cases", ->
beforeEach ->
@LockManager.tryLock @doc_id, @callback
it "should set the lock key with an expiry if it is not set", ->
@set.calledWith("Blocking:#{@doc_id}", @lockValue, "EX", 30, "NX")
.should.equal true
it "should try to release the lock", ->
@LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
describe "if the lock is released successfully", ->
beforeEach ->
@LockManager.releaseLock = sinon.stub().callsArgWith(2,null)
@LockManager.tryLock @doc_id, @callback
it "should return the callback with false", ->
@callback.calledWith(null, false).should.equal true
describe "if the lock has already timed out", ->
beforeEach ->
@LockManager.releaseLock = sinon.stub().callsArgWith(2, new Error("tried to release timed out lock"))
@LockManager.tryLock @doc_id, @callback
it "should return the callback with an error", ->
e = new Error("tried to release timed out lock")
@callback.calledWith(e).should.equal true