ApplyingUpdatesToADocTests.coffee 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. async = require "async"
  5. mongojs = require "../../../app/js/mongojs"
  6. db = mongojs.db
  7. ObjectId = mongojs.ObjectId
  8. MockWebApi = require "./helpers/MockWebApi"
  9. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  10. describe "Applying updates to a doc", ->
  11. before ->
  12. @lines = ["one", "two", "three"]
  13. @update =
  14. doc: @doc_id
  15. op: [{
  16. i: "one and a half\n"
  17. p: 4
  18. }]
  19. v: 0
  20. @result = ["one", "one and a half", "two", "three"]
  21. describe "when the document is not loaded", ->
  22. before (done) ->
  23. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  24. MockWebApi.insertDoc @project_id, @doc_id, {
  25. lines: @lines
  26. }
  27. sinon.spy MockWebApi, "getDocument"
  28. DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) ->
  29. throw error if error?
  30. setTimeout done, 200
  31. after ->
  32. MockWebApi.getDocument.restore()
  33. it "should load the document from the web API", ->
  34. MockWebApi.getDocument
  35. .calledWith(@project_id, @doc_id)
  36. .should.equal true
  37. it "should update the doc", (done) ->
  38. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  39. doc.lines.should.deep.equal @result
  40. done()
  41. describe "when the document is loaded", ->
  42. before (done) ->
  43. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  44. MockWebApi.insertDoc @project_id, @doc_id, {
  45. lines: @lines
  46. }
  47. DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
  48. throw error if error?
  49. sinon.spy MockWebApi, "getDocument"
  50. DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) ->
  51. throw error if error?
  52. setTimeout done, 200
  53. after ->
  54. MockWebApi.getDocument.restore()
  55. it "should not need to call the web api", ->
  56. MockWebApi.getDocument.called.should.equal false
  57. it "should update the doc", (done) ->
  58. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  59. doc.lines.should.deep.equal @result
  60. done()
  61. describe "when the document has been deleted", ->
  62. describe "when the ops come in a single linear order", ->
  63. before ->
  64. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  65. @lines = ["", "", ""]
  66. MockWebApi.insertDoc @project_id, @doc_id, {
  67. lines: @lines
  68. }
  69. @updates = [
  70. { doc_id: @doc_id, v: 0, op: [i: "h", p: 0 ] }
  71. { doc_id: @doc_id, v: 1, op: [i: "e", p: 1 ] }
  72. { doc_id: @doc_id, v: 2, op: [i: "l", p: 2 ] }
  73. { doc_id: @doc_id, v: 3, op: [i: "l", p: 3 ] }
  74. { doc_id: @doc_id, v: 4, op: [i: "o", p: 4 ] }
  75. { doc_id: @doc_id, v: 5, op: [i: " ", p: 5 ] }
  76. { doc_id: @doc_id, v: 6, op: [i: "w", p: 6 ] }
  77. { doc_id: @doc_id, v: 7, op: [i: "o", p: 7 ] }
  78. { doc_id: @doc_id, v: 8, op: [i: "r", p: 8 ] }
  79. { doc_id: @doc_id, v: 9, op: [i: "l", p: 9 ] }
  80. { doc_id: @doc_id, v: 10, op: [i: "d", p: 10] }
  81. ]
  82. @result = ["hello world", "", ""]
  83. MockWebApi.insertDoc @project_id, @doc_id, {
  84. lines: @lines
  85. }
  86. it "should be able to continue applying updates when the project has been deleted", (done) ->
  87. actions = []
  88. for update in @updates.slice(0,6)
  89. do (update) =>
  90. actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
  91. actions.push (callback) => DocUpdaterClient.deleteDoc @project_id, @doc_id, callback
  92. for update in @updates.slice(6)
  93. do (update) =>
  94. actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
  95. async.series actions, (error) =>
  96. throw error if error?
  97. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  98. doc.lines.should.deep.equal @result
  99. done()
  100. describe "when older ops come in after the delete", ->
  101. before ->
  102. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  103. @lines = ["", "", ""]
  104. MockWebApi.insertDoc @project_id, @doc_id, {
  105. lines: @lines
  106. }
  107. @updates = [
  108. { doc_id: @doc_id, v: 0, op: [i: "h", p: 0 ] }
  109. { doc_id: @doc_id, v: 1, op: [i: "e", p: 1 ] }
  110. { doc_id: @doc_id, v: 2, op: [i: "l", p: 2 ] }
  111. { doc_id: @doc_id, v: 3, op: [i: "l", p: 3 ] }
  112. { doc_id: @doc_id, v: 4, op: [i: "o", p: 4 ] }
  113. { doc_id: @doc_id, v: 0, op: [i: "world", p: 1 ] }
  114. ]
  115. @result = ["hello", "world", ""]
  116. MockWebApi.insertDoc @project_id, @doc_id, {
  117. lines: @lines
  118. }
  119. it "should be able to continue applying updates when the project has been deleted", (done) ->
  120. actions = []
  121. for update in @updates.slice(0,5)
  122. do (update) =>
  123. actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
  124. actions.push (callback) => DocUpdaterClient.deleteDoc @project_id, @doc_id, callback
  125. for update in @updates.slice(5)
  126. do (update) =>
  127. actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
  128. async.series actions, (error) =>
  129. throw error if error?
  130. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  131. doc.lines.should.deep.equal @result
  132. done()
  133. describe "when the mongo array has been trimmed", ->
  134. before ->
  135. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  136. @lines = ["", "", ""]
  137. MockWebApi.insertDoc @project_id, @doc_id, {
  138. lines: @lines
  139. }
  140. @updates = [
  141. { doc_id: @doc_id, v: 0, op: [i: "h", p: 0 ] }
  142. { doc_id: @doc_id, v: 1, op: [i: "e", p: 1 ] }
  143. { doc_id: @doc_id, v: 2, op: [i: "l", p: 2 ] }
  144. { doc_id: @doc_id, v: 3, op: [i: "l", p: 3 ] }
  145. { doc_id: @doc_id, v: 4, op: [i: "o", p: 4 ] }
  146. { doc_id: @doc_id, v: 3, op: [i: "world", p: 4 ] }
  147. ]
  148. @result = ["hello", "world", ""]
  149. MockWebApi.insertDoc @project_id, @doc_id, {
  150. lines: @lines
  151. }
  152. it "should be able to reload the required ops from the trimmed mongo array", (done) ->
  153. actions = []
  154. # Apply first set of ops
  155. for update in @updates.slice(0,5)
  156. do (update) =>
  157. actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
  158. # Delete doc from redis and trim ops back to version 3
  159. actions.push (callback) => DocUpdaterClient.deleteDoc @project_id, @doc_id, callback
  160. actions.push (callback) =>
  161. db.docOps.update({doc_id: ObjectId(@doc_id)}, {$push: docOps: { $each: [], $slice: -2 }}, callback)
  162. # Apply older update back from version 3
  163. for update in @updates.slice(5)
  164. do (update) =>
  165. actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
  166. # Flush ops to mongo
  167. actions.push (callback) => DocUpdaterClient.flushDoc @project_id, @doc_id, callback
  168. async.series actions, (error) =>
  169. throw error if error?
  170. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  171. db.docOps.find {doc_id: ObjectId(@doc_id)}, (error, docOps) =>
  172. # Check mongo array has been trimmed
  173. docOps = docOps[0]
  174. docOps.docOps.length.should.equal 3
  175. # Check ops have all be applied properly
  176. doc.lines.should.deep.equal @result
  177. done()
  178. describe "with a broken update", ->
  179. before (done) ->
  180. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  181. MockWebApi.insertDoc @project_id, @doc_id, {
  182. lines: @lines
  183. }
  184. DocUpdaterClient.sendUpdate @project_id, @doc_id, @undefined, (error) ->
  185. throw error if error?
  186. setTimeout done, 200
  187. it "should not update the doc", (done) ->
  188. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  189. doc.lines.should.deep.equal @lines
  190. done()