tryLockTests.coffee 1.1 KB

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