FlushingDocsTests.coffee 2.5 KB

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