2014-11-25 10:26:33 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
path = require('path')
|
|
|
|
modulePath = path.join __dirname, '../../../../../app/js/infrastructure/LockManager.js'
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe 'LockManager - trying the lock', ->
|
|
|
|
beforeEach ->
|
|
|
|
@LockManager = SandboxedModule.require modulePath, requires:
|
|
|
|
"logger-sharelatex": log:->
|
2017-05-04 10:22:54 -04:00
|
|
|
"./RedisWrapper":
|
|
|
|
client: () =>
|
2014-11-25 10:26:33 -05:00
|
|
|
auth:->
|
|
|
|
set: @set = sinon.stub()
|
2016-03-09 07:51:19 -05:00
|
|
|
"settings-sharelatex":{redis:{}}
|
2017-04-03 11:18:30 -04:00
|
|
|
"metrics-sharelatex": inc:->
|
2014-11-25 10:26:33 -05:00
|
|
|
@callback = sinon.stub()
|
|
|
|
@doc_id = "doc-id-123"
|
|
|
|
|
|
|
|
describe "when the lock is not set", ->
|
|
|
|
beforeEach ->
|
|
|
|
@set.callsArgWith(5, null, "OK")
|
|
|
|
@LockManager.tryLock @doc_id, @callback
|
|
|
|
|
|
|
|
it "should set the lock key with an expiry if it is not set", ->
|
2017-05-11 10:27:01 -04:00
|
|
|
@set.calledWith("lock:web:{#{@doc_id}}", "locked", "EX", 30, "NX")
|
2014-11-25 10:26:33 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the callback with true", ->
|
|
|
|
@callback.calledWith(null, true).should.equal true
|
|
|
|
|
|
|
|
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
|
|
|
|
|