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:->
|
2014-10-07 07:08:36 -04:00
|
|
|
"redis-sharelatex":
|
2014-02-12 05:40:42 -05:00
|
|
|
createClient : () =>
|
|
|
|
auth:->
|
|
|
|
set: @set = sinon.stub()
|
2016-01-20 12:36:06 -05:00
|
|
|
"./Metrics": {inc: () ->}
|
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
@callback = sinon.stub()
|
|
|
|
@doc_id = "doc-id-123"
|
|
|
|
|
|
|
|
describe "when the lock is not set", ->
|
|
|
|
beforeEach ->
|
2016-04-13 06:59:56 -04:00
|
|
|
@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", ->
|
2016-04-13 06:59:56 -04:00
|
|
|
@set.calledWith("Blocking:#{@doc_id}", @lockValue, "EX", 30, "NX")
|
2014-02-12 05:40:42 -05:00
|
|
|
.should.equal true
|
|
|
|
|
2016-04-13 06:59:56 -04:00
|
|
|
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
|
|
|
|
|