LockManagerTests.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * decaffeinate suggestions:
  3. * DS101: Remove unnecessary use of Array.from
  4. * DS102: Remove unnecessary code created because of implicit returns
  5. * DS207: Consider shorter variations of null checks
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. const sinon = require('sinon');
  9. const chai = require('chai');
  10. const should = chai.should();
  11. const { expect } = chai;
  12. const modulePath = "../../../../app/js/LockManager.js";
  13. const SandboxedModule = require('sandboxed-module');
  14. describe("LockManager", function() {
  15. beforeEach(function() {
  16. this.Settings = {
  17. redis: {
  18. lock:{}
  19. }
  20. };
  21. this.LockManager = SandboxedModule.require(modulePath, { requires: {
  22. "redis-sharelatex": {
  23. createClient: () => { return this.rclient =
  24. {auth: sinon.stub()}; }
  25. },
  26. "settings-sharelatex": this.Settings,
  27. "logger-sharelatex": {error() {}}
  28. }
  29. });
  30. this.key = "lock-key";
  31. return this.callback = sinon.stub();
  32. });
  33. describe("checkLock", function() {
  34. describe("when the lock is taken", function() {
  35. beforeEach(function() {
  36. this.rclient.exists = sinon.stub().callsArgWith(1, null, "1");
  37. return this.LockManager.checkLock(this.key, this.callback);
  38. });
  39. it("should check the lock in redis", function() {
  40. return this.rclient.exists
  41. .calledWith(this.key)
  42. .should.equal(true);
  43. });
  44. return it("should return the callback with false", function() {
  45. return this.callback.calledWith(null, false).should.equal(true);
  46. });
  47. });
  48. return describe("when the lock is free", function() {
  49. beforeEach(function() {
  50. this.rclient.exists = sinon.stub().callsArgWith(1, null, "0");
  51. return this.LockManager.checkLock(this.key, this.callback);
  52. });
  53. return it("should return the callback with true", function() {
  54. return this.callback.calledWith(null, true).should.equal(true);
  55. });
  56. });
  57. });
  58. describe("tryLock", function() {
  59. describe("when the lock is taken", function() {
  60. beforeEach(function() {
  61. this.rclient.set = sinon.stub().callsArgWith(5, null, null);
  62. this.LockManager.randomLock = sinon.stub().returns("locked-random-value");
  63. return this.LockManager.tryLock(this.key, this.callback);
  64. });
  65. it("should check the lock in redis", function() {
  66. return this.rclient.set
  67. .calledWith(this.key, "locked-random-value", "EX", this.LockManager.LOCK_TTL, "NX")
  68. .should.equal(true);
  69. });
  70. return it("should return the callback with false", function() {
  71. return this.callback.calledWith(null, false).should.equal(true);
  72. });
  73. });
  74. return describe("when the lock is free", function() {
  75. beforeEach(function() {
  76. this.rclient.set = sinon.stub().callsArgWith(5, null, "OK");
  77. return this.LockManager.tryLock(this.key, this.callback);
  78. });
  79. return it("should return the callback with true", function() {
  80. return this.callback.calledWith(null, true).should.equal(true);
  81. });
  82. });
  83. });
  84. describe("deleteLock", () =>
  85. beforeEach(function() {
  86. beforeEach(function() {
  87. this.rclient.del = sinon.stub().callsArg(1);
  88. return this.LockManager.deleteLock(this.key, this.callback);
  89. });
  90. it("should delete the lock in redis", function() {
  91. return this.rclient.del
  92. .calledWith(key)
  93. .should.equal(true);
  94. });
  95. return it("should call the callback", function() {
  96. return this.callback.called.should.equal(true);
  97. });
  98. })
  99. );
  100. describe("getLock", function() {
  101. describe("when the lock is not taken", function() {
  102. beforeEach(function(done) {
  103. this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, true);
  104. return this.LockManager.getLock(this.key, (...args) => {
  105. this.callback(...Array.from(args || []));
  106. return done();
  107. });
  108. });
  109. it("should try to get the lock", function() {
  110. return this.LockManager.tryLock
  111. .calledWith(this.key)
  112. .should.equal(true);
  113. });
  114. it("should only need to try once", function() {
  115. return this.LockManager.tryLock.callCount.should.equal(1);
  116. });
  117. return it("should return the callback", function() {
  118. return this.callback.calledWith(null).should.equal(true);
  119. });
  120. });
  121. describe("when the lock is initially set", function() {
  122. beforeEach(function(done) {
  123. const startTime = Date.now();
  124. this.LockManager.LOCK_TEST_INTERVAL = 5;
  125. this.LockManager.tryLock = function(doc_id, callback) {
  126. if (callback == null) { callback = function(error, isFree) {}; }
  127. if ((Date.now() - startTime) < 100) {
  128. return callback(null, false);
  129. } else {
  130. return callback(null, true);
  131. }
  132. };
  133. sinon.spy(this.LockManager, "tryLock");
  134. return this.LockManager.getLock(this.key, (...args) => {
  135. this.callback(...Array.from(args || []));
  136. return done();
  137. });
  138. });
  139. it("should call tryLock multiple times until free", function() {
  140. return (this.LockManager.tryLock.callCount > 1).should.equal(true);
  141. });
  142. return it("should return the callback", function() {
  143. return this.callback.calledWith(null).should.equal(true);
  144. });
  145. });
  146. return describe("when the lock times out", function() {
  147. beforeEach(function(done) {
  148. const time = Date.now();
  149. this.LockManager.MAX_LOCK_WAIT_TIME = 5;
  150. this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, false);
  151. return this.LockManager.getLock(this.key, (...args) => {
  152. this.callback(...Array.from(args || []));
  153. return done();
  154. });
  155. });
  156. return it("should return the callback with an error", function() {
  157. return this.callback.calledWith(sinon.match.instanceOf(Error)).should.equal(true);
  158. });
  159. });
  160. });
  161. return describe("runWithLock", function() {
  162. describe("with successful run", function() {
  163. beforeEach(function() {
  164. this.runner = function(releaseLock) {
  165. if (releaseLock == null) { releaseLock = function(error) {}; }
  166. return releaseLock();
  167. };
  168. sinon.spy(this, "runner");
  169. this.LockManager.getLock = sinon.stub().callsArg(1);
  170. this.LockManager.releaseLock = sinon.stub().callsArg(2);
  171. return this.LockManager.runWithLock(this.key, this.runner, this.callback);
  172. });
  173. it("should get the lock", function() {
  174. return this.LockManager.getLock
  175. .calledWith(this.key)
  176. .should.equal(true);
  177. });
  178. it("should run the passed function", function() {
  179. return this.runner.called.should.equal(true);
  180. });
  181. it("should release the lock", function() {
  182. return this.LockManager.releaseLock
  183. .calledWith(this.key)
  184. .should.equal(true);
  185. });
  186. return it("should call the callback", function() {
  187. return this.callback.called.should.equal(true);
  188. });
  189. });
  190. describe("when the runner function returns an error", function() {
  191. beforeEach(function() {
  192. this.error = new Error("oops");
  193. this.runner = releaseLock => {
  194. if (releaseLock == null) { releaseLock = function(error) {}; }
  195. return releaseLock(this.error);
  196. };
  197. sinon.spy(this, "runner");
  198. this.LockManager.getLock = sinon.stub().callsArg(1);
  199. this.LockManager.releaseLock = sinon.stub().callsArg(2);
  200. return this.LockManager.runWithLock(this.key, this.runner, this.callback);
  201. });
  202. it("should release the lock", function() {
  203. return this.LockManager.releaseLock
  204. .calledWith(this.key)
  205. .should.equal(true);
  206. });
  207. return it("should call the callback with the error", function() {
  208. return this.callback.calledWith(this.error).should.equal(true);
  209. });
  210. });
  211. return describe("releaseLock", function() {
  212. describe("when the lock is current", function() {
  213. beforeEach(function() {
  214. this.rclient.eval = sinon.stub().yields(null, 1);
  215. return this.LockManager.releaseLock(this.key, this.lockValue, this.callback);
  216. });
  217. it('should clear the data from redis', function() {
  218. return this.rclient.eval.calledWith(this.LockManager.unlockScript, 1, this.key, this.lockValue).should.equal(true);
  219. });
  220. return it('should call the callback', function() {
  221. return this.callback.called.should.equal(true);
  222. });
  223. });
  224. return describe("when the lock has expired", function() {
  225. beforeEach(function() {
  226. this.rclient.eval = sinon.stub().yields(null, 0);
  227. return this.LockManager.releaseLock(this.key, this.lockValue, this.callback);
  228. });
  229. return it('should return an error if the lock has expired', function() {
  230. return this.callback.calledWith(sinon.match.has('message', "tried to release timed out lock")).should.equal(true);
  231. });
  232. });
  233. });
  234. });
  235. });