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 - getting 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:->
|
2016-03-09 07:51:19 -05:00
|
|
|
"settings-sharelatex":{redis:{}}
|
2018-02-21 08:17:51 -05:00
|
|
|
"metrics-sharelatex":
|
|
|
|
inc:->
|
|
|
|
gauge:->
|
2016-03-09 07:51:19 -05:00
|
|
|
|
2014-11-25 10:26:33 -05:00
|
|
|
@callback = sinon.stub()
|
2018-02-20 08:58:09 -05:00
|
|
|
@key = "lock:web:lockName:project-id}"
|
|
|
|
@namespace = 'lockName'
|
|
|
|
|
2014-11-25 10:26:33 -05:00
|
|
|
describe "when the lock is not set", ->
|
|
|
|
beforeEach (done) ->
|
2018-02-20 08:58:09 -05:00
|
|
|
@LockManager._tryLock = sinon.stub().yields(null, true)
|
|
|
|
@LockManager._getLock @key, @namespace, (args...) =>
|
2014-11-25 10:26:33 -05:00
|
|
|
@callback(args...)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should try to get the lock", ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@LockManager._tryLock
|
2018-02-20 08:58:09 -05:00
|
|
|
.calledWith(@key, @namespace)
|
2014-11-25 10:26:33 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should only need to try once", ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@LockManager._tryLock.callCount.should.equal 1
|
2014-11-25 10:26:33 -05:00
|
|
|
|
|
|
|
it "should return the callback", ->
|
|
|
|
@callback.calledWith(null).should.equal true
|
|
|
|
|
|
|
|
describe "when the lock is initially set", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
startTime = Date.now()
|
2015-05-18 04:14:02 -04:00
|
|
|
tries = 0
|
2014-11-25 10:26:33 -05:00
|
|
|
@LockManager.LOCK_TEST_INTERVAL = 5
|
2018-02-20 08:58:09 -05:00
|
|
|
@LockManager._tryLock = (key, namespace, callback = (error, isFree) ->) ->
|
2015-05-18 04:14:02 -04:00
|
|
|
if (Date.now() - startTime < 20) or (tries < 2)
|
|
|
|
tries = tries + 1
|
2014-11-25 10:26:33 -05:00
|
|
|
callback null, false
|
|
|
|
else
|
|
|
|
callback null, true
|
2018-02-01 10:31:42 -05:00
|
|
|
sinon.spy @LockManager, "_tryLock"
|
2014-11-25 10:26:33 -05:00
|
|
|
|
2018-02-20 08:58:09 -05:00
|
|
|
@LockManager._getLock @key, @namespace, (args...) =>
|
2014-11-25 10:26:33 -05:00
|
|
|
@callback(args...)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should call tryLock multiple times until free", ->
|
2018-02-01 10:31:42 -05:00
|
|
|
(@LockManager._tryLock.callCount > 1).should.equal true
|
2014-11-25 10:26:33 -05:00
|
|
|
|
|
|
|
it "should return the callback", ->
|
|
|
|
@callback.calledWith(null).should.equal true
|
|
|
|
|
|
|
|
describe "when the lock times out", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
time = Date.now()
|
|
|
|
@LockManager.MAX_LOCK_WAIT_TIME = 5
|
2018-02-20 08:58:09 -05:00
|
|
|
@LockManager._tryLock = sinon.stub().yields(null, false)
|
|
|
|
@LockManager._getLock @key, @namespace, (args...) =>
|
2014-11-25 10:26:33 -05:00
|
|
|
@callback(args...)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return the callback with an error", ->
|
|
|
|
@callback.calledWith(new Error("timeout")).should.equal true
|