CheckingTheLock.coffee 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. sinon = require('sinon')
  2. assert = require('assert')
  3. path = require('path')
  4. modulePath = path.join __dirname, '../../../../../app/js/infrastructure/LockManager.js'
  5. project_id = 1234
  6. doc_id = 5678
  7. blockingKey = "Blocking:#{doc_id}"
  8. SandboxedModule = require('sandboxed-module')
  9. describe 'Lock Manager - checking the lock', ()->
  10. existsStub = sinon.stub()
  11. setStub = sinon.stub()
  12. exireStub = sinon.stub()
  13. execStub = sinon.stub()
  14. mocks =
  15. "logger-sharelatex": log:->
  16. "redis-sharelatex":
  17. createClient : ()->
  18. auth:->
  19. multi: ->
  20. exists: existsStub
  21. expire: exireStub
  22. set: setStub
  23. exec: execStub
  24. LockManager = SandboxedModule.require(modulePath, requires: mocks)
  25. it 'should check if lock exists but not set or expire', (done)->
  26. execStub.callsArgWith(0, null, ["1"])
  27. LockManager.checkLock doc_id, (err, docIsLocked)->
  28. existsStub.calledWith(blockingKey).should.equal true
  29. setStub.called.should.equal false
  30. exireStub.called.should.equal false
  31. done()
  32. it 'should return true if the key does not exists', (done)->
  33. execStub.callsArgWith(0, null, "0")
  34. LockManager.checkLock doc_id, (err, free)->
  35. free.should.equal true
  36. done()
  37. it 'should return false if the key does exists', (done)->
  38. execStub.callsArgWith(0, null, "1")
  39. LockManager.checkLock doc_id, (err, free)->
  40. free.should.equal false
  41. done()