getLockTests.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS206: Consider reworking classes to avoid initClass
  14. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. const sinon = require('sinon');
  18. const chai = require('chai');
  19. const should = chai.should();
  20. const modulePath = "../../../../app/js/LockManager.js";
  21. const SandboxedModule = require('sandboxed-module');
  22. describe('LockManager - getting the lock', function() {
  23. beforeEach(function() {
  24. let Profiler;
  25. this.LockManager = SandboxedModule.require(modulePath, { requires: {
  26. "logger-sharelatex": { log() {}
  27. },
  28. "redis-sharelatex": {
  29. createClient : () => {
  30. return {auth() {}};
  31. }
  32. },
  33. "./Metrics": {inc() {}},
  34. "./Profiler": (Profiler = (function() {
  35. Profiler = class Profiler {
  36. static initClass() {
  37. this.prototype.log = sinon.stub().returns({ end: sinon.stub() });
  38. this.prototype.end = sinon.stub();
  39. }
  40. };
  41. Profiler.initClass();
  42. return Profiler;
  43. })())
  44. }
  45. }
  46. );
  47. this.callback = sinon.stub();
  48. return this.doc_id = "doc-id-123";
  49. });
  50. describe("when the lock is not set", function() {
  51. beforeEach(function(done) {
  52. this.lockValue = "mock-lock-value";
  53. this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, true, this.lockValue);
  54. return this.LockManager.getLock(this.doc_id, (...args) => {
  55. this.callback(...Array.from(args || []));
  56. return done();
  57. });
  58. });
  59. it("should try to get the lock", function() {
  60. return this.LockManager.tryLock
  61. .calledWith(this.doc_id)
  62. .should.equal(true);
  63. });
  64. it("should only need to try once", function() {
  65. return this.LockManager.tryLock.callCount.should.equal(1);
  66. });
  67. return it("should return the callback with the lock value", function() {
  68. return this.callback.calledWith(null, this.lockValue).should.equal(true);
  69. });
  70. });
  71. describe("when the lock is initially set", function() {
  72. beforeEach(function(done) {
  73. this.lockValue = "mock-lock-value";
  74. const startTime = Date.now();
  75. let tries = 0;
  76. this.LockManager.LOCK_TEST_INTERVAL = 5;
  77. this.LockManager.tryLock = (doc_id, callback) => {
  78. if (callback == null) { callback = function(error, isFree) {}; }
  79. if (((Date.now() - startTime) < 20) || (tries < 2)) {
  80. tries = tries + 1;
  81. return callback(null, false);
  82. } else {
  83. return callback(null, true, this.lockValue);
  84. }
  85. };
  86. sinon.spy(this.LockManager, "tryLock");
  87. return this.LockManager.getLock(this.doc_id, (...args) => {
  88. this.callback(...Array.from(args || []));
  89. return done();
  90. });
  91. });
  92. it("should call tryLock multiple times until free", function() {
  93. return (this.LockManager.tryLock.callCount > 1).should.equal(true);
  94. });
  95. return it("should return the callback with the lock value", function() {
  96. return this.callback.calledWith(null, this.lockValue).should.equal(true);
  97. });
  98. });
  99. return describe("when the lock times out", function() {
  100. beforeEach(function(done) {
  101. const time = Date.now();
  102. this.LockManager.MAX_LOCK_WAIT_TIME = 5;
  103. this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, false);
  104. return this.LockManager.getLock(this.doc_id, (...args) => {
  105. this.callback(...Array.from(args || []));
  106. return done();
  107. });
  108. });
  109. return it("should return the callback with an error", function() {
  110. const e = new Error("Timeout");
  111. e.doc_id = this.doc_id;
  112. return this.callback.calledWith(e).should.equal(true);
  113. });
  114. });
  115. });