tryLockTests.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS206: Consider reworking classes to avoid initClass
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon');
  15. const chai = require('chai');
  16. const should = chai.should();
  17. const modulePath = "../../../../app/js/LockManager.js";
  18. const SandboxedModule = require('sandboxed-module');
  19. describe('LockManager - trying the lock', function() {
  20. beforeEach(function() {
  21. let Profiler;
  22. this.LockManager = SandboxedModule.require(modulePath, { requires: {
  23. "logger-sharelatex": { log() {}
  24. },
  25. "redis-sharelatex": {
  26. createClient : () => {
  27. return {
  28. auth() {},
  29. set: (this.set = sinon.stub())
  30. };
  31. }
  32. },
  33. "./Metrics": {inc() {}},
  34. "settings-sharelatex": {
  35. redis: {
  36. lock: {
  37. key_schema: {
  38. blockingKey({doc_id}) { return `Blocking:${doc_id}`; }
  39. }
  40. }
  41. }
  42. },
  43. "./Profiler": (this.Profiler = (Profiler = (function() {
  44. Profiler = class Profiler {
  45. static initClass() {
  46. this.prototype.log = sinon.stub().returns({ end: sinon.stub() });
  47. this.prototype.end = sinon.stub();
  48. }
  49. };
  50. Profiler.initClass();
  51. return Profiler;
  52. })()))
  53. }
  54. }
  55. );
  56. this.callback = sinon.stub();
  57. return this.doc_id = "doc-id-123";
  58. });
  59. describe("when the lock is not set", function() {
  60. beforeEach(function() {
  61. this.lockValue = "mock-lock-value";
  62. this.LockManager.randomLock = sinon.stub().returns(this.lockValue);
  63. this.set.callsArgWith(5, null, "OK");
  64. return this.LockManager.tryLock(this.doc_id, this.callback);
  65. });
  66. it("should set the lock key with an expiry if it is not set", function() {
  67. return this.set.calledWith(`Blocking:${this.doc_id}`, this.lockValue, "EX", 30, "NX")
  68. .should.equal(true);
  69. });
  70. return it("should return the callback with true and the lock value", function() {
  71. return this.callback.calledWith(null, true, this.lockValue).should.equal(true);
  72. });
  73. });
  74. describe("when the lock is already set", function() {
  75. beforeEach(function() {
  76. this.set.callsArgWith(5, null, null);
  77. return this.LockManager.tryLock(this.doc_id, this.callback);
  78. });
  79. return it("should return the callback with false", function() {
  80. return this.callback.calledWith(null, false).should.equal(true);
  81. });
  82. });
  83. return describe("when it takes a long time for redis to set the lock", function() {
  84. beforeEach(function() {
  85. this.Profiler.prototype.end = () => 7000; // take a long time
  86. this.Profiler.prototype.log = sinon.stub().returns({ end: this.Profiler.prototype.end });
  87. this.lockValue = "mock-lock-value";
  88. this.LockManager.randomLock = sinon.stub().returns(this.lockValue);
  89. this.LockManager.releaseLock = sinon.stub().callsArgWith(2,null);
  90. return this.set.callsArgWith(5, null, "OK");
  91. });
  92. describe("in all cases", function() {
  93. beforeEach(function() {
  94. return this.LockManager.tryLock(this.doc_id, this.callback);
  95. });
  96. it("should set the lock key with an expiry if it is not set", function() {
  97. return this.set.calledWith(`Blocking:${this.doc_id}`, this.lockValue, "EX", 30, "NX")
  98. .should.equal(true);
  99. });
  100. return it("should try to release the lock", function() {
  101. return this.LockManager.releaseLock.calledWith(this.doc_id, this.lockValue).should.equal(true);
  102. });
  103. });
  104. describe("if the lock is released successfully", function() {
  105. beforeEach(function() {
  106. this.LockManager.releaseLock = sinon.stub().callsArgWith(2,null);
  107. return this.LockManager.tryLock(this.doc_id, this.callback);
  108. });
  109. return it("should return the callback with false", function() {
  110. return this.callback.calledWith(null, false).should.equal(true);
  111. });
  112. });
  113. return describe("if the lock has already timed out", function() {
  114. beforeEach(function() {
  115. this.LockManager.releaseLock = sinon.stub().callsArgWith(2, new Error("tried to release timed out lock"));
  116. return this.LockManager.tryLock(this.doc_id, this.callback);
  117. });
  118. return it("should return the callback with an error", function() {
  119. const e = new Error("tried to release timed out lock");
  120. return this.callback.calledWith(e).should.equal(true);
  121. });
  122. });
  123. });
  124. });