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

53 lines
1.5 KiB
CoffeeScript
Raw Normal View History

2014-02-12 10:40:42 +00:00
require('coffee-script')
sinon = require('sinon')
assert = require('assert')
path = require('path')
modulePath = path.join __dirname, '../../../../app/js/LockManager.js'
project_id = 1234
doc_id = 5678
SandboxedModule = require('sandboxed-module')
2014-02-12 10:40:42 +00:00
describe 'LockManager - releasing the lock', ()->
2017-03-30 14:31:34 +00:00
beforeEach ->
@client = {
auth: ->
eval: sinon.stub()
}
mocks =
"logger-sharelatex":
log:->
error:->
"redis-sharelatex":
createClient : () => @client
2017-05-08 15:02:40 +00:00
"settings-sharelatex": {
redis:
lock:
key_schema:
blockingKey: ({doc_id}) -> "Blocking:#{doc_id}"
}
2017-03-30 14:31:34 +00:00
"./Metrics": {inc: () ->}
"./Profiler": class Profiler
log: sinon.stub().returns { end: sinon.stub() }
end: sinon.stub()
2017-03-30 14:31:34 +00:00
@LockManager = SandboxedModule.require(modulePath, requires: mocks)
@lockValue = "lock-value-stub"
2014-02-12 10:40:42 +00:00
2017-03-30 14:31:34 +00:00
describe "when the lock is current", ->
beforeEach ->
@client.eval = sinon.stub().yields(null, 1)
@LockManager.releaseLock doc_id, @lockValue, @callback
2014-02-12 10:40:42 +00:00
2017-03-30 14:31:34 +00:00
it 'should clear the data from redis', ->
@client.eval.calledWith(@LockManager.unlockScript, 1, "Blocking:#{doc_id}", @lockValue).should.equal true
2014-02-12 10:40:42 +00:00
2017-03-30 14:31:34 +00:00
it 'should call the callback', ->
@callback.called.should.equal true
describe "when the lock has expired", ->
beforeEach ->
@client.eval = sinon.stub().yields(null, 0)
@LockManager.releaseLock doc_id, @lockValue, @callback
it 'should return an error if the lock has expired', ->
@callback.calledWith(new Error("tried to release timed out lock")).should.equal true