RedisManagerTests.coffee 23 KB

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