DockerLockManager.test.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { vi, describe, beforeEach, it } from 'vitest'
  2. import sinon from 'sinon'
  3. import path from 'node:path'
  4. const modulePath = path.join(
  5. import.meta.dirname,
  6. '../../../app/js/DockerLockManager'
  7. )
  8. describe('DockerLockManager', function () {
  9. beforeEach(async function (ctx) {
  10. vi.doMock('@overleaf/settings', () => ({
  11. default: (ctx.Settings = { clsi: { docker: {} } }),
  12. }))
  13. return (ctx.LockManager = (await import(modulePath)).default)
  14. })
  15. return describe('runWithLock', function () {
  16. describe('with a single lock', function () {
  17. beforeEach(async function (ctx) {
  18. await new Promise((resolve, reject) => {
  19. ctx.callback = sinon.stub()
  20. return ctx.LockManager.runWithLock(
  21. 'lock-one',
  22. releaseLock =>
  23. setTimeout(() => releaseLock(null, 'hello', 'world'), 100),
  24. (err, ...args) => {
  25. ctx.callback(err, ...Array.from(args))
  26. return resolve()
  27. }
  28. )
  29. })
  30. })
  31. return it('should call the callback', function (ctx) {
  32. return ctx.callback
  33. .calledWith(null, 'hello', 'world')
  34. .should.equal(true)
  35. })
  36. })
  37. describe('with two locks', function () {
  38. beforeEach(async function (ctx) {
  39. await new Promise((resolve, reject) => {
  40. ctx.callback1 = sinon.stub()
  41. ctx.callback2 = sinon.stub()
  42. ctx.LockManager.runWithLock(
  43. 'lock-one',
  44. releaseLock =>
  45. setTimeout(() => releaseLock(null, 'hello', 'world', 'one'), 100),
  46. (err, ...args) => {
  47. return ctx.callback1(err, ...Array.from(args))
  48. }
  49. )
  50. return ctx.LockManager.runWithLock(
  51. 'lock-two',
  52. releaseLock =>
  53. setTimeout(() => releaseLock(null, 'hello', 'world', 'two'), 200),
  54. (err, ...args) => {
  55. ctx.callback2(err, ...Array.from(args))
  56. return resolve()
  57. }
  58. )
  59. })
  60. })
  61. it('should call the first callback', function (ctx) {
  62. return ctx.callback1
  63. .calledWith(null, 'hello', 'world', 'one')
  64. .should.equal(true)
  65. })
  66. return it('should call the second callback', function (ctx) {
  67. return ctx.callback2
  68. .calledWith(null, 'hello', 'world', 'two')
  69. .should.equal(true)
  70. })
  71. })
  72. return describe('with lock contention', function () {
  73. describe('where the first lock is released quickly', function () {
  74. beforeEach(async function (ctx) {
  75. await new Promise((resolve, reject) => {
  76. ctx.LockManager.MAX_LOCK_WAIT_TIME = 1000
  77. ctx.LockManager.LOCK_TEST_INTERVAL = 100
  78. ctx.callback1 = sinon.stub()
  79. ctx.callback2 = sinon.stub()
  80. ctx.LockManager.runWithLock(
  81. 'lock',
  82. releaseLock =>
  83. setTimeout(
  84. () => releaseLock(null, 'hello', 'world', 'one'),
  85. 100
  86. ),
  87. (err, ...args) => {
  88. return ctx.callback1(err, ...Array.from(args))
  89. }
  90. )
  91. return ctx.LockManager.runWithLock(
  92. 'lock',
  93. releaseLock =>
  94. setTimeout(
  95. () => releaseLock(null, 'hello', 'world', 'two'),
  96. 200
  97. ),
  98. (err, ...args) => {
  99. ctx.callback2(err, ...Array.from(args))
  100. return resolve()
  101. }
  102. )
  103. })
  104. })
  105. it('should call the first callback', function (ctx) {
  106. return ctx.callback1
  107. .calledWith(null, 'hello', 'world', 'one')
  108. .should.equal(true)
  109. })
  110. return it('should call the second callback', function (ctx) {
  111. return ctx.callback2
  112. .calledWith(null, 'hello', 'world', 'two')
  113. .should.equal(true)
  114. })
  115. })
  116. describe('where the first lock is held longer than the waiting time', function () {
  117. beforeEach(async function (ctx) {
  118. await new Promise((resolve, reject) => {
  119. let doneTwo
  120. ctx.LockManager.MAX_LOCK_HOLD_TIME = 10000
  121. ctx.LockManager.MAX_LOCK_WAIT_TIME = 1000
  122. ctx.LockManager.LOCK_TEST_INTERVAL = 100
  123. ctx.callback1 = sinon.stub()
  124. ctx.callback2 = sinon.stub()
  125. let doneOne = (doneTwo = false)
  126. const finish = function (key) {
  127. if (key === 1) {
  128. doneOne = true
  129. }
  130. if (key === 2) {
  131. doneTwo = true
  132. }
  133. if (doneOne && doneTwo) {
  134. return resolve()
  135. }
  136. }
  137. ctx.LockManager.runWithLock(
  138. 'lock',
  139. releaseLock =>
  140. setTimeout(
  141. () => releaseLock(null, 'hello', 'world', 'one'),
  142. 1100
  143. ),
  144. (err, ...args) => {
  145. ctx.callback1(err, ...Array.from(args))
  146. return finish(1)
  147. }
  148. )
  149. return ctx.LockManager.runWithLock(
  150. 'lock',
  151. releaseLock =>
  152. setTimeout(
  153. () => releaseLock(null, 'hello', 'world', 'two'),
  154. 100
  155. ),
  156. (err, ...args) => {
  157. ctx.callback2(err, ...Array.from(args))
  158. return finish(2)
  159. }
  160. )
  161. })
  162. })
  163. it('should call the first callback', function (ctx) {
  164. return ctx.callback1
  165. .calledWith(null, 'hello', 'world', 'one')
  166. .should.equal(true)
  167. })
  168. return it('should call the second callback with an error', function (ctx) {
  169. const error = sinon.match.instanceOf(Error)
  170. return ctx.callback2.calledWith(error).should.equal(true)
  171. })
  172. })
  173. return describe('where the first lock is held longer than the max holding time', function () {
  174. beforeEach(async function (ctx) {
  175. await new Promise((resolve, reject) => {
  176. let doneTwo
  177. ctx.LockManager.MAX_LOCK_HOLD_TIME = 1000
  178. ctx.LockManager.MAX_LOCK_WAIT_TIME = 2000
  179. ctx.LockManager.LOCK_TEST_INTERVAL = 100
  180. ctx.callback1 = sinon.stub()
  181. ctx.callback2 = sinon.stub()
  182. let doneOne = (doneTwo = false)
  183. const finish = function (key) {
  184. if (key === 1) {
  185. doneOne = true
  186. }
  187. if (key === 2) {
  188. doneTwo = true
  189. }
  190. if (doneOne && doneTwo) {
  191. return resolve()
  192. }
  193. }
  194. ctx.LockManager.runWithLock(
  195. 'lock',
  196. releaseLock =>
  197. setTimeout(
  198. () => releaseLock(null, 'hello', 'world', 'one'),
  199. 1500
  200. ),
  201. (err, ...args) => {
  202. ctx.callback1(err, ...Array.from(args))
  203. return finish(1)
  204. }
  205. )
  206. return ctx.LockManager.runWithLock(
  207. 'lock',
  208. releaseLock =>
  209. setTimeout(
  210. () => releaseLock(null, 'hello', 'world', 'two'),
  211. 100
  212. ),
  213. (err, ...args) => {
  214. ctx.callback2(err, ...Array.from(args))
  215. return finish(2)
  216. }
  217. )
  218. })
  219. })
  220. it('should call the first callback', function (ctx) {
  221. return ctx.callback1
  222. .calledWith(null, 'hello', 'world', 'one')
  223. .should.equal(true)
  224. })
  225. return it('should call the second callback', function (ctx) {
  226. return ctx.callback2
  227. .calledWith(null, 'hello', 'world', 'two')
  228. .should.equal(true)
  229. })
  230. })
  231. })
  232. })
  233. })