LockManagerTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* eslint-disable
  2. camelcase,
  3. mocha/no-nested-tests,
  4. no-return-assign,
  5. no-undef,
  6. no-unused-vars,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS206: Consider reworking classes to avoid initClass
  15. * DS207: Consider shorter variations of null checks
  16. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  17. */
  18. import async from 'async'
  19. import sinon from 'sinon'
  20. import { expect } from 'chai'
  21. import { strict as esmock } from 'esmock'
  22. const MODULE_PATH = '../../../../app/js/LockManager.js'
  23. describe('LockManager', function () {
  24. beforeEach(async function () {
  25. let Timer
  26. this.Settings = {
  27. redis: {
  28. lock: {},
  29. },
  30. }
  31. this.rclient = {
  32. auth: sinon.stub(),
  33. del: sinon.stub().yields(),
  34. eval: sinon.stub(),
  35. exists: sinon.stub(),
  36. set: sinon.stub(),
  37. }
  38. this.RedisWrapper = {
  39. createClient: sinon.stub().returns(this.rclient),
  40. }
  41. this.Metrics = {
  42. inc: sinon.stub(),
  43. gauge: sinon.stub(),
  44. Timer: (Timer = (function () {
  45. Timer = class Timer {
  46. static initClass() {
  47. this.prototype.done = sinon.stub()
  48. }
  49. }
  50. Timer.initClass()
  51. return Timer
  52. })()),
  53. }
  54. this.logger = {
  55. debug: sinon.stub(),
  56. }
  57. this.LockManager = await esmock(MODULE_PATH, {
  58. '@overleaf/redis-wrapper': this.RedisWrapper,
  59. '@overleaf/settings': this.Settings,
  60. '@overleaf/metrics': this.Metrics,
  61. '@overleaf/logger': this.logger,
  62. })
  63. this.key = 'lock-key'
  64. this.callback = sinon.stub()
  65. this.clock = sinon.useFakeTimers()
  66. })
  67. afterEach(function () {
  68. this.clock.restore()
  69. })
  70. describe('checkLock', function () {
  71. describe('when the lock is taken', function () {
  72. beforeEach(function () {
  73. this.rclient.exists.yields(null, '1')
  74. return this.LockManager.checkLock(this.key, this.callback)
  75. })
  76. it('should check the lock in redis', function () {
  77. return this.rclient.exists.calledWith(this.key).should.equal(true)
  78. })
  79. return it('should return the callback with false', function () {
  80. return this.callback.calledWith(null, false).should.equal(true)
  81. })
  82. })
  83. return describe('when the lock is free', function () {
  84. beforeEach(function () {
  85. this.rclient.exists.yields(null, '0')
  86. return this.LockManager.checkLock(this.key, this.callback)
  87. })
  88. return it('should return the callback with true', function () {
  89. return this.callback.calledWith(null, true).should.equal(true)
  90. })
  91. })
  92. })
  93. describe('tryLock', function () {
  94. describe('when the lock is taken', function () {
  95. beforeEach(function () {
  96. this.rclient.set.yields(null, null)
  97. this.LockManager._mocks.randomLock = sinon
  98. .stub()
  99. .returns('locked-random-value')
  100. return this.LockManager.tryLock(this.key, this.callback)
  101. })
  102. it('should check the lock in redis', function () {
  103. return this.rclient.set.should.have.been.calledWith(
  104. this.key,
  105. 'locked-random-value',
  106. 'EX',
  107. this.LockManager.LOCK_TTL,
  108. 'NX'
  109. )
  110. })
  111. return it('should return the callback with false', function () {
  112. return this.callback.calledWith(null, false).should.equal(true)
  113. })
  114. })
  115. return describe('when the lock is free', function () {
  116. beforeEach(function () {
  117. this.rclient.set.yields(null, 'OK')
  118. return this.LockManager.tryLock(this.key, this.callback)
  119. })
  120. return it('should return the callback with true', function () {
  121. return this.callback.calledWith(null, true).should.equal(true)
  122. })
  123. })
  124. })
  125. describe('deleteLock', function () {
  126. return beforeEach(function () {
  127. beforeEach(function () {
  128. return this.LockManager.deleteLock(this.key, this.callback)
  129. })
  130. it('should delete the lock in redis', function () {
  131. return this.rclient.del.calledWith(key).should.equal(true)
  132. })
  133. return it('should call the callback', function () {
  134. return this.callback.called.should.equal(true)
  135. })
  136. })
  137. })
  138. describe('getLock', function () {
  139. describe('when the lock is not taken', function () {
  140. beforeEach(function (done) {
  141. this.LockManager._mocks.tryLock = sinon.stub().yields(null, true)
  142. return this.LockManager.getLock(this.key, (...args) => {
  143. this.callback(...Array.from(args || []))
  144. return done()
  145. })
  146. })
  147. it('should try to get the lock', function () {
  148. return this.LockManager._mocks.tryLock
  149. .calledWith(this.key)
  150. .should.equal(true)
  151. })
  152. it('should only need to try once', function () {
  153. return this.LockManager._mocks.tryLock.callCount.should.equal(1)
  154. })
  155. return it('should return the callback', function () {
  156. return this.callback.calledWith(null).should.equal(true)
  157. })
  158. })
  159. describe('when the lock is initially set', function () {
  160. beforeEach(function (done) {
  161. this.LockManager._mocks.tryLock = sinon.stub()
  162. this.LockManager._mocks.tryLock.onCall(0).yields(null, false)
  163. this.LockManager._mocks.tryLock.onCall(1).yields(null, false)
  164. this.LockManager._mocks.tryLock.onCall(2).yields(null, false)
  165. this.LockManager._mocks.tryLock.onCall(3).yields(null, true)
  166. this.LockManager.getLock(this.key, (...args) => {
  167. this.callback(...args)
  168. return done()
  169. })
  170. this.clock.runAll()
  171. })
  172. it('should call tryLock multiple times until free', function () {
  173. this.LockManager._mocks.tryLock.callCount.should.equal(4)
  174. })
  175. return it('should return the callback', function () {
  176. return this.callback.calledWith(null).should.equal(true)
  177. })
  178. })
  179. return describe('when the lock times out', function () {
  180. beforeEach(function (done) {
  181. const time = Date.now()
  182. this.LockManager._mocks.tryLock = sinon.stub().yields(null, false)
  183. this.LockManager.getLock(this.key, (...args) => {
  184. this.callback(...args)
  185. return done()
  186. })
  187. this.clock.runAll()
  188. })
  189. return it('should return the callback with an error', function () {
  190. return this.callback
  191. .calledWith(sinon.match.instanceOf(Error))
  192. .should.equal(true)
  193. })
  194. })
  195. })
  196. return describe('runWithLock', function () {
  197. describe('with successful run', function () {
  198. beforeEach(function () {
  199. this.result = 'mock-result'
  200. this.runner = sinon.stub().callsFake((extendLock, releaseLock) => {
  201. return releaseLock(null, this.result)
  202. })
  203. this.LockManager._mocks.getLock = sinon.stub().yields()
  204. this.LockManager._mocks.releaseLock = sinon.stub().yields()
  205. return this.LockManager.runWithLock(
  206. this.key,
  207. this.runner,
  208. this.callback
  209. )
  210. })
  211. it('should get the lock', function () {
  212. return this.LockManager._mocks.getLock
  213. .calledWith(this.key)
  214. .should.equal(true)
  215. })
  216. it('should run the passed function', function () {
  217. return this.runner.called.should.equal(true)
  218. })
  219. it('should release the lock', function () {
  220. return this.LockManager._mocks.releaseLock
  221. .calledWith(this.key)
  222. .should.equal(true)
  223. })
  224. return it('should call the callback', function () {
  225. return this.callback.calledWith(null, this.result).should.equal(true)
  226. })
  227. })
  228. describe('when the runner function returns an error', function () {
  229. beforeEach(function () {
  230. this.error = new Error('oops')
  231. this.result = 'mock-result'
  232. this.runner = sinon.stub().callsFake((extendLock, releaseLock) => {
  233. return releaseLock(this.error, this.result)
  234. })
  235. this.LockManager._mocks.getLock = sinon.stub().yields()
  236. this.LockManager._mocks.releaseLock = sinon.stub().yields()
  237. return this.LockManager.runWithLock(
  238. this.key,
  239. this.runner,
  240. this.callback
  241. )
  242. })
  243. it('should release the lock', function () {
  244. return this.LockManager._mocks.releaseLock
  245. .calledWith(this.key)
  246. .should.equal(true)
  247. })
  248. return it('should call the callback with the error', function () {
  249. return this.callback
  250. .calledWith(this.error, this.result)
  251. .should.equal(true)
  252. })
  253. })
  254. describe('extending the lock whilst running', function () {
  255. beforeEach(function () {
  256. this.lockValue = 'lock-value'
  257. this.LockManager._mocks.getLock = sinon
  258. .stub()
  259. .yields(null, this.lockValue)
  260. this.LockManager._mocks.extendLock = sinon.stub().callsArg(2)
  261. this.LockManager._mocks.releaseLock = sinon.stub().callsArg(2)
  262. })
  263. it('should extend the lock if the minimum interval has been passed', function (done) {
  264. const runner = (extendLock, releaseLock) => {
  265. this.clock.tick(this.LockManager.MIN_LOCK_EXTENSION_INTERVAL + 1)
  266. return extendLock(releaseLock)
  267. }
  268. return this.LockManager.runWithLock(this.key, runner, () => {
  269. this.LockManager._mocks.extendLock
  270. .calledWith(this.key, this.lockValue)
  271. .should.equal(true)
  272. return done()
  273. })
  274. })
  275. return it('should not extend the lock if the minimum interval has not been passed', function (done) {
  276. const runner = (extendLock, releaseLock) => {
  277. this.clock.tick(this.LockManager.MIN_LOCK_EXTENSION_INTERVAL - 1)
  278. return extendLock(releaseLock)
  279. }
  280. return this.LockManager.runWithLock(this.key, runner, () => {
  281. this.LockManager._mocks.extendLock.callCount.should.equal(0)
  282. return done()
  283. })
  284. })
  285. })
  286. describe('exceeding the lock ttl', function () {
  287. beforeEach(function () {
  288. this.lockValue = 'lock-value'
  289. this.LockManager._mocks.getLock = sinon
  290. .stub()
  291. .yields(null, this.lockValue)
  292. this.LockManager._mocks.extendLock = sinon.stub().yields()
  293. this.LockManager._mocks.releaseLock = sinon.stub().yields()
  294. return (this.LOCK_TTL_MS = this.LockManager.LOCK_TTL * 1000)
  295. })
  296. it("doesn't log if the ttl wasn't exceeded", function (done) {
  297. const runner = (extendLock, releaseLock) => {
  298. this.clock.tick(this.LOCK_TTL_MS - 1)
  299. return releaseLock()
  300. }
  301. return this.LockManager.runWithLock(this.key, runner, () => {
  302. this.logger.debug.callCount.should.equal(0)
  303. return done()
  304. })
  305. })
  306. it("doesn't log if the lock was extended", function (done) {
  307. const runner = (extendLock, releaseLock) => {
  308. this.clock.tick(this.LOCK_TTL_MS - 1)
  309. return extendLock(() => {
  310. this.clock.tick(2)
  311. return releaseLock()
  312. })
  313. }
  314. return this.LockManager.runWithLock(this.key, runner, () => {
  315. this.logger.debug.callCount.should.equal(0)
  316. return done()
  317. })
  318. })
  319. return it('logs that the excecution exceeded the lock', function (done) {
  320. const runner = (extendLock, releaseLock) => {
  321. this.clock.tick(this.LOCK_TTL_MS + 1)
  322. return releaseLock()
  323. }
  324. return this.LockManager.runWithLock(this.key, runner, () => {
  325. const slowExecutionError = new Error('slow execution during lock')
  326. this.logger.debug
  327. .calledWithMatch('exceeded lock timeout', { key: this.key })
  328. .should.equal(true)
  329. return done()
  330. })
  331. })
  332. })
  333. return describe('releaseLock', function () {
  334. describe('when the lock is current', function () {
  335. beforeEach(function () {
  336. this.rclient.eval.yields(null, 1)
  337. return this.LockManager.releaseLock(
  338. this.key,
  339. this.lockValue,
  340. this.callback
  341. )
  342. })
  343. it('should clear the data from redis', function () {
  344. return this.rclient.eval
  345. .calledWith(
  346. this.LockManager.UNLOCK_SCRIPT,
  347. 1,
  348. this.key,
  349. this.lockValue
  350. )
  351. .should.equal(true)
  352. })
  353. return it('should call the callback', function () {
  354. return this.callback.called.should.equal(true)
  355. })
  356. })
  357. return describe('when the lock has expired', function () {
  358. beforeEach(function () {
  359. this.rclient.eval.yields(null, 0)
  360. return this.LockManager.releaseLock(
  361. this.key,
  362. this.lockValue,
  363. this.callback
  364. )
  365. })
  366. return it('should return an error if the lock has expired', function () {
  367. return this.callback
  368. .calledWith(
  369. sinon.match.has('message', 'tried to release timed out lock')
  370. )
  371. .should.equal(true)
  372. })
  373. })
  374. })
  375. })
  376. })