LockManagerTests.js 13 KB

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