overleaf/services/web/test/unit/coffee/infrastructure/LockManager/tryLockTests.coffee

42 lines
1.2 KiB
CoffeeScript
Raw Normal View History

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:->
"./RedisWrapper":
client: () =>
auth:->
set: @set = sinon.stub()
"settings-sharelatex":{redis:{}}
"metrics-sharelatex": inc:->
@callback = sinon.stub()
2018-02-20 08:58:09 -05:00
@key = "lock:web:lockName:project-id}"
@namespace = "lockName"
describe "when the lock is not set", ->
beforeEach ->
@set.callsArgWith(5, null, "OK")
2018-02-20 08:58:09 -05:00
@LockManager._tryLock @key, @namespace, @callback
it "should set the lock key with an expiry if it is not set", ->
@set.calledWith(@key, "locked", "EX", 30, "NX")
.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)
2018-02-20 08:58:09 -05:00
@LockManager._tryLock @key, @namespace, @callback
it "should return the callback with false", ->
@callback.calledWith(null, false).should.equal true