LockManagerTests.js 9.7 KB

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