getLockTests.coffee 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 - getting the lock', ->
  8. beforeEach ->
  9. @LockManager = SandboxedModule.require modulePath, requires:
  10. "logger-sharelatex": log:->
  11. "redis-sharelatex":
  12. createClient : () =>
  13. auth:->
  14. @callback = sinon.stub()
  15. @doc_id = "doc-id-123"
  16. describe "when the lock is not set", ->
  17. beforeEach (done) ->
  18. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, true)
  19. @LockManager.getLock @doc_id, (args...) =>
  20. @callback(args...)
  21. done()
  22. it "should try to get the lock", ->
  23. @LockManager.tryLock
  24. .calledWith(@doc_id)
  25. .should.equal true
  26. it "should only need to try once", ->
  27. @LockManager.tryLock.callCount.should.equal 1
  28. it "should return the callback", ->
  29. @callback.calledWith(null).should.equal true
  30. describe "when the lock is initially set", ->
  31. beforeEach (done) ->
  32. startTime = Date.now()
  33. @LockManager.LOCK_TEST_INTERVAL = 5
  34. @LockManager.tryLock = (doc_id, callback = (error, isFree) ->) ->
  35. if Date.now() - startTime < 20
  36. callback null, false
  37. else
  38. callback null, true
  39. sinon.spy @LockManager, "tryLock"
  40. @LockManager.getLock @doc_id, (args...) =>
  41. @callback(args...)
  42. done()
  43. it "should call tryLock multiple times until free", ->
  44. (@LockManager.tryLock.callCount > 1).should.equal true
  45. it "should return the callback", ->
  46. @callback.calledWith(null).should.equal true
  47. describe "when the lock times out", ->
  48. beforeEach (done) ->
  49. time = Date.now()
  50. @LockManager.MAX_LOCK_WAIT_TIME = 5
  51. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, false)
  52. @LockManager.getLock @doc_id, (args...) =>
  53. @callback(args...)
  54. done()
  55. it "should return the callback with an error", ->
  56. @callback.calledWith(new Error("timeout")).should.equal true