DockerLockManagerTests.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module');
  13. const sinon = require('sinon');
  14. require('chai').should();
  15. require("coffee-script");
  16. const modulePath = require('path').join(__dirname, '../../../app/coffee/DockerLockManager');
  17. describe("LockManager", function() {
  18. beforeEach(function() {
  19. return this.LockManager = SandboxedModule.require(modulePath, { requires: {
  20. "settings-sharelatex": (this.Settings =
  21. {clsi: {docker: {}}}),
  22. "logger-sharelatex": (this.logger = { log: sinon.stub(), error: sinon.stub() })
  23. }
  24. });});
  25. return describe("runWithLock", function() {
  26. describe("with a single lock", function() {
  27. beforeEach(function(done) {
  28. this.callback = sinon.stub();
  29. return this.LockManager.runWithLock("lock-one", releaseLock =>
  30. setTimeout(() => releaseLock(null, "hello", "world")
  31. , 100)
  32. , (err, ...args) => {
  33. this.callback(err,...Array.from(args));
  34. return done();
  35. });
  36. });
  37. return it("should call the callback", function() {
  38. return this.callback.calledWith(null,"hello","world").should.equal(true);
  39. });
  40. });
  41. describe("with two locks", function() {
  42. beforeEach(function(done) {
  43. this.callback1 = sinon.stub();
  44. this.callback2 = sinon.stub();
  45. this.LockManager.runWithLock("lock-one", releaseLock =>
  46. setTimeout(() => releaseLock(null, "hello", "world","one")
  47. , 100)
  48. , (err, ...args) => {
  49. return this.callback1(err,...Array.from(args));
  50. });
  51. return this.LockManager.runWithLock("lock-two", releaseLock =>
  52. setTimeout(() => releaseLock(null, "hello", "world","two")
  53. , 200)
  54. , (err, ...args) => {
  55. this.callback2(err,...Array.from(args));
  56. return done();
  57. });
  58. });
  59. it("should call the first callback", function() {
  60. return this.callback1.calledWith(null,"hello","world","one").should.equal(true);
  61. });
  62. return it("should call the second callback", function() {
  63. return this.callback2.calledWith(null,"hello","world","two").should.equal(true);
  64. });
  65. });
  66. return describe("with lock contention", function() {
  67. describe("where the first lock is released quickly", function() {
  68. beforeEach(function(done) {
  69. this.LockManager.MAX_LOCK_WAIT_TIME = 1000;
  70. this.LockManager.LOCK_TEST_INTERVAL = 100;
  71. this.callback1 = sinon.stub();
  72. this.callback2 = sinon.stub();
  73. this.LockManager.runWithLock("lock", releaseLock =>
  74. setTimeout(() => releaseLock(null, "hello", "world","one")
  75. , 100)
  76. , (err, ...args) => {
  77. return this.callback1(err,...Array.from(args));
  78. });
  79. return this.LockManager.runWithLock("lock", releaseLock =>
  80. setTimeout(() => releaseLock(null, "hello", "world","two")
  81. , 200)
  82. , (err, ...args) => {
  83. this.callback2(err,...Array.from(args));
  84. return done();
  85. });
  86. });
  87. it("should call the first callback", function() {
  88. return this.callback1.calledWith(null,"hello","world","one").should.equal(true);
  89. });
  90. return it("should call the second callback", function() {
  91. return this.callback2.calledWith(null,"hello","world","two").should.equal(true);
  92. });
  93. });
  94. describe("where the first lock is held longer than the waiting time", function() {
  95. beforeEach(function(done) {
  96. let doneTwo;
  97. this.LockManager.MAX_LOCK_HOLD_TIME = 10000;
  98. this.LockManager.MAX_LOCK_WAIT_TIME = 1000;
  99. this.LockManager.LOCK_TEST_INTERVAL = 100;
  100. this.callback1 = sinon.stub();
  101. this.callback2 = sinon.stub();
  102. let doneOne = (doneTwo = false);
  103. const finish = function(key) {
  104. if (key === 1) { doneOne = true; }
  105. if (key === 2) { doneTwo = true; }
  106. if (doneOne && doneTwo) { return done(); }
  107. };
  108. this.LockManager.runWithLock("lock", releaseLock =>
  109. setTimeout(() => releaseLock(null, "hello", "world","one")
  110. , 1100)
  111. , (err, ...args) => {
  112. this.callback1(err,...Array.from(args));
  113. return finish(1);
  114. });
  115. return this.LockManager.runWithLock("lock", releaseLock =>
  116. setTimeout(() => releaseLock(null, "hello", "world","two")
  117. , 100)
  118. , (err, ...args) => {
  119. this.callback2(err,...Array.from(args));
  120. return finish(2);
  121. });
  122. });
  123. it("should call the first callback", function() {
  124. return this.callback1.calledWith(null,"hello","world","one").should.equal(true);
  125. });
  126. return it("should call the second callback with an error", function() {
  127. const error = sinon.match.instanceOf(Error);
  128. return this.callback2.calledWith(error).should.equal(true);
  129. });
  130. });
  131. return describe("where the first lock is held longer than the max holding time", function() {
  132. beforeEach(function(done) {
  133. let doneTwo;
  134. this.LockManager.MAX_LOCK_HOLD_TIME = 1000;
  135. this.LockManager.MAX_LOCK_WAIT_TIME = 2000;
  136. this.LockManager.LOCK_TEST_INTERVAL = 100;
  137. this.callback1 = sinon.stub();
  138. this.callback2 = sinon.stub();
  139. let doneOne = (doneTwo = false);
  140. const finish = function(key) {
  141. if (key === 1) { doneOne = true; }
  142. if (key === 2) { doneTwo = true; }
  143. if (doneOne && doneTwo) { return done(); }
  144. };
  145. this.LockManager.runWithLock("lock", releaseLock =>
  146. setTimeout(() => releaseLock(null, "hello", "world","one")
  147. , 1500)
  148. , (err, ...args) => {
  149. this.callback1(err,...Array.from(args));
  150. return finish(1);
  151. });
  152. return this.LockManager.runWithLock("lock", releaseLock =>
  153. setTimeout(() => releaseLock(null, "hello", "world","two")
  154. , 100)
  155. , (err, ...args) => {
  156. this.callback2(err,...Array.from(args));
  157. return finish(2);
  158. });
  159. });
  160. it("should call the first callback", function() {
  161. return this.callback1.calledWith(null,"hello","world","one").should.equal(true);
  162. });
  163. return it("should call the second callback", function() {
  164. return this.callback2.calledWith(null,"hello","world","two").should.equal(true);
  165. });
  166. });
  167. });
  168. });
  169. });