DockerLockManagerTests.js 6.1 KB

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