LockManagerTests.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. const sinon = require('sinon')
  18. const { expect } = require('chai')
  19. const modulePath = '../../../../app/js/LockManager.js'
  20. const SandboxedModule = require('sandboxed-module')
  21. describe('LockManager', function () {
  22. beforeEach(function () {
  23. this.Settings = {
  24. redis: {
  25. lock: {},
  26. },
  27. }
  28. this.LockManager = SandboxedModule.require(modulePath, {
  29. requires: {
  30. '@overleaf/redis-wrapper': {
  31. createClient: () => {
  32. return (this.rclient = { auth: sinon.stub() })
  33. },
  34. },
  35. '@overleaf/settings': this.Settings,
  36. },
  37. })
  38. this.key = 'lock-key'
  39. return (this.callback = sinon.stub())
  40. })
  41. describe('checkLock', function () {
  42. describe('when the lock is taken', function () {
  43. beforeEach(function () {
  44. this.rclient.exists = sinon.stub().callsArgWith(1, null, '1')
  45. return this.LockManager.checkLock(this.key, this.callback)
  46. })
  47. it('should check the lock in redis', function () {
  48. return this.rclient.exists.calledWith(this.key).should.equal(true)
  49. })
  50. return it('should return the callback with false', function () {
  51. return this.callback.calledWith(null, false).should.equal(true)
  52. })
  53. })
  54. return describe('when the lock is free', function () {
  55. beforeEach(function () {
  56. this.rclient.exists = sinon.stub().callsArgWith(1, null, '0')
  57. return this.LockManager.checkLock(this.key, this.callback)
  58. })
  59. return it('should return the callback with true', function () {
  60. return this.callback.calledWith(null, true).should.equal(true)
  61. })
  62. })
  63. })
  64. describe('tryLock', function () {
  65. describe('when the lock is taken', function () {
  66. beforeEach(function () {
  67. this.rclient.set = sinon.stub().callsArgWith(5, null, null)
  68. this.LockManager.randomLock = sinon
  69. .stub()
  70. .returns('locked-random-value')
  71. return this.LockManager.tryLock(this.key, this.callback)
  72. })
  73. it('should check the lock in redis', function () {
  74. return this.rclient.set
  75. .calledWith(
  76. this.key,
  77. 'locked-random-value',
  78. 'EX',
  79. this.LockManager.LOCK_TTL,
  80. 'NX'
  81. )
  82. .should.equal(true)
  83. })
  84. return it('should return the callback with false', function () {
  85. return this.callback.calledWith(null, false).should.equal(true)
  86. })
  87. })
  88. return describe('when the lock is free', function () {
  89. beforeEach(function () {
  90. this.rclient.set = sinon.stub().callsArgWith(5, null, 'OK')
  91. return this.LockManager.tryLock(this.key, this.callback)
  92. })
  93. return it('should return the callback with true', function () {
  94. return this.callback.calledWith(null, true).should.equal(true)
  95. })
  96. })
  97. })
  98. describe('deleteLock', function () {
  99. return beforeEach(function () {
  100. beforeEach(function () {
  101. this.rclient.del = sinon.stub().callsArg(1)
  102. return this.LockManager.deleteLock(this.key, this.callback)
  103. })
  104. it('should delete the lock in redis', function () {
  105. return this.rclient.del.calledWith(key).should.equal(true)
  106. })
  107. return it('should call the callback', function () {
  108. return this.callback.called.should.equal(true)
  109. })
  110. })
  111. })
  112. describe('getLock', function () {
  113. describe('when the lock is not taken', function () {
  114. beforeEach(function (done) {
  115. this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, true)
  116. return this.LockManager.getLock(this.key, (...args) => {
  117. this.callback(...Array.from(args || []))
  118. return done()
  119. })
  120. })
  121. it('should try to get the lock', function () {
  122. return this.LockManager.tryLock.calledWith(this.key).should.equal(true)
  123. })
  124. it('should only need to try once', function () {
  125. return this.LockManager.tryLock.callCount.should.equal(1)
  126. })
  127. return it('should return the callback', function () {
  128. return this.callback.calledWith(null).should.equal(true)
  129. })
  130. })
  131. describe('when the lock is initially set', function () {
  132. beforeEach(function (done) {
  133. const startTime = Date.now()
  134. this.LockManager.LOCK_TEST_INTERVAL = 5
  135. this.LockManager.tryLock = function (doc_id, callback) {
  136. if (callback == null) {
  137. callback = function () {}
  138. }
  139. if (Date.now() - startTime < 100) {
  140. return callback(null, false)
  141. } else {
  142. return callback(null, true)
  143. }
  144. }
  145. sinon.spy(this.LockManager, 'tryLock')
  146. return this.LockManager.getLock(this.key, (...args) => {
  147. this.callback(...Array.from(args || []))
  148. return done()
  149. })
  150. })
  151. it('should call tryLock multiple times until free', function () {
  152. return (this.LockManager.tryLock.callCount > 1).should.equal(true)
  153. })
  154. return it('should return the callback', function () {
  155. return this.callback.calledWith(null).should.equal(true)
  156. })
  157. })
  158. return describe('when the lock times out', function () {
  159. beforeEach(function (done) {
  160. const time = Date.now()
  161. this.LockManager.MAX_LOCK_WAIT_TIME = 5
  162. this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, false)
  163. return this.LockManager.getLock(this.key, (...args) => {
  164. this.callback(...Array.from(args || []))
  165. return done()
  166. })
  167. })
  168. return it('should return the callback with an error', function () {
  169. return this.callback
  170. .calledWith(sinon.match.instanceOf(Error))
  171. .should.equal(true)
  172. })
  173. })
  174. })
  175. return describe('runWithLock', function () {
  176. describe('with successful run', function () {
  177. beforeEach(function () {
  178. this.runner = function (releaseLock) {
  179. if (releaseLock == null) {
  180. releaseLock = function () {}
  181. }
  182. return releaseLock()
  183. }
  184. sinon.spy(this, 'runner')
  185. this.LockManager.getLock = sinon.stub().callsArg(1)
  186. this.LockManager.releaseLock = sinon.stub().callsArg(2)
  187. return this.LockManager.runWithLock(
  188. this.key,
  189. this.runner,
  190. this.callback
  191. )
  192. })
  193. it('should get the lock', function () {
  194. return this.LockManager.getLock.calledWith(this.key).should.equal(true)
  195. })
  196. it('should run the passed function', function () {
  197. return this.runner.called.should.equal(true)
  198. })
  199. it('should release the lock', function () {
  200. return this.LockManager.releaseLock
  201. .calledWith(this.key)
  202. .should.equal(true)
  203. })
  204. return it('should call the callback', function () {
  205. return this.callback.called.should.equal(true)
  206. })
  207. })
  208. describe('when the runner function returns an error', function () {
  209. beforeEach(function () {
  210. this.error = new Error('oops')
  211. this.runner = releaseLock => {
  212. if (releaseLock == null) {
  213. releaseLock = function () {}
  214. }
  215. return releaseLock(this.error)
  216. }
  217. sinon.spy(this, 'runner')
  218. this.LockManager.getLock = sinon.stub().callsArg(1)
  219. this.LockManager.releaseLock = sinon.stub().callsArg(2)
  220. return this.LockManager.runWithLock(
  221. this.key,
  222. this.runner,
  223. this.callback
  224. )
  225. })
  226. it('should release the lock', function () {
  227. return this.LockManager.releaseLock
  228. .calledWith(this.key)
  229. .should.equal(true)
  230. })
  231. return it('should call the callback with the error', function () {
  232. return this.callback.calledWith(this.error).should.equal(true)
  233. })
  234. })
  235. return describe('releaseLock', function () {
  236. describe('when the lock is current', function () {
  237. beforeEach(function () {
  238. this.rclient.eval = sinon.stub().yields(null, 1)
  239. return this.LockManager.releaseLock(
  240. this.key,
  241. this.lockValue,
  242. this.callback
  243. )
  244. })
  245. it('should clear the data from redis', function () {
  246. return this.rclient.eval
  247. .calledWith(
  248. this.LockManager.unlockScript,
  249. 1,
  250. this.key,
  251. this.lockValue
  252. )
  253. .should.equal(true)
  254. })
  255. return it('should call the callback', function () {
  256. return this.callback.called.should.equal(true)
  257. })
  258. })
  259. return describe('when the lock has expired', function () {
  260. beforeEach(function () {
  261. this.rclient.eval = sinon.stub().yields(null, 0)
  262. return this.LockManager.releaseLock(
  263. this.key,
  264. this.lockValue,
  265. this.callback
  266. )
  267. })
  268. return it('should return an error if the lock has expired', function () {
  269. return this.callback
  270. .calledWith(
  271. sinon.match.has('message', 'tried to release timed out lock')
  272. )
  273. .should.equal(true)
  274. })
  275. })
  276. })
  277. })
  278. })