RedisManagerTests.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/RedisManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors"
  7. describe "RedisManager", ->
  8. beforeEach ->
  9. @rclient =
  10. auth: () ->
  11. exec: sinon.stub()
  12. @rclient.multi = () => @rclient
  13. @RedisManager = SandboxedModule.require modulePath, requires:
  14. "./RedisBackend":
  15. createClient: () => @rclient
  16. "./RedisKeyBuilder":
  17. blockingKey: ({doc_id}) -> "Blocking:#{doc_id}"
  18. docLines: ({doc_id}) -> "doclines:#{doc_id}"
  19. docOps: ({doc_id}) -> "DocOps:#{doc_id}"
  20. docVersion: ({doc_id}) -> "DocVersion:#{doc_id}"
  21. projectKey: ({doc_id}) -> "ProjectId:#{doc_id}"
  22. pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
  23. docsInProject: ({project_id}) -> "DocsIn:#{project_id}"
  24. trackChangesEntries: ({doc_id}) -> "TrackChangesEntries:#{doc_id}"
  25. "logger-sharelatex": @logger = { error: sinon.stub(), log: sinon.stub(), warn: sinon.stub() }
  26. "./Metrics": @metrics =
  27. inc: sinon.stub()
  28. Timer: class Timer
  29. done: () ->
  30. "./Errors": Errors
  31. @doc_id = "doc-id-123"
  32. @project_id = "project-id-123"
  33. @callback = sinon.stub()
  34. describe "getDoc", ->
  35. beforeEach ->
  36. @lines = ["one", "two", "three"]
  37. @jsonlines = JSON.stringify @lines
  38. @version = 42
  39. @track_changes_on = true
  40. @track_changes_entries = { comments: "mock", entries: "mock" }
  41. @json_track_changes_entries = JSON.stringify @track_changes_entries
  42. @rclient.get = sinon.stub()
  43. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @project_id, @json_track_changes_entries])
  44. describe "successfully", ->
  45. beforeEach ->
  46. @RedisManager.getDoc @project_id, @doc_id, @callback
  47. it "should get the lines from redis", ->
  48. @rclient.get
  49. .calledWith("doclines:#{@doc_id}")
  50. .should.equal true
  51. it "should get the version from", ->
  52. @rclient.get
  53. .calledWith("DocVersion:#{@doc_id}")
  54. .should.equal true
  55. it "should get the track changes entries", ->
  56. @rclient.get
  57. .calledWith("TrackChangesEntries:#{@doc_id}")
  58. .should.equal true
  59. it 'should return the document', ->
  60. @callback
  61. .calledWith(null, @lines, @version, @track_changes_entries)
  62. .should.equal true
  63. describe "getDoc with an invalid project id", ->
  64. beforeEach ->
  65. @another_project_id = "project-id-456"
  66. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @another_project_id, @json_track_changes_entries])
  67. @RedisManager.getDoc @project_id, @doc_id, @callback
  68. it 'should return an error', ->
  69. @callback
  70. .calledWith(new Errors.NotFoundError("not found"))
  71. .should.equal true
  72. describe "getPreviousDocOpsTests", ->
  73. describe "with a start and an end value", ->
  74. beforeEach ->
  75. @first_version_in_redis = 30
  76. @version = 70
  77. @length = @version - @first_version_in_redis
  78. @start = 50
  79. @end = 60
  80. @ops = [
  81. { "mock": "op-1" },
  82. { "mock": "op-2" }
  83. ]
  84. @jsonOps = @ops.map (op) -> JSON.stringify op
  85. @rclient.llen = sinon.stub().callsArgWith(1, null, @length)
  86. @rclient.get = sinon.stub().callsArgWith(1, null, @version.toString())
  87. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonOps)
  88. @RedisManager.getPreviousDocOps(@doc_id, @start, @end, @callback)
  89. it "should get the length of the existing doc ops", ->
  90. @rclient.llen
  91. .calledWith("DocOps:#{@doc_id}")
  92. .should.equal true
  93. it "should get the current version of the doc", ->
  94. @rclient.get
  95. .calledWith("DocVersion:#{@doc_id}")
  96. .should.equal true
  97. it "should get the appropriate docs ops", ->
  98. @rclient.lrange
  99. .calledWith("DocOps:#{@doc_id}", @start - @first_version_in_redis, @end - @first_version_in_redis)
  100. .should.equal true
  101. it "should return the docs with the doc ops deserialized", ->
  102. @callback.calledWith(null, @ops).should.equal true
  103. describe "with an end value of -1", ->
  104. beforeEach ->
  105. @first_version_in_redis = 30
  106. @version = 70
  107. @length = @version - @first_version_in_redis
  108. @start = 50
  109. @end = -1
  110. @ops = [
  111. { "mock": "op-1" },
  112. { "mock": "op-2" }
  113. ]
  114. @jsonOps = @ops.map (op) -> JSON.stringify op
  115. @rclient.llen = sinon.stub().callsArgWith(1, null, @length)
  116. @rclient.get = sinon.stub().callsArgWith(1, null, @version.toString())
  117. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonOps)
  118. @RedisManager.getPreviousDocOps(@doc_id, @start, @end, @callback)
  119. it "should get the appropriate docs ops to the end of list", ->
  120. @rclient.lrange
  121. .calledWith("DocOps:#{@doc_id}", @start - @first_version_in_redis, -1)
  122. .should.equal true
  123. it "should return the docs with the doc ops deserialized", ->
  124. @callback.calledWith(null, @ops).should.equal true
  125. describe "when the requested range is not in Redis", ->
  126. beforeEach ->
  127. @first_version_in_redis = 30
  128. @version = 70
  129. @length = @version - @first_version_in_redis
  130. @start = 20
  131. @end = -1
  132. @ops = [
  133. { "mock": "op-1" },
  134. { "mock": "op-2" }
  135. ]
  136. @jsonOps = @ops.map (op) -> JSON.stringify op
  137. @rclient.llen = sinon.stub().callsArgWith(1, null, @length)
  138. @rclient.get = sinon.stub().callsArgWith(1, null, @version.toString())
  139. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonOps)
  140. @RedisManager.getPreviousDocOps(@doc_id, @start, @end, @callback)
  141. it "should return an error", ->
  142. @callback.calledWith(new Errors.OpRangeNotAvailableError("doc ops range is not loaded in redis")).should.equal true
  143. it "should log out the problem", ->
  144. @logger.warn.called.should.equal true
  145. describe "updateDocument", ->
  146. beforeEach ->
  147. @rclient.set = sinon.stub()
  148. @rclient.rpush = sinon.stub()
  149. @rclient.expire = sinon.stub()
  150. @rclient.ltrim = sinon.stub()
  151. @RedisManager.getDocVersion = sinon.stub()
  152. @lines = ["one", "two", "three"]
  153. @ops = [{ op: [{ i: "foo", p: 4 }] },{ op: [{ i: "bar", p: 8 }] }]
  154. @version = 42
  155. @track_changes_entries = { comments: "mock", entries: "mock" }
  156. @rclient.exec = sinon.stub().callsArg(0)
  157. describe "with a consistent version", ->
  158. beforeEach ->
  159. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length)
  160. @RedisManager.updateDocument @doc_id, @lines, @version, @ops, @track_changes_entries, @callback
  161. it "should get the current doc version to check for consistency", ->
  162. @RedisManager.getDocVersion
  163. .calledWith(@doc_id)
  164. .should.equal true
  165. it "should set the doclines", ->
  166. @rclient.set
  167. .calledWith("doclines:#{@doc_id}", JSON.stringify(@lines))
  168. .should.equal true
  169. it "should set the version", ->
  170. @rclient.set
  171. .calledWith("DocVersion:#{@doc_id}", @version)
  172. .should.equal true
  173. it "should set the track changes entries", ->
  174. @rclient.set
  175. .calledWith("TrackChangesEntries:#{@doc_id}", JSON.stringify(@track_changes_entries))
  176. .should.equal true
  177. it "should push the doc op into the doc ops list", ->
  178. @rclient.rpush
  179. .calledWith("DocOps:#{@doc_id}", JSON.stringify(@ops[0]), JSON.stringify(@ops[1]))
  180. .should.equal true
  181. it "should renew the expiry ttl on the doc ops array", ->
  182. @rclient.expire
  183. .calledWith("DocOps:#{@doc_id}", @RedisManager.DOC_OPS_TTL)
  184. .should.equal true
  185. it "should truncate the list to 100 members", ->
  186. @rclient.ltrim
  187. .calledWith("DocOps:#{@doc_id}", -@RedisManager.DOC_OPS_MAX_LENGTH, -1)
  188. .should.equal true
  189. it "should call the callback", ->
  190. @callback.called.should.equal true
  191. describe "with an inconsistent version", ->
  192. beforeEach ->
  193. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length - 1)
  194. @RedisManager.updateDocument @doc_id, @lines, @version, @ops, @track_changes_entries, @callback
  195. it "should not call multi.exec", ->
  196. @rclient.exec.called.should.equal false
  197. it "should call the callback with an error", ->
  198. @callback
  199. .calledWith(new Error("Version mismatch. '#{@doc_id}' is corrupted."))
  200. .should.equal true
  201. describe "with no updates", ->
  202. beforeEach ->
  203. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version)
  204. @RedisManager.updateDocument @doc_id, @lines, @version, [], @track_changes_entries, @callback
  205. it "should not do an rpush", ->
  206. @rclient.rpush
  207. .called
  208. .should.equal false
  209. it "should still set the doclines", ->
  210. @rclient.set
  211. .calledWith("doclines:#{@doc_id}", JSON.stringify(@lines))
  212. .should.equal true
  213. describe "putDocInMemory", ->
  214. beforeEach (done) ->
  215. @rclient.set = sinon.stub()
  216. @rclient.sadd = sinon.stub().yields()
  217. @rclient.exec.yields()
  218. @lines = ["one", "two", "three"]
  219. @version = 42
  220. @track_changes_entries = { comments: "mock", entries: "mock" }
  221. @RedisManager.putDocInMemory @project_id, @doc_id, @lines, @version, @track_changes_entries, done
  222. it "should set the lines", ->
  223. @rclient.set
  224. .calledWith("doclines:#{@doc_id}", JSON.stringify @lines)
  225. .should.equal true
  226. it "should set the version", ->
  227. @rclient.set
  228. .calledWith("DocVersion:#{@doc_id}", @version)
  229. .should.equal true
  230. it "should set the track changes entries", ->
  231. @rclient.set
  232. .calledWith("TrackChangesEntries:#{@doc_id}", JSON.stringify(@track_changes_entries))
  233. .should.equal true
  234. it "should set the project_id for the doc", ->
  235. @rclient.set
  236. .calledWith("ProjectId:#{@doc_id}", @project_id)
  237. .should.equal true
  238. it "should add the doc_id to the project set", ->
  239. @rclient.sadd
  240. .calledWith("DocsIn:#{@project_id}", @doc_id)
  241. .should.equal true
  242. describe "removeDocFromMemory", ->
  243. beforeEach (done) ->
  244. @rclient.del = sinon.stub()
  245. @rclient.srem = sinon.stub().yields()
  246. @rclient.exec.yields()
  247. @RedisManager.removeDocFromMemory @project_id, @doc_id, done
  248. it "should delete the lines", ->
  249. @rclient.del
  250. .calledWith("doclines:#{@doc_id}")
  251. .should.equal true
  252. it "should delete the version", ->
  253. @rclient.del
  254. .calledWith("DocVersion:#{@doc_id}")
  255. .should.equal true
  256. it "should delete the project_id for the doc", ->
  257. @rclient.del
  258. .calledWith("ProjectId:#{@doc_id}")
  259. .should.equal true
  260. it "should remove the doc_id from the project set", ->
  261. @rclient.srem
  262. .calledWith("DocsIn:#{@project_id}", @doc_id)
  263. .should.equal true