tryLockTests.coffee 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. path = require('path')
  5. modulePath = path.join __dirname, '../../../../../app/js/infrastructure/LockManager.js'
  6. SandboxedModule = require('sandboxed-module')
  7. describe 'LockManager - trying the lock', ->
  8. beforeEach ->
  9. @LockManager = SandboxedModule.require modulePath, requires:
  10. "logger-sharelatex": log:->
  11. "redis-sharelatex":
  12. createClient : () =>
  13. auth:->
  14. set: @set = sinon.stub()
  15. "settings-sharelatex":{redis:{}}
  16. "./Metrics": inc:->
  17. @callback = sinon.stub()
  18. @doc_id = "doc-id-123"
  19. describe "when the lock is not set", ->
  20. beforeEach ->
  21. @set.callsArgWith(5, null, "OK")
  22. @LockManager.tryLock @doc_id, @callback
  23. it "should set the lock key with an expiry if it is not set", ->
  24. @set.calledWith("Blocking:#{@doc_id}", "locked", "EX", 30, "NX")
  25. .should.equal true
  26. it "should return the callback with true", ->
  27. @callback.calledWith(null, true).should.equal true
  28. describe "when the lock is already set", ->
  29. beforeEach ->
  30. @set.callsArgWith(5, null, null)
  31. @LockManager.tryLock @doc_id, @callback
  32. it "should return the callback with false", ->
  33. @callback.calledWith(null, false).should.equal true