tryLock.test.mjs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { vi } from 'vitest'
  2. /* eslint-disable
  3. max-len,
  4. no-return-assign,
  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. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. import sinon from 'sinon'
  15. import path from 'node:path'
  16. const modulePath = path.join(
  17. import.meta.dirname,
  18. '../../../../../app/src/infrastructure/LockManager.mjs'
  19. )
  20. describe('LockManager - trying the lock', function () {
  21. beforeEach(async function (ctx) {
  22. vi.doMock('../../../../../app/src/infrastructure/RedisWrapper', () => ({
  23. default: {
  24. client: () => {
  25. return {
  26. auth() {},
  27. set: (ctx.set = sinon.stub()),
  28. }
  29. },
  30. },
  31. }))
  32. vi.doMock('@overleaf/settings', () => ({
  33. default: {
  34. redis: {},
  35. lockManager: {
  36. lockTestInterval: 50,
  37. maxTestInterval: 1000,
  38. maxLockWaitTime: 10000,
  39. redisLockExpiry: 30,
  40. slowExecutionThreshold: 5000,
  41. },
  42. },
  43. }))
  44. ctx.LockManager = (await import(modulePath)).default
  45. ctx.callback = sinon.stub()
  46. ctx.key = 'lock:web:lockName:project-id}'
  47. return (ctx.namespace = 'lockName')
  48. })
  49. describe('when the lock is not set', function () {
  50. beforeEach(function (ctx) {
  51. ctx.set.callsArgWith(5, null, 'OK')
  52. ctx.LockManager.randomLock = sinon.stub().returns('random-lock-value')
  53. return ctx.LockManager._tryLock(ctx.key, ctx.namespace, ctx.callback)
  54. })
  55. it('should set the lock key with an expiry if it is not set', function (ctx) {
  56. return ctx.set
  57. .calledWith(ctx.key, 'random-lock-value', 'EX', 30, 'NX')
  58. .should.equal(true)
  59. })
  60. it('should return the callback with true', function (ctx) {
  61. return ctx.callback.calledWith(null, true).should.equal(true)
  62. })
  63. })
  64. describe('when the lock is already set', function () {
  65. beforeEach(function (ctx) {
  66. ctx.set.callsArgWith(5, null, null)
  67. return ctx.LockManager._tryLock(ctx.key, ctx.namespace, ctx.callback)
  68. })
  69. it('should return the callback with false', function (ctx) {
  70. return ctx.callback.calledWith(null, false).should.equal(true)
  71. })
  72. })
  73. })