LockManagerTests.coffee 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/LockManager'
  5. Path = require "path"
  6. Errors = require "../../../app/js/Errors"
  7. describe "DockerLockManager", ->
  8. beforeEach ->
  9. @LockManager = SandboxedModule.require modulePath, requires:
  10. "settings-sharelatex": {}
  11. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), err:-> }
  12. "fs":
  13. lstat:sinon.stub().callsArgWith(1)
  14. readdir: sinon.stub().callsArgWith(1)
  15. "lockfile": @Lockfile = {}
  16. @lockFile = "/local/compile/directory/.project-lock"
  17. describe "runWithLock", ->
  18. beforeEach ->
  19. @runner = sinon.stub().callsArgWith(0, null, "foo", "bar")
  20. @callback = sinon.stub()
  21. describe "normally", ->
  22. beforeEach ->
  23. @Lockfile.lock = sinon.stub().callsArgWith(2, null)
  24. @Lockfile.unlock = sinon.stub().callsArgWith(1, null)
  25. @LockManager.runWithLock @lockFile, @runner, @callback
  26. it "should run the compile", ->
  27. @runner
  28. .calledWith()
  29. .should.equal true
  30. it "should call the callback with the response from the compile", ->
  31. @callback
  32. .calledWithExactly(null, "foo", "bar")
  33. .should.equal true
  34. describe "when the project is locked", ->
  35. beforeEach ->
  36. @error = new Error()
  37. @error.code = "EEXIST"
  38. @Lockfile.lock = sinon.stub().callsArgWith(2,@error)
  39. @Lockfile.unlock = sinon.stub().callsArgWith(1, null)
  40. @LockManager.runWithLock @lockFile, @runner, @callback
  41. it "should not run the compile", ->
  42. @runner
  43. .called
  44. .should.equal false
  45. it "should return an error", ->
  46. error = new Errors.AlreadyCompilingError()
  47. @callback
  48. .calledWithExactly(error)
  49. .should.equal true