getLockTests.coffee 1.9 KB

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