ReleasingTheLock.coffee 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. require('coffee-script')
  2. sinon = require('sinon')
  3. assert = require('assert')
  4. path = require('path')
  5. modulePath = path.join __dirname, '../../../../app/js/LockManager.js'
  6. project_id = 1234
  7. doc_id = 5678
  8. SandboxedModule = require('sandboxed-module')
  9. describe 'LockManager - releasing the lock', ()->
  10. beforeEach ->
  11. @client = {
  12. auth: ->
  13. eval: sinon.stub()
  14. }
  15. mocks =
  16. "logger-sharelatex":
  17. log:->
  18. error:->
  19. "redis-sharelatex":
  20. createClient : () => @client
  21. "settings-sharelatex": {
  22. redis:
  23. lock:
  24. key_schema:
  25. blockingKey: ({doc_id}) -> "Blocking:#{doc_id}"
  26. }
  27. "./Metrics": {inc: () ->}
  28. "./Profiler": class Profiler
  29. log: sinon.stub().returns { end: sinon.stub() }
  30. end: sinon.stub()
  31. @LockManager = SandboxedModule.require(modulePath, requires: mocks)
  32. @lockValue = "lock-value-stub"
  33. @callback = sinon.stub()
  34. describe "when the lock is current", ->
  35. beforeEach ->
  36. @client.eval = sinon.stub().yields(null, 1)
  37. @LockManager.releaseLock doc_id, @lockValue, @callback
  38. it 'should clear the data from redis', ->
  39. @client.eval.calledWith(@LockManager.unlockScript, 1, "Blocking:#{doc_id}", @lockValue).should.equal true
  40. it 'should call the callback', ->
  41. @callback.called.should.equal true
  42. describe "when the lock has expired", ->
  43. beforeEach ->
  44. @client.eval = sinon.stub().yields(null, 0)
  45. @LockManager.releaseLock doc_id, @lockValue, @callback
  46. it 'should return an error if the lock has expired', ->
  47. @callback.calledWith(new Error("tried to release timed out lock")).should.equal true