FlushingDocsTests.coffee 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. expect = chai.expect
  5. async = require "async"
  6. MockWebApi = require "./helpers/MockWebApi"
  7. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  8. describe "Flushing a doc to Mongo", ->
  9. before ->
  10. @lines = ["one", "two", "three"]
  11. @version = 42
  12. @update =
  13. doc: @doc_id
  14. op: [{
  15. i: "one and a half\n"
  16. p: 4
  17. }]
  18. v: @version
  19. @result = ["one", "one and a half", "two", "three"]
  20. describe "when the updated doc exists in the doc updater", ->
  21. before (done) ->
  22. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  23. sinon.spy MockWebApi, "setDocument"
  24. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
  25. DocUpdaterClient.sendUpdates @project_id, @doc_id, [@update], (error) =>
  26. throw error if error?
  27. setTimeout () =>
  28. DocUpdaterClient.flushDoc @project_id, @doc_id, done
  29. , 200
  30. after ->
  31. MockWebApi.setDocument.restore()
  32. it "should flush the updated doc lines and version to the web api", ->
  33. MockWebApi.setDocument
  34. .calledWith(@project_id, @doc_id, @result, @version + 1)
  35. .should.equal true
  36. describe "when the doc does not exist in the doc updater", ->
  37. before (done) ->
  38. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  39. MockWebApi.insertDoc @project_id, @doc_id, {
  40. lines: @lines
  41. }
  42. sinon.spy MockWebApi, "setDocument"
  43. DocUpdaterClient.flushDoc @project_id, @doc_id, done
  44. after ->
  45. MockWebApi.setDocument.restore()
  46. it "should not flush the doc to the web api", ->
  47. MockWebApi.setDocument.called.should.equal false
  48. describe "when the web api http request takes a long time", ->
  49. before (done) ->
  50. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  51. @timeout = 10000
  52. MockWebApi.insertDoc @project_id, @doc_id, {
  53. lines: @lines
  54. version: @version
  55. }
  56. sinon.stub MockWebApi, "setDocument", (project_id, doc_id, lines, version, callback = (error) ->) ->
  57. setTimeout callback, 30000
  58. DocUpdaterClient.preloadDoc @project_id, @doc_id, done
  59. after ->
  60. MockWebApi.setDocument.restore()
  61. it "should return quickly(ish)", (done) ->
  62. start = Date.now()
  63. DocUpdaterClient.flushDoc @project_id, @doc_id, (error, res, doc) =>
  64. res.statusCode.should.equal 500
  65. delta = Date.now() - start
  66. expect(delta).to.be.below 20000
  67. done()