CheckingTheLock.coffee 1.4 KB

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