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

38 lines
1.1 KiB
CoffeeScript
Raw Normal View History

2014-02-12 10:40:42 +00: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 11:08:36 +00:00
"redis-sharelatex":
2014-02-12 10:40:42 +00:00
createClient : () =>
auth:->
set: @set = sinon.stub()
@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", ->
@set.calledWith("Blocking:#{@doc_id}", "locked", "EX", 30, "NX")
2014-02-12 10:40:42 +00: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