MongoHealthCheckTests.coffee 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/MongoHealthCheck'
  5. describe "MongoHealthCheck", ->
  6. beforeEach ->
  7. @MongoHealthCheck = SandboxedModule.require modulePath, requires:
  8. "settings-sharelatex": @Settings = {}
  9. "./PersistenceManager": @PersistenceManager = {}
  10. @doc_id = "mock-doc-id"
  11. @callback = sinon.stub()
  12. describe "isAlive", ->
  13. describe "with no configured doc_id", ->
  14. beforeEach ->
  15. @MongoHealthCheck.isAlive @callback
  16. it "should call the call the callback with an error", ->
  17. @callback.calledOnce.should.equal true
  18. error = @callback.args[0][0]
  19. error.message.should.equal "No test doc_id configured"
  20. describe "when mongo returns within the timeout", ->
  21. beforeEach ->
  22. @Settings.smokeTest =
  23. doc_id: @doc_id
  24. @PersistenceManager.getDocVersionInMongo = sinon.stub().callsArg(1)
  25. @MongoHealthCheck.isAlive @callback
  26. it "should call PersistenceManager.getDocVersionInMongo", ->
  27. @PersistenceManager.getDocVersionInMongo
  28. .calledWith(@doc_id)
  29. .should.equal true
  30. it "should call the call the callback without an error", ->
  31. @callback.calledOnce.should.equal true
  32. @callback.calledWith(null).should.equal true
  33. describe "when mongo does not return within the timeout", ->
  34. beforeEach (done) ->
  35. @Settings.smokeTest =
  36. doc_id: @doc_id
  37. timeout: 50
  38. @PersistenceManager.getDocVersionInMongo = (doc_id, callback) ->
  39. setTimeout callback, 100
  40. @MongoHealthCheck.isAlive (@error) =>
  41. done()
  42. it "should call the call the callback with an error", ->
  43. @error.message.should.equal "Mongo did not return in 50ms"