LockManagerTests.coffee 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/LockManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "LockManager", ->
  8. beforeEach ->
  9. @Settings =
  10. redis:
  11. lock:{}
  12. @LockManager = SandboxedModule.require modulePath, requires:
  13. "redis-sharelatex":
  14. createClient: () => @rclient =
  15. auth: sinon.stub()
  16. "settings-sharelatex": @Settings
  17. "logger-sharelatex": {error: ->}
  18. @key = "lock-key"
  19. @callback = sinon.stub()
  20. describe "checkLock", ->
  21. describe "when the lock is taken", ->
  22. beforeEach ->
  23. @rclient.exists = sinon.stub().callsArgWith(1, null, "1")
  24. @LockManager.checkLock @key, @callback
  25. it "should check the lock in redis", ->
  26. @rclient.exists
  27. .calledWith(@key)
  28. .should.equal true
  29. it "should return the callback with false", ->
  30. @callback.calledWith(null, false).should.equal true
  31. describe "when the lock is free", ->
  32. beforeEach ->
  33. @rclient.exists = sinon.stub().callsArgWith(1, null, "0")
  34. @LockManager.checkLock @key, @callback
  35. it "should return the callback with true", ->
  36. @callback.calledWith(null, true).should.equal true
  37. describe "tryLock", ->
  38. describe "when the lock is taken", ->
  39. beforeEach ->
  40. @rclient.set = sinon.stub().callsArgWith(5, null, null)
  41. @LockManager.randomLock = sinon.stub().returns "locked-random-value"
  42. @LockManager.tryLock @key, @callback
  43. it "should check the lock in redis", ->
  44. @rclient.set
  45. .calledWith(@key, "locked-random-value", "EX", @LockManager.LOCK_TTL, "NX")
  46. .should.equal true
  47. it "should return the callback with false", ->
  48. @callback.calledWith(null, false).should.equal true
  49. describe "when the lock is free", ->
  50. beforeEach ->
  51. @rclient.set = sinon.stub().callsArgWith(5, null, "OK")
  52. @LockManager.tryLock @key, @callback
  53. it "should return the callback with true", ->
  54. @callback.calledWith(null, true).should.equal true
  55. describe "deleteLock", ->
  56. beforeEach ->
  57. beforeEach ->
  58. @rclient.del = sinon.stub().callsArg(1)
  59. @LockManager.deleteLock @key, @callback
  60. it "should delete the lock in redis", ->
  61. @rclient.del
  62. .calledWith(key)
  63. .should.equal true
  64. it "should call the callback", ->
  65. @callback.called.should.equal true
  66. describe "getLock", ->
  67. describe "when the lock is not taken", ->
  68. beforeEach (done) ->
  69. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, true)
  70. @LockManager.getLock @key, (args...) =>
  71. @callback(args...)
  72. done()
  73. it "should try to get the lock", ->
  74. @LockManager.tryLock
  75. .calledWith(@key)
  76. .should.equal true
  77. it "should only need to try once", ->
  78. @LockManager.tryLock.callCount.should.equal 1
  79. it "should return the callback", ->
  80. @callback.calledWith(null).should.equal true
  81. describe "when the lock is initially set", ->
  82. beforeEach (done) ->
  83. startTime = Date.now()
  84. @LockManager.LOCK_TEST_INTERVAL = 5
  85. @LockManager.tryLock = (doc_id, callback = (error, isFree) ->) ->
  86. if Date.now() - startTime < 100
  87. callback null, false
  88. else
  89. callback null, true
  90. sinon.spy @LockManager, "tryLock"
  91. @LockManager.getLock @key, (args...) =>
  92. @callback(args...)
  93. done()
  94. it "should call tryLock multiple times until free", ->
  95. (@LockManager.tryLock.callCount > 1).should.equal true
  96. it "should return the callback", ->
  97. @callback.calledWith(null).should.equal true
  98. describe "when the lock times out", ->
  99. beforeEach (done) ->
  100. time = Date.now()
  101. @LockManager.MAX_LOCK_WAIT_TIME = 5
  102. @LockManager.tryLock = sinon.stub().callsArgWith(1, null, false)
  103. @LockManager.getLock @key, (args...) =>
  104. @callback(args...)
  105. done()
  106. it "should return the callback with an error", ->
  107. @callback.calledWith(sinon.match.instanceOf(Error)).should.equal true
  108. describe "runWithLock", ->
  109. describe "with successful run", ->
  110. beforeEach ->
  111. @runner = (releaseLock = (error) ->) ->
  112. releaseLock()
  113. sinon.spy @, "runner"
  114. @LockManager.getLock = sinon.stub().callsArg(1)
  115. @LockManager.releaseLock = sinon.stub().callsArg(2)
  116. @LockManager.runWithLock @key, @runner, @callback
  117. it "should get the lock", ->
  118. @LockManager.getLock
  119. .calledWith(@key)
  120. .should.equal true
  121. it "should run the passed function", ->
  122. @runner.called.should.equal true
  123. it "should release the lock", ->
  124. @LockManager.releaseLock
  125. .calledWith(@key)
  126. .should.equal true
  127. it "should call the callback", ->
  128. @callback.called.should.equal true
  129. describe "when the runner function returns an error", ->
  130. beforeEach ->
  131. @error = new Error("oops")
  132. @runner = (releaseLock = (error) ->) =>
  133. releaseLock(@error)
  134. sinon.spy @, "runner"
  135. @LockManager.getLock = sinon.stub().callsArg(1)
  136. @LockManager.releaseLock = sinon.stub().callsArg(2)
  137. @LockManager.runWithLock @key, @runner, @callback
  138. it "should release the lock", ->
  139. @LockManager.releaseLock
  140. .calledWith(@key)
  141. .should.equal true
  142. it "should call the callback with the error", ->
  143. @callback.calledWith(@error).should.equal true
  144. describe "releaseLock", ->
  145. describe "when the lock is current", ->
  146. beforeEach ->
  147. @rclient.eval = sinon.stub().yields(null, 1)
  148. @LockManager.releaseLock @key, @lockValue, @callback
  149. it 'should clear the data from redis', ->
  150. @rclient.eval.calledWith(@LockManager.unlockScript, 1, @key, @lockValue).should.equal true
  151. it 'should call the callback', ->
  152. @callback.called.should.equal true
  153. describe "when the lock has expired", ->
  154. beforeEach ->
  155. @rclient.eval = sinon.stub().yields(null, 0)
  156. @LockManager.releaseLock @key, @lockValue, @callback
  157. it 'should return an error if the lock has expired', ->
  158. @callback.calledWith(sinon.match.has('message', "tried to release timed out lock")).should.equal true