2014-11-25 15:26:33 +00: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 14:22:54 +00:00
|
|
|
"./RedisWrapper":
|
|
|
|
client: () =>
|
2014-11-25 15:26:33 +00:00
|
|
|
auth:->
|
|
|
|
set: @set = sinon.stub()
|
2016-03-09 12:51:19 +00:00
|
|
|
"settings-sharelatex":{redis:{}}
|
2018-02-01 15:31:42 +00:00
|
|
|
"metrics-sharelatex": inc:->
|
2014-11-25 15:26:33 +00:00
|
|
|
@callback = sinon.stub()
|
2018-02-20 13:58:09 +00:00
|
|
|
@key = "lock:web:lockName:project-id}"
|
|
|
|
@namespace = "lockName"
|
2018-02-01 15:31:42 +00:00
|
|
|
|
2014-11-25 15:26:33 +00:00
|
|
|
describe "when the lock is not set", ->
|
|
|
|
beforeEach ->
|
|
|
|
@set.callsArgWith(5, null, "OK")
|
2018-05-04 12:22:33 +00:00
|
|
|
@LockManager.randomLock = sinon.stub().returns("random-lock-value")
|
2018-02-20 13:58:09 +00:00
|
|
|
@LockManager._tryLock @key, @namespace, @callback
|
2014-11-25 15:26:33 +00:00
|
|
|
|
|
|
|
it "should set the lock key with an expiry if it is not set", ->
|
2018-05-04 12:22:33 +00:00
|
|
|
@set.calledWith(@key, "random-lock-value", "EX", 30, "NX")
|
2014-11-25 15:26:33 +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)
|
2018-02-20 13:58:09 +00:00
|
|
|
@LockManager._tryLock @key, @namespace, @callback
|
2014-11-25 15:26:33 +00:00
|
|
|
|
|
|
|
it "should return the callback with false", ->
|
|
|
|
@callback.calledWith(null, false).should.equal true
|
|
|
|
|