tryLockTests.coffee 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 - trying the lock', ->
  7. beforeEach ->
  8. @LockManager = SandboxedModule.require modulePath, requires:
  9. "logger-sharelatex": log:->
  10. "redis-sharelatex":
  11. createClient : () =>
  12. auth:->
  13. set: @set = sinon.stub()
  14. "./Metrics": {inc: () ->}
  15. "settings-sharelatex": {
  16. redis:
  17. lock:
  18. key_schema:
  19. blockingKey: ({doc_id}) -> "Blocking:#{doc_id}"
  20. }
  21. "./Profiler": @Profiler = class Profiler
  22. log: sinon.stub().returns { end: sinon.stub() }
  23. end: sinon.stub()
  24. @callback = sinon.stub()
  25. @doc_id = "doc-id-123"
  26. describe "when the lock is not set", ->
  27. beforeEach ->
  28. @lockValue = "mock-lock-value"
  29. @LockManager.randomLock = sinon.stub().returns @lockValue
  30. @set.callsArgWith(5, null, "OK")
  31. @LockManager.tryLock @doc_id, @callback
  32. it "should set the lock key with an expiry if it is not set", ->
  33. @set.calledWith("Blocking:#{@doc_id}", @lockValue, "EX", 30, "NX")
  34. .should.equal true
  35. it "should return the callback with true and the lock value", ->
  36. @callback.calledWith(null, true, @lockValue).should.equal true
  37. describe "when the lock is already set", ->
  38. beforeEach ->
  39. @set.callsArgWith(5, null, null)
  40. @LockManager.tryLock @doc_id, @callback
  41. it "should return the callback with false", ->
  42. @callback.calledWith(null, false).should.equal true
  43. describe "when it takes a long time for redis to set the lock", ->
  44. beforeEach ->
  45. @Profiler.prototype.end = () -> 7000 # take a long time
  46. @Profiler.prototype.log = sinon.stub().returns { end: @Profiler.prototype.end }
  47. @lockValue = "mock-lock-value"
  48. @LockManager.randomLock = sinon.stub().returns @lockValue
  49. @LockManager.releaseLock = sinon.stub().callsArgWith(2,null)
  50. @set.callsArgWith(5, null, "OK")
  51. describe "in all cases", ->
  52. beforeEach ->
  53. @LockManager.tryLock @doc_id, @callback
  54. it "should set the lock key with an expiry if it is not set", ->
  55. @set.calledWith("Blocking:#{@doc_id}", @lockValue, "EX", 30, "NX")
  56. .should.equal true
  57. it "should try to release the lock", ->
  58. @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true
  59. describe "if the lock is released successfully", ->
  60. beforeEach ->
  61. @LockManager.releaseLock = sinon.stub().callsArgWith(2,null)
  62. @LockManager.tryLock @doc_id, @callback
  63. it "should return the callback with false", ->
  64. @callback.calledWith(null, false).should.equal true
  65. describe "if the lock has already timed out", ->
  66. beforeEach ->
  67. @LockManager.releaseLock = sinon.stub().callsArgWith(2, new Error("tried to release timed out lock"))
  68. @LockManager.tryLock @doc_id, @callback
  69. it "should return the callback with an error", ->
  70. e = new Error("tried to release timed out lock")
  71. @callback.calledWith(e).should.equal true