tryLockTests.coffee 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. "./RedisWrapper":
  12. client: () =>
  13. auth:->
  14. set: @set = sinon.stub()
  15. "settings-sharelatex":{redis:{}}
  16. "metrics-sharelatex": inc:->
  17. @callback = sinon.stub()
  18. @key = "lock:web:lockName:project-id}"
  19. @namespace = "lockName"
  20. describe "when the lock is not set", ->
  21. beforeEach ->
  22. @set.callsArgWith(5, null, "OK")
  23. @LockManager._tryLock @key, @namespace, @callback
  24. it "should set the lock key with an expiry if it is not set", ->
  25. @set.calledWith(@key, "locked", "EX", 30, "NX")
  26. .should.equal true
  27. it "should return the callback with true", ->
  28. @callback.calledWith(null, true).should.equal true
  29. describe "when the lock is already set", ->
  30. beforeEach ->
  31. @set.callsArgWith(5, null, null)
  32. @LockManager._tryLock @key, @namespace, @callback
  33. it "should return the callback with false", ->
  34. @callback.calledWith(null, false).should.equal true