LockManagerTests.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const LockManager = require('../../../app/js/LockManager')
  4. const Errors = require('../../../app/js/Errors')
  5. describe('LockManager', function () {
  6. beforeEach(function () {
  7. this.key = '/local/compile/directory'
  8. this.clock = sinon.useFakeTimers()
  9. })
  10. afterEach(function () {
  11. this.clock.restore()
  12. })
  13. describe('when the lock is available', function () {
  14. it('the lock can be acquired', function () {
  15. const lock = LockManager.acquire(this.key)
  16. expect(lock).to.exist
  17. lock.release()
  18. })
  19. })
  20. describe('after the lock is acquired', function () {
  21. beforeEach(function () {
  22. this.lock = LockManager.acquire(this.key)
  23. })
  24. afterEach(function () {
  25. if (this.lock != null) {
  26. this.lock.release()
  27. }
  28. })
  29. it("the lock can't be acquired again", function () {
  30. expect(() => LockManager.acquire(this.key)).to.throw(
  31. Errors.AlreadyCompilingError
  32. )
  33. })
  34. it('another lock can be acquired', function () {
  35. const lock = LockManager.acquire('another key')
  36. expect(lock).to.exist
  37. lock.release()
  38. })
  39. it('the lock can be acquired again after an expiry period', function () {
  40. // The expiry time is a little bit over 10 minutes. Let's wait 15 minutes.
  41. this.clock.tick(15 * 60 * 1000)
  42. this.lock = LockManager.acquire(this.key)
  43. expect(this.lock).to.exist
  44. })
  45. it('the lock can be acquired again after it was released', function () {
  46. this.lock.release()
  47. this.lock = LockManager.acquire(this.key)
  48. expect(this.lock).to.exist
  49. })
  50. })
  51. })