DocOpsManagerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/DocOpsManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. ObjectId = require("../../../../app/js/mongojs").ObjectId
  7. describe "DocOpsManager", ->
  8. beforeEach ->
  9. @doc_id = ObjectId().toString()
  10. @project_id = "project-id"
  11. @callback = sinon.stub()
  12. @DocOpsManager = SandboxedModule.require modulePath, requires:
  13. "./RedisManager": @RedisManager = {}
  14. "./mongojs":
  15. db: @db = { docOps: {} }
  16. ObjectId: ObjectId
  17. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  18. "./Metrics": @Metrics =
  19. Timer: class Timer
  20. done: sinon.stub()
  21. "./TrackChangesManager": @TrackChangesManager = {}
  22. describe "flushDocOpsToMongo", ->
  23. describe "when versions are consistent", ->
  24. beforeEach ->
  25. @mongo_version = 40
  26. @redis_version = 42
  27. @ops = [ "mock-op-1", "mock-op-2" ]
  28. @DocOpsManager.getDocVersionInMongo = sinon.stub().callsArgWith(1, null, @mongo_version)
  29. @RedisManager.getDocVersion = sinon.stub().callsArgWith(1, null, @redis_version)
  30. @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
  31. @DocOpsManager._appendDocOpsInMongo = sinon.stub().callsArg(3)
  32. @DocOpsManager.flushDocOpsToMongo @project_id, @doc_id, @callback
  33. it "should get the version from Mongo", ->
  34. @DocOpsManager.getDocVersionInMongo
  35. .calledWith(@doc_id)
  36. .should.equal true
  37. it "should get the version from REdis", ->
  38. @RedisManager.getDocVersion
  39. .calledWith(@doc_id)
  40. .should.equal true
  41. it "should get all doc ops since the version in Mongo", ->
  42. @RedisManager.getPreviousDocOps
  43. .calledWith(@doc_id, @mongo_version, -1)
  44. .should.equal true
  45. it "should update Mongo with the new ops", ->
  46. @DocOpsManager._appendDocOpsInMongo
  47. .calledWith(@doc_id, @ops, @redis_version)
  48. .should.equal true
  49. it "should call the callback", ->
  50. @callback.called.should.equal true
  51. it "should time the execution", ->
  52. @Metrics.Timer::done.called.should.equal true
  53. describe "when the number of ops does not match the difference in versions", ->
  54. beforeEach ->
  55. @mongo_version = 40
  56. @redis_version = 45
  57. @ops = [ "mock-op-1", "mock-op-2" ]
  58. @DocOpsManager.getDocVersionInMongo = sinon.stub().callsArgWith(1, null, @mongo_version)
  59. @RedisManager.getDocVersion = sinon.stub().callsArgWith(1, null, @redis_version)
  60. @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
  61. @DocOpsManager._appendDocOpsInMongo = sinon.stub().callsArg(3)
  62. @DocOpsManager.flushDocOpsToMongo @project_id, @doc_id, @callback
  63. it "should call the callback with an error", ->
  64. @callback.calledWith(new Error("inconsistet versions")).should.equal true
  65. it "should log an error", ->
  66. @logger.error
  67. .calledWith(doc_id: @doc_id, mongoVersion: @mongo_version, redisVersion: @redis_version, opsLength: @ops.length, "version difference does not match ops length")
  68. .should.equal true
  69. it "should not modify mongo", ->
  70. @DocOpsManager._appendDocOpsInMongo.called.should.equal false
  71. it "should time the execution", ->
  72. @Metrics.Timer::done.called.should.equal true
  73. describe "when redis version is behind mongo version", ->
  74. beforeEach ->
  75. @mongo_version = 40
  76. @redis_version = 30
  77. @DocOpsManager.getDocVersionInMongo = sinon.stub().callsArgWith(1, null, @mongo_version)
  78. @RedisManager.getDocVersion = sinon.stub().callsArgWith(1, null, @redis_version)
  79. @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
  80. @DocOpsManager._appendDocOpsInMongo = sinon.stub().callsArg(3)
  81. @DocOpsManager.flushDocOpsToMongo @project_id, @doc_id, @callback
  82. it "should call the callback with an error", ->
  83. @callback.calledWith(new Error("inconsistet versions")).should.equal true
  84. it "should log an error", ->
  85. @logger.error
  86. .calledWith(doc_id: @doc_id, mongoVersion: @mongo_version, redisVersion: @redis_version, "mongo version is ahead of redis")
  87. .should.equal true
  88. it "should not modify mongo", ->
  89. @DocOpsManager._appendDocOpsInMongo.called.should.equal false
  90. it "should time the execution", ->
  91. @Metrics.Timer::done.called.should.equal true
  92. describe "getPreviousDocOps", ->
  93. beforeEach ->
  94. @ops = [ "mock-op-1", "mock-op-2" ]
  95. @start = 30
  96. @end = 32
  97. @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
  98. @DocOpsManager._ensureOpsAreLoaded = sinon.stub().callsArg(3)
  99. @DocOpsManager.getPreviousDocOps @project_id, @doc_id, @start, @end, @callback
  100. it "should ensure the ops are loaded back far enough", ->
  101. @DocOpsManager._ensureOpsAreLoaded
  102. .calledWith(@project_id, @doc_id, @start)
  103. .should.equal true
  104. it "should get the previous doc ops", ->
  105. @RedisManager.getPreviousDocOps
  106. .calledWith(@doc_id, @start, @end)
  107. .should.equal true
  108. it "should call the callback with the ops", ->
  109. @callback.calledWith(null, @ops).should.equal true
  110. it "should time the execution", ->
  111. @Metrics.Timer::done.called.should.equal true
  112. describe "_ensureOpsAreLoaded", ->
  113. describe "when the ops are not loaded", ->
  114. beforeEach ->
  115. @redisVersion = 42
  116. @redisOpsLength = 10
  117. @backToVersion = 30
  118. @ops = [ "mock-op-1", "mock-op-2" ]
  119. @RedisManager.getDocVersion = sinon.stub().callsArgWith(1, null, @redisVersion)
  120. @RedisManager.getDocOpsLength = sinon.stub().callsArgWith(1, null, @redisOpsLength)
  121. @DocOpsManager._getDocOpsFromMongo = sinon.stub().callsArgWith(3, null, @ops)
  122. @RedisManager.prependDocOps = sinon.stub().callsArgWith(2, null)
  123. @DocOpsManager._ensureOpsAreLoaded @project_id, @doc_id, @backToVersion, @callback
  124. it "should get the doc version from redis", ->
  125. @RedisManager.getDocVersion
  126. .calledWith(@doc_id)
  127. .should.equal true
  128. it "should get the doc ops length in redis", ->
  129. @RedisManager.getDocOpsLength
  130. .calledWith(@doc_id)
  131. .should.equal true
  132. it "should get the doc ops that need loading from Mongo", ->
  133. @DocOpsManager._getDocOpsFromMongo
  134. .calledWith(@doc_id, @backToVersion, @redisVersion - @redisOpsLength)
  135. .should.equal true
  136. it "should prepend the retrieved ops to redis", ->
  137. @RedisManager.prependDocOps
  138. .calledWith(@doc_id, @ops)
  139. .should.equal true
  140. it "should call the callback", ->
  141. @callback.called.should.equal true
  142. describe "when the ops are loaded", ->
  143. beforeEach ->
  144. @redisVersion = 42
  145. @redisOpsLength = 10
  146. @backToVersion = 35
  147. @RedisManager.getDocVersion = sinon.stub().callsArgWith(1, null, @redisVersion)
  148. @RedisManager.getDocOpsLength = sinon.stub().callsArgWith(1, null, @redisOpsLength)
  149. @DocOpsManager._getDocOpsFromMongo = sinon.stub().callsArgWith(3, null, @ops)
  150. @RedisManager.prependDocOps = sinon.stub().callsArgWith(2, null)
  151. @DocOpsManager._ensureOpsAreLoaded @project_id, @doc_id, @backToVersion, @callback
  152. it "should not need to get the docs from Mongo or put any into redis", ->
  153. @DocOpsManager._getDocOpsFromMongo.called.should.equal false
  154. @RedisManager.prependDocOps.called.should.equal false
  155. it "should call the callback", ->
  156. @callback.called.should.equal true
  157. describe "getDocVersionInMongo", ->
  158. describe "when the doc exists", ->
  159. beforeEach ->
  160. @doc =
  161. version: @version = 42
  162. @db.docOps.find = sinon.stub().callsArgWith(2, null, [@doc])
  163. @DocOpsManager.getDocVersionInMongo @doc_id, @callback
  164. it "should look for the doc in the database", ->
  165. @db.docOps.find
  166. .calledWith({ doc_id: ObjectId(@doc_id) }, {version: 1})
  167. .should.equal true
  168. it "should call the callback with the version", ->
  169. @callback.calledWith(null, @version).should.equal true
  170. describe "when the doc doesn't exist", ->
  171. beforeEach ->
  172. @db.docOps.find = sinon.stub().callsArgWith(2, null, [])
  173. @DocOpsManager.getDocVersionInMongo @doc_id, @callback
  174. it "should call the callback with 0", ->
  175. @callback.calledWith(null, 0).should.equal true
  176. describe "_appendDocOpsInMongo", ->
  177. describe "with a small set of updates", ->
  178. beforeEach (done) ->
  179. @ops = [ "mock-op-1", "mock-op-2" ]
  180. @version = 42
  181. @db.docOps.update = sinon.stub().callsArg(3)
  182. @DocOpsManager._appendDocOpsInMongo @doc_id, @ops, @version, (error) =>
  183. @callback(error)
  184. done()
  185. it "should update the database", ->
  186. @db.docOps.update
  187. .calledWith({
  188. doc_id: ObjectId(@doc_id)
  189. }, {
  190. $push: docOps: { $each: @ops, $slice: -100 }
  191. $set: version: @version
  192. }, {
  193. upsert: true
  194. })
  195. .should.equal true
  196. it "should call the callbak", ->
  197. @callback.called.should.equal true
  198. describe "with a large set of updates", ->
  199. beforeEach (done) ->
  200. @ops = [ "mock-op-1", "mock-op-2", "mock-op-3", "mock-op-4", "mock-op-5" ]
  201. @version = 42
  202. @DocOpsManager.APPEND_OPS_BATCH_SIZE = 2
  203. @db.docOps.update = sinon.stub().callsArg(3)
  204. @DocOpsManager._appendDocOpsInMongo @doc_id, @ops, @version, (error) =>
  205. @callback(error)
  206. done()
  207. it "should update the database in batches", ->
  208. @db.docOps.update
  209. .calledWith({ doc_id: ObjectId(@doc_id) }, {
  210. $push: docOps: { $each: @ops.slice(0,2), $slice: -100 }
  211. $set: version: @version - 3
  212. }, { upsert: true })
  213. .should.equal true
  214. @db.docOps.update
  215. .calledWith({ doc_id: ObjectId(@doc_id) }, {
  216. $push: docOps: { $each: @ops.slice(2,4), $slice: -100 }
  217. $set: version: @version - 1
  218. }, { upsert: true })
  219. .should.equal true
  220. @db.docOps.update
  221. .calledWith({ doc_id: ObjectId(@doc_id) }, {
  222. $push: docOps: { $each: @ops.slice(4,5), $slice: -100 }
  223. $set: version: @version
  224. }, { upsert: true })
  225. .should.equal true
  226. it "should call the callbak", ->
  227. @callback.called.should.equal true
  228. describe "with no updates", ->
  229. beforeEach (done) ->
  230. @ops = []
  231. @version = 42
  232. @db.docOps.update = sinon.stub().callsArg(3)
  233. @DocOpsManager._appendDocOpsInMongo @doc_id, @ops, @version, (error) =>
  234. @callback(error)
  235. done()
  236. it "should not try to update the database", ->
  237. @db.docOps.update.called.should.equal false
  238. describe "_getDocsOpsFromMongo", ->
  239. beforeEach ->
  240. @version = 42
  241. @start = 32
  242. @limit = 5
  243. @doc =
  244. docOps: ["mock-ops"]
  245. @DocOpsManager.getDocVersionInMongo = sinon.stub().callsArgWith(1, null, @version)
  246. @db.docOps.find = sinon.stub().callsArgWith(2, null, [@doc])
  247. @DocOpsManager._getDocOpsFromMongo @doc_id, @start, @start + @limit, @callback
  248. it "should get the current version", ->
  249. @DocOpsManager.getDocVersionInMongo
  250. .calledWith(@doc_id)
  251. .should.equal true
  252. it "should get the doc ops", ->
  253. @db.docOps.find
  254. .calledWith({ doc_id: ObjectId(@doc_id) }, {
  255. docOps: $slice: [-(@version - @start), @limit]
  256. })
  257. .should.equal true
  258. it "should return the ops", ->
  259. @callback.calledWith(null, @doc.docOps).should.equal true
  260. describe "pushDocOp", ->
  261. beforeEach ->
  262. @op = "mock-op"
  263. @RedisManager.pushDocOp = sinon.stub().callsArgWith(2, null, @version = 42)
  264. @TrackChangesManager.pushUncompressedHistoryOp = sinon.stub().callsArg(2)
  265. @DocOpsManager.pushDocOp @project_id, @doc_id, @op, @callback
  266. it "should push the op in to the docOps list", ->
  267. @RedisManager.pushDocOp
  268. .calledWith(@doc_id, @op)
  269. .should.equal true
  270. it "should push the op into the pushUncompressedHistoryOp", ->
  271. @TrackChangesManager.pushUncompressedHistoryOp
  272. .calledWith(@doc_id, @op)
  273. .should.equal true
  274. it "should call the callback with the version", ->
  275. @callback.calledWith(null, @version).should.equal true