RedisManagerTests.coffee 24 KB

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