RedisManagerTests.coffee 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. crypto = require "crypto"
  8. tk = require "timekeeper"
  9. describe "RedisManager", ->
  10. beforeEach ->
  11. @multi = exec: sinon.stub()
  12. @rclient = multi: () => @multi
  13. tk.freeze(new Date())
  14. @RedisManager = SandboxedModule.require modulePath,
  15. requires:
  16. "logger-sharelatex": @logger = { error: sinon.stub(), log: sinon.stub(), warn: sinon.stub() }
  17. "./ProjectHistoryRedisManager": @ProjectHistoryRedisManager = {}
  18. "settings-sharelatex": @settings = {
  19. documentupdater: {logHashErrors: {write:true, read:true}}
  20. apis:
  21. project_history: {enabled: true}
  22. redis:
  23. documentupdater:
  24. key_schema:
  25. blockingKey: ({doc_id}) -> "Blocking:#{doc_id}"
  26. docLines: ({doc_id}) -> "doclines:#{doc_id}"
  27. docOps: ({doc_id}) -> "DocOps:#{doc_id}"
  28. docVersion: ({doc_id}) -> "DocVersion:#{doc_id}"
  29. docHash: ({doc_id}) -> "DocHash:#{doc_id}"
  30. projectKey: ({doc_id}) -> "ProjectId:#{doc_id}"
  31. pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
  32. docsInProject: ({project_id}) -> "DocsIn:#{project_id}"
  33. ranges: ({doc_id}) -> "Ranges:#{doc_id}"
  34. pathname: ({doc_id}) -> "Pathname:#{doc_id}"
  35. projectHistoryId: ({doc_id}) -> "ProjectHistoryId:#{doc_id}"
  36. projectState: ({project_id}) -> "ProjectState:#{project_id}"
  37. unflushedTime: ({doc_id}) -> "UnflushedTime:#{doc_id}"
  38. lastUpdatedBy: ({doc_id}) -> "lastUpdatedBy:#{doc_id}"
  39. lastUpdatedAt: ({doc_id}) -> "lastUpdatedAt:#{doc_id}"
  40. history:
  41. key_schema:
  42. uncompressedHistoryOps: ({doc_id}) -> "UncompressedHistoryOps:#{doc_id}"
  43. docsWithHistoryOps: ({project_id}) -> "DocsWithHistoryOps:#{project_id}"
  44. }
  45. "redis-sharelatex":
  46. createClient: () => @rclient
  47. "./Metrics": @metrics =
  48. inc: sinon.stub()
  49. Timer: class Timer
  50. constructor: () ->
  51. this.start = new Date()
  52. done: () ->
  53. timeSpan = new Date - this.start
  54. return timeSpan
  55. "./Errors": Errors
  56. globals:
  57. JSON: @JSON = JSON
  58. @doc_id = "doc-id-123"
  59. @project_id = "project-id-123"
  60. @projectHistoryId = 123
  61. @callback = sinon.stub()
  62. afterEach ->
  63. tk.reset()
  64. describe "getDoc", ->
  65. beforeEach ->
  66. @lines = ["one", "two", "three", "これは"] # include some utf8
  67. @jsonlines = JSON.stringify @lines
  68. @version = 42
  69. @hash = crypto.createHash('sha1').update(@jsonlines,'utf8').digest('hex')
  70. @ranges = { comments: "mock", entries: "mock" }
  71. @json_ranges = JSON.stringify @ranges
  72. @unflushed_time = 12345
  73. @pathname = '/a/b/c.tex'
  74. @multi.get = sinon.stub()
  75. @multi.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @hash, @project_id, @json_ranges, @pathname, @projectHistoryId.toString(), @unflushed_time])
  76. @rclient.sadd = sinon.stub().yields(null, 0)
  77. describe "successfully", ->
  78. beforeEach ->
  79. @RedisManager.getDoc @project_id, @doc_id, @callback
  80. it "should get the lines from redis", ->
  81. @multi.get
  82. .calledWith("doclines:#{@doc_id}")
  83. .should.equal true
  84. it "should get the version from", ->
  85. @multi.get
  86. .calledWith("DocVersion:#{@doc_id}")
  87. .should.equal true
  88. it 'should get the hash', ->
  89. @multi.get
  90. .calledWith("DocHash:#{@doc_id}")
  91. .should.equal true
  92. it "should get the ranges", ->
  93. @multi.get
  94. .calledWith("Ranges:#{@doc_id}")
  95. .should.equal true
  96. it "should get the unflushed time", ->
  97. @multi.get
  98. .calledWith("UnflushedTime:#{@doc_id}")
  99. .should.equal true
  100. it "should get the pathname", ->
  101. @multi.get
  102. .calledWith("Pathname:#{@doc_id}")
  103. .should.equal true
  104. it "should get the projectHistoryId as an integer", ->
  105. @multi.get
  106. .calledWith("ProjectHistoryId:#{@doc_id}")
  107. .should.equal true
  108. it "should get lastUpdatedAt", ->
  109. @multi.get
  110. .calledWith("lastUpdatedAt:#{@doc_id}")
  111. .should.equal true
  112. it "should get lastUpdatedBy", ->
  113. @multi.get
  114. .calledWith("lastUpdatedBy:#{@doc_id}")
  115. .should.equal true
  116. it "should check if the document is in the DocsIn set", ->
  117. @rclient.sadd
  118. .calledWith("DocsIn:#{@project_id}")
  119. .should.equal true
  120. it 'should return the document', ->
  121. @callback
  122. .calledWithExactly(null, @lines, @version, @ranges, @pathname, @projectHistoryId, @unflushed_time, @lastUpdatedAt, @lastUpdatedBy)
  123. .should.equal true
  124. it 'should not log any errors', ->
  125. @logger.error.calledWith()
  126. .should.equal false
  127. describe "when the document is not present", ->
  128. beforeEach ->
  129. @multi.exec = sinon.stub().callsArgWith(0, null, [null, null, null, null, null, null, null, null, null, null])
  130. @rclient.sadd = sinon.stub().yields()
  131. @RedisManager.getDoc @project_id, @doc_id, @callback
  132. it "should not check if the document is in the DocsIn set", ->
  133. @rclient.sadd
  134. .calledWith("DocsIn:#{@project_id}")
  135. .should.equal false
  136. it 'should return an empty result', ->
  137. @callback
  138. .calledWithExactly(null, null, 0, {}, null, null, null, null, null)
  139. .should.equal true
  140. it 'should not log any errors', ->
  141. @logger.error.calledWith()
  142. .should.equal false
  143. describe "when the document is missing from the DocsIn set", ->
  144. beforeEach ->
  145. @rclient.sadd = sinon.stub().yields(null, 1)
  146. @RedisManager.getDoc @project_id, @doc_id, @callback
  147. it 'should log an error', ->
  148. @logger.error.calledWith()
  149. .should.equal true
  150. it 'should return the document', ->
  151. @callback
  152. .calledWithExactly(null, @lines, @version, @ranges, @pathname, @projectHistoryId, @unflushed_time, @lastUpdatedAt, @lastUpdatedBy)
  153. .should.equal true
  154. describe "with a corrupted document", ->
  155. beforeEach ->
  156. @badHash = "INVALID-HASH-VALUE"
  157. @multi.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @badHash, @project_id, @json_ranges])
  158. @RedisManager.getDoc @project_id, @doc_id, @callback
  159. it 'should log a hash error', ->
  160. @logger.error.calledWith()
  161. .should.equal true
  162. it 'should return the document', ->
  163. @callback
  164. .calledWith(null, @lines, @version, @ranges)
  165. .should.equal true
  166. describe "with a slow request to redis", ->
  167. beforeEach ->
  168. @multi.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @badHash, @project_id, @json_ranges, @pathname, @unflushed_time])
  169. @clock = sinon.useFakeTimers();
  170. @multi.exec = (cb) =>
  171. @clock.tick(6000);
  172. cb(null, [@jsonlines, @version, @another_project_id, @json_ranges, @pathname, @unflushed_time])
  173. @RedisManager.getDoc @project_id, @doc_id, @callback
  174. afterEach ->
  175. @clock.restore()
  176. it 'should return an error', ->
  177. @callback
  178. .calledWith(new Error("redis getDoc exceeded timeout"))
  179. .should.equal true
  180. describe "getDoc with an invalid project id", ->
  181. beforeEach ->
  182. @another_project_id = "project-id-456"
  183. @multi.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @hash, @another_project_id, @json_ranges, @pathname, @unflushed_time])
  184. @RedisManager.getDoc @project_id, @doc_id, @callback
  185. it 'should return an error', ->
  186. @callback
  187. .calledWith(new Errors.NotFoundError("not found"))
  188. .should.equal true
  189. describe "getPreviousDocOpsTests", ->
  190. describe "with a start and an end value", ->
  191. beforeEach ->
  192. @first_version_in_redis = 30
  193. @version = 70
  194. @length = @version - @first_version_in_redis
  195. @start = 50
  196. @end = 60
  197. @ops = [
  198. { "mock": "op-1" },
  199. { "mock": "op-2" }
  200. ]
  201. @jsonOps = @ops.map (op) -> JSON.stringify op
  202. @rclient.llen = sinon.stub().callsArgWith(1, null, @length)
  203. @rclient.get = sinon.stub().callsArgWith(1, null, @version.toString())
  204. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonOps)
  205. @RedisManager.getPreviousDocOps(@doc_id, @start, @end, @callback)
  206. it "should get the length of the existing doc ops", ->
  207. @rclient.llen
  208. .calledWith("DocOps:#{@doc_id}")
  209. .should.equal true
  210. it "should get the current version of the doc", ->
  211. @rclient.get
  212. .calledWith("DocVersion:#{@doc_id}")
  213. .should.equal true
  214. it "should get the appropriate docs ops", ->
  215. @rclient.lrange
  216. .calledWith("DocOps:#{@doc_id}", @start - @first_version_in_redis, @end - @first_version_in_redis)
  217. .should.equal true
  218. it "should return the docs with the doc ops deserialized", ->
  219. @callback.calledWith(null, @ops).should.equal true
  220. describe "with an end value of -1", ->
  221. beforeEach ->
  222. @first_version_in_redis = 30
  223. @version = 70
  224. @length = @version - @first_version_in_redis
  225. @start = 50
  226. @end = -1
  227. @ops = [
  228. { "mock": "op-1" },
  229. { "mock": "op-2" }
  230. ]
  231. @jsonOps = @ops.map (op) -> JSON.stringify op
  232. @rclient.llen = sinon.stub().callsArgWith(1, null, @length)
  233. @rclient.get = sinon.stub().callsArgWith(1, null, @version.toString())
  234. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonOps)
  235. @RedisManager.getPreviousDocOps(@doc_id, @start, @end, @callback)
  236. it "should get the appropriate docs ops to the end of list", ->
  237. @rclient.lrange
  238. .calledWith("DocOps:#{@doc_id}", @start - @first_version_in_redis, -1)
  239. .should.equal true
  240. it "should return the docs with the doc ops deserialized", ->
  241. @callback.calledWith(null, @ops).should.equal true
  242. describe "when the requested range is not in Redis", ->
  243. beforeEach ->
  244. @first_version_in_redis = 30
  245. @version = 70
  246. @length = @version - @first_version_in_redis
  247. @start = 20
  248. @end = -1
  249. @ops = [
  250. { "mock": "op-1" },
  251. { "mock": "op-2" }
  252. ]
  253. @jsonOps = @ops.map (op) -> JSON.stringify op
  254. @rclient.llen = sinon.stub().callsArgWith(1, null, @length)
  255. @rclient.get = sinon.stub().callsArgWith(1, null, @version.toString())
  256. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonOps)
  257. @RedisManager.getPreviousDocOps(@doc_id, @start, @end, @callback)
  258. it "should return an error", ->
  259. @callback.calledWith(new Errors.OpRangeNotAvailableError("doc ops range is not loaded in redis")).should.equal true
  260. it "should log out the problem", ->
  261. @logger.warn.called.should.equal true
  262. describe "with a slow request to redis", ->
  263. beforeEach ->
  264. @first_version_in_redis = 30
  265. @version = 70
  266. @length = @version - @first_version_in_redis
  267. @start = 50
  268. @end = 60
  269. @ops = [
  270. { "mock": "op-1" },
  271. { "mock": "op-2" }
  272. ]
  273. @jsonOps = @ops.map (op) -> JSON.stringify op
  274. @rclient.llen = sinon.stub().callsArgWith(1, null, @length)
  275. @rclient.get = sinon.stub().callsArgWith(1, null, @version.toString())
  276. @clock = sinon.useFakeTimers();
  277. @rclient.lrange = (key, start, end, cb) =>
  278. @clock.tick(6000);
  279. cb(null, @jsonOps)
  280. @RedisManager.getPreviousDocOps(@doc_id, @start, @end, @callback)
  281. afterEach ->
  282. @clock.restore()
  283. it 'should return an error', ->
  284. @callback
  285. .calledWith(new Error("redis getPreviousDocOps exceeded timeout"))
  286. .should.equal true
  287. describe "updateDocument", ->
  288. beforeEach ->
  289. @lines = ["one", "two", "three", "これは"]
  290. @ops = [{ op: [{ i: "foo", p: 4 }] },{ op: [{ i: "bar", p: 8 }] }]
  291. @version = 42
  292. @hash = crypto.createHash('sha1').update(JSON.stringify(@lines),'utf8').digest('hex')
  293. @ranges = { comments: "mock", entries: "mock" }
  294. @updateMeta = { user_id: 'last-author-fake-id' }
  295. @doc_update_list_length = sinon.stub()
  296. @project_update_list_length = sinon.stub()
  297. @RedisManager.getDocVersion = sinon.stub()
  298. @multi.set = sinon.stub()
  299. @multi.rpush = sinon.stub()
  300. @multi.expire = sinon.stub()
  301. @multi.ltrim = sinon.stub()
  302. @multi.del = sinon.stub()
  303. @multi.eval = sinon.stub()
  304. @multi.exec = sinon.stub().callsArgWith(0, null,
  305. [@hash, null, null, null, null, null, null, @doc_update_list_length, null, null]
  306. )
  307. @ProjectHistoryRedisManager.queueOps = sinon.stub().callsArgWith(
  308. @ops.length + 1, null, @project_update_list_length
  309. )
  310. describe "with a consistent version", ->
  311. beforeEach ->
  312. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length)
  313. describe "with project history enabled", ->
  314. beforeEach ->
  315. @settings.apis.project_history.enabled = true
  316. @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, @ranges, @updateMeta, @callback
  317. it "should get the current doc version to check for consistency", ->
  318. @RedisManager.getDocVersion
  319. .calledWith(@doc_id)
  320. .should.equal true
  321. it "should set the doclines", ->
  322. @multi.eval
  323. .calledWith(sinon.match(/redis.call/), 1, "doclines:#{@doc_id}", JSON.stringify(@lines))
  324. .should.equal true
  325. it "should set the version", ->
  326. @multi.set
  327. .calledWith("DocVersion:#{@doc_id}", @version)
  328. .should.equal true
  329. it "should set the hash", ->
  330. @multi.set
  331. .calledWith("DocHash:#{@doc_id}", @hash)
  332. .should.equal true
  333. it "should set the ranges", ->
  334. @multi.set
  335. .calledWith("Ranges:#{@doc_id}", JSON.stringify(@ranges))
  336. .should.equal true
  337. it "should set the unflushed time", ->
  338. @multi.set
  339. .calledWith("UnflushedTime:#{@doc_id}", Date.now(), "NX")
  340. .should.equal true
  341. it "should set the last updated time", ->
  342. @multi.set
  343. .calledWith("lastUpdatedAt:#{@doc_id}", Date.now())
  344. .should.equal true
  345. it "should set the last updater", ->
  346. @multi.set
  347. .calledWith("lastUpdatedBy:#{@doc_id}", 'last-author-fake-id')
  348. .should.equal true
  349. it "should push the doc op into the doc ops list", ->
  350. @multi.rpush
  351. .calledWith("DocOps:#{@doc_id}", JSON.stringify(@ops[0]), JSON.stringify(@ops[1]))
  352. .should.equal true
  353. it "should renew the expiry ttl on the doc ops array", ->
  354. @multi.expire
  355. .calledWith("DocOps:#{@doc_id}", @RedisManager.DOC_OPS_TTL)
  356. .should.equal true
  357. it "should truncate the list to 100 members", ->
  358. @multi.ltrim
  359. .calledWith("DocOps:#{@doc_id}", -@RedisManager.DOC_OPS_MAX_LENGTH, -1)
  360. .should.equal true
  361. it "should push the updates into the history ops list", ->
  362. @multi.rpush
  363. .calledWith("UncompressedHistoryOps:#{@doc_id}", JSON.stringify(@ops[0]), JSON.stringify(@ops[1]))
  364. .should.equal true
  365. it "should push the updates into the project history ops list", ->
  366. @ProjectHistoryRedisManager.queueOps
  367. .calledWith(@project_id, JSON.stringify(@ops[0]))
  368. .should.equal true
  369. it "should call the callback", ->
  370. @callback
  371. .calledWith(null, @doc_update_list_length, @project_update_list_length)
  372. .should.equal true
  373. it 'should not log any errors', ->
  374. @logger.error.calledWith()
  375. .should.equal false
  376. describe "with project history disabled", ->
  377. beforeEach ->
  378. @rclient.rpush = sinon.stub()
  379. @settings.apis.project_history.enabled = false
  380. @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, @ranges, @updateMeta, @callback
  381. it "should not push the updates into the project history ops list", ->
  382. @rclient.rpush.called.should.equal false
  383. it "should call the callback", ->
  384. @callback
  385. .calledWith(null, @doc_update_list_length)
  386. .should.equal true
  387. describe "with an inconsistent version", ->
  388. beforeEach ->
  389. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length - 1)
  390. @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, @ranges, @updateMeta, @callback
  391. it "should not call multi.exec", ->
  392. @multi.exec.called.should.equal false
  393. it "should call the callback with an error", ->
  394. @callback
  395. .calledWith(new Error("Version mismatch. '#{@doc_id}' is corrupted."))
  396. .should.equal true
  397. describe "with no updates", ->
  398. beforeEach ->
  399. @rclient.rpush = sinon.stub().callsArgWith(1, null, @project_update_list_length)
  400. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version)
  401. @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, [], @ranges, @updateMeta, @callback
  402. it "should not try to enqueue doc updates", ->
  403. @multi.rpush
  404. .called
  405. .should.equal false
  406. it "should not try to enqueue project updates", ->
  407. @rclient.rpush
  408. .called
  409. .should.equal false
  410. it "should still set the doclines", ->
  411. @multi.eval
  412. .calledWith(sinon.match(/redis.call/), 1, "doclines:#{@doc_id}", JSON.stringify(@lines))
  413. .should.equal true
  414. describe "with empty ranges", ->
  415. beforeEach ->
  416. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length)
  417. @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, {}, @updateMeta, @callback
  418. it "should not set the ranges", ->
  419. @multi.set
  420. .calledWith("Ranges:#{@doc_id}", JSON.stringify(@ranges))
  421. .should.equal false
  422. it "should delete the ranges key", ->
  423. @multi.del
  424. .calledWith("Ranges:#{@doc_id}")
  425. .should.equal true
  426. describe "with a corrupted write", ->
  427. beforeEach ->
  428. @badHash = "INVALID-HASH-VALUE"
  429. @multi.exec = sinon.stub().callsArgWith(0, null, [@badHash])
  430. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length)
  431. @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, @ranges, @updateMeta, @callback
  432. it 'should log a hash error', ->
  433. @logger.error.calledWith()
  434. .should.equal true
  435. it "should call the callback", ->
  436. @callback.called.should.equal true
  437. describe "with null bytes in the serialized doc lines", ->
  438. beforeEach ->
  439. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length)
  440. @_stringify = JSON.stringify
  441. @JSON.stringify = () -> return '["bad bytes! \u0000 <- here"]'
  442. @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, @ranges, @updateMeta, @callback
  443. afterEach ->
  444. @JSON.stringify = @_stringify
  445. it "should log an error", ->
  446. @logger.error.called.should.equal true
  447. it "should call the callback with an error", ->
  448. @callback.calledWith(new Error("null bytes found in doc lines")).should.equal true
  449. describe "with ranges that are too big", ->
  450. beforeEach ->
  451. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length)
  452. @RedisManager._serializeRanges = sinon.stub().yields(new Error("ranges are too large"))
  453. @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, @ranges, @updateMeta, @callback
  454. it 'should log an error', ->
  455. @logger.error.called.should.equal true
  456. it "should call the callback with the error", ->
  457. @callback.calledWith(new Error("ranges are too large")).should.equal true
  458. describe "without user id from meta", ->
  459. beforeEach ->
  460. @RedisManager.getDocVersion.withArgs(@doc_id).yields(null, @version - @ops.length)
  461. @RedisManager.updateDocument @project_id, @doc_id, @lines, @version, @ops, @ranges, {}, @callback
  462. it "should set the last updater to null", ->
  463. @multi.del
  464. .calledWith("lastUpdatedBy:#{@doc_id}")
  465. .should.equal true
  466. it "should still set the last updated time", ->
  467. @multi.set
  468. .calledWith("lastUpdatedAt:#{@doc_id}", Date.now())
  469. .should.equal true
  470. describe "putDocInMemory", ->
  471. beforeEach ->
  472. @multi.set = sinon.stub()
  473. @rclient.sadd = sinon.stub().yields()
  474. @multi.del = sinon.stub()
  475. @multi.eval = sinon.stub()
  476. @lines = ["one", "two", "three", "これは"]
  477. @version = 42
  478. @hash = crypto.createHash('sha1').update(JSON.stringify(@lines),'utf8').digest('hex')
  479. @multi.exec = sinon.stub().callsArgWith(0, null, [@hash])
  480. @ranges = { comments: "mock", entries: "mock" }
  481. @pathname = '/a/b/c.tex'
  482. describe "with non-empty ranges", ->
  483. beforeEach (done) ->
  484. @RedisManager.putDocInMemory @project_id, @doc_id, @lines, @version, @ranges, @pathname, @projectHistoryId, done
  485. it "should set the lines", ->
  486. @multi.eval
  487. .calledWith(sinon.match(/redis.call/), 1, "doclines:#{@doc_id}", JSON.stringify(@lines))
  488. .should.equal true
  489. it "should set the version", ->
  490. @multi.set
  491. .calledWith("DocVersion:#{@doc_id}", @version)
  492. .should.equal true
  493. it "should set the hash", ->
  494. @multi.set
  495. .calledWith("DocHash:#{@doc_id}", @hash)
  496. .should.equal true
  497. it "should set the ranges", ->
  498. @multi.set
  499. .calledWith("Ranges:#{@doc_id}", JSON.stringify(@ranges))
  500. .should.equal true
  501. it "should set the project_id for the doc", ->
  502. @multi.set
  503. .calledWith("ProjectId:#{@doc_id}", @project_id)
  504. .should.equal true
  505. it "should set the pathname for the doc", ->
  506. @multi.set
  507. .calledWith("Pathname:#{@doc_id}", @pathname)
  508. .should.equal true
  509. it "should set the projectHistoryId for the doc", ->
  510. @multi.set
  511. .calledWith("ProjectHistoryId:#{@doc_id}", @projectHistoryId)
  512. .should.equal true
  513. it "should add the doc_id to the project set", ->
  514. @rclient.sadd
  515. .calledWith("DocsIn:#{@project_id}", @doc_id)
  516. .should.equal true
  517. it 'should not log any errors', ->
  518. @logger.error.calledWith()
  519. .should.equal false
  520. describe "with empty ranges", ->
  521. beforeEach (done) ->
  522. @RedisManager.putDocInMemory @project_id, @doc_id, @lines, @version, {}, @pathname, @projectHistoryId, done
  523. it "should delete the ranges key", ->
  524. @multi.del
  525. .calledWith("Ranges:#{@doc_id}")
  526. .should.equal true
  527. it "should not set the ranges", ->
  528. @multi.set
  529. .calledWith("Ranges:#{@doc_id}", JSON.stringify(@ranges))
  530. .should.equal false
  531. describe "with a corrupted write", ->
  532. beforeEach (done) ->
  533. @multi.exec = sinon.stub().callsArgWith(0, null, ["INVALID-HASH-VALUE"])
  534. @RedisManager.putDocInMemory @project_id, @doc_id, @lines, @version, @ranges, @pathname, @projectHistoryId, done
  535. it 'should log a hash error', ->
  536. @logger.error.calledWith()
  537. .should.equal true
  538. describe "with null bytes in the serialized doc lines", ->
  539. beforeEach ->
  540. @_stringify = JSON.stringify
  541. @JSON.stringify = () -> return '["bad bytes! \u0000 <- here"]'
  542. @RedisManager.putDocInMemory @project_id, @doc_id, @lines, @version, @ranges, @pathname, @projectHistoryId, @callback
  543. afterEach ->
  544. @JSON.stringify = @_stringify
  545. it "should log an error", ->
  546. @logger.error.called.should.equal true
  547. it "should call the callback with an error", ->
  548. @callback.calledWith(new Error("null bytes found in doc lines")).should.equal true
  549. describe "with ranges that are too big", ->
  550. beforeEach ->
  551. @RedisManager._serializeRanges = sinon.stub().yields(new Error("ranges are too large"))
  552. @RedisManager.putDocInMemory @project_id, @doc_id, @lines, @version, @ranges, @pathname, @projectHistoryId, @callback
  553. it 'should log an error', ->
  554. @logger.error.called.should.equal true
  555. it "should call the callback with the error", ->
  556. @callback.calledWith(new Error("ranges are too large")).should.equal true
  557. describe "removeDocFromMemory", ->
  558. beforeEach (done) ->
  559. @multi.del = sinon.stub()
  560. @multi.srem = sinon.stub()
  561. @multi.exec.yields()
  562. @RedisManager.removeDocFromMemory @project_id, @doc_id, done
  563. it "should delete the lines", ->
  564. @multi.del
  565. .calledWith("doclines:#{@doc_id}")
  566. .should.equal true
  567. it "should delete the version", ->
  568. @multi.del
  569. .calledWith("DocVersion:#{@doc_id}")
  570. .should.equal true
  571. it "should delete the hash", ->
  572. @multi.del
  573. .calledWith("DocHash:#{@doc_id}")
  574. .should.equal true
  575. it "should delete the unflushed time", ->
  576. @multi.del
  577. .calledWith("UnflushedTime:#{@doc_id}")
  578. .should.equal true
  579. it "should delete the project_id for the doc", ->
  580. @multi.del
  581. .calledWith("ProjectId:#{@doc_id}")
  582. .should.equal true
  583. it "should remove the doc_id from the project set", ->
  584. @multi.srem
  585. .calledWith("DocsIn:#{@project_id}", @doc_id)
  586. .should.equal true
  587. it "should delete the pathname for the doc", ->
  588. @multi.del
  589. .calledWith("Pathname:#{@doc_id}")
  590. .should.equal true
  591. it "should delete the pathname for the doc", ->
  592. @multi.del
  593. .calledWith("ProjectHistoryId:#{@doc_id}")
  594. .should.equal true
  595. it "should delete lastUpdatedAt", ->
  596. @multi.del
  597. .calledWith("lastUpdatedAt:#{@doc_id}")
  598. .should.equal true
  599. it "should delete lastUpdatedBy", ->
  600. @multi.del
  601. .calledWith("lastUpdatedBy:#{@doc_id}")
  602. .should.equal true
  603. describe "clearProjectState", ->
  604. beforeEach (done) ->
  605. @rclient.del = sinon.stub().callsArg(1)
  606. @RedisManager.clearProjectState @project_id, done
  607. it "should delete the project state", ->
  608. @rclient.del
  609. .calledWith("ProjectState:#{@project_id}")
  610. .should.equal true
  611. describe "renameDoc", ->
  612. beforeEach () ->
  613. @rclient.rpush = sinon.stub().yields()
  614. @rclient.set = sinon.stub().yields()
  615. @update =
  616. id: @doc_id
  617. pathname: @pathname = 'pathname'
  618. newPathname: @newPathname = 'new-pathname'
  619. describe "the document is cached in redis", ->
  620. beforeEach ->
  621. @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, 'lines', 'version')
  622. @ProjectHistoryRedisManager.queueRenameEntity = sinon.stub().yields()
  623. @RedisManager.renameDoc @project_id, @doc_id, @userId, @update, @projectHistoryId, @callback
  624. it "update the cached pathname", ->
  625. @rclient.set
  626. .calledWith("Pathname:#{@doc_id}", @newPathname)
  627. .should.equal true
  628. it "should queue an update", ->
  629. @ProjectHistoryRedisManager.queueRenameEntity
  630. .calledWithExactly(@project_id, @projectHistoryId, 'doc', @doc_id, @userId, @update, @callback)
  631. .should.equal true
  632. describe "the document is not cached in redis", ->
  633. beforeEach ->
  634. @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, null, null)
  635. @ProjectHistoryRedisManager.queueRenameEntity = sinon.stub().yields()
  636. @RedisManager.renameDoc @project_id, @doc_id, @userId, @update, @projectHistoryId, @callback
  637. it "does not update the cached pathname", ->
  638. @rclient.set.called.should.equal false
  639. it "should queue an update", ->
  640. @ProjectHistoryRedisManager.queueRenameEntity
  641. .calledWithExactly(@project_id, @projectHistoryId, 'doc', @doc_id, @userId, @update, @callback)
  642. .should.equal true