tryLockTests.coffee 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. @callback = sinon.stub()
  16. @doc_id = "doc-id-123"
  17. describe "when the lock is not set", ->
  18. beforeEach ->
  19. @set.callsArgWith(5, null, "OK")
  20. @LockManager.tryLock @doc_id, @callback
  21. it "should set the lock key with an expiry if it is not set", ->
  22. @set.calledWith("Blocking:#{@doc_id}", "locked", "EX", 30, "NX")
  23. .should.equal true
  24. it "should return the callback with true", ->
  25. @callback.calledWith(null, true).should.equal true
  26. describe "when the lock is already set", ->
  27. beforeEach ->
  28. @set.callsArgWith(5, null, null)
  29. @LockManager.tryLock @doc_id, @callback
  30. it "should return the callback with false", ->
  31. @callback.calledWith(null, false).should.equal true