LockManagerTests.js 9.6 KB

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