2014-02-25 11:27:14 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../../app/js/RedisManager.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe "RedisManager", ->
|
|
|
|
beforeEach ->
|
|
|
|
@RedisManager = SandboxedModule.require modulePath, requires:
|
2014-09-26 12:51:41 -04:00
|
|
|
"redis-sharelatex" :
|
2014-02-25 11:27:14 -05:00
|
|
|
createClient: () => @rclient =
|
|
|
|
auth: sinon.stub()
|
2014-03-21 09:17:58 -04:00
|
|
|
multi: () => @rclient
|
2014-09-26 12:51:41 -04:00
|
|
|
"settings-sharelatex":
|
|
|
|
redis:
|
2017-04-24 09:23:32 -04:00
|
|
|
history:
|
|
|
|
key_schema:
|
|
|
|
uncompressedHistoryOps: ({doc_id}) -> "UncompressedHistoryOps:#{doc_id}"
|
|
|
|
docsWithHistoryOps: ({project_id}) -> "DocsWithHistoryOps:#{project_id}"
|
2014-02-25 11:27:14 -05:00
|
|
|
@doc_id = "doc-id-123"
|
2014-03-21 09:17:58 -04:00
|
|
|
@project_id = "project-id-123"
|
2014-02-25 11:27:14 -05:00
|
|
|
@batchSize = 100
|
|
|
|
@callback = sinon.stub()
|
|
|
|
|
2015-10-08 09:40:42 -04:00
|
|
|
describe "getOldestDocUpdates", ->
|
2014-02-25 11:27:14 -05:00
|
|
|
beforeEach ->
|
|
|
|
@rawUpdates = [ {v: 42, op: "mock-op-42"}, { v: 45, op: "mock-op-45" }]
|
|
|
|
@jsonUpdates = (JSON.stringify(update) for update in @rawUpdates)
|
|
|
|
@rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonUpdates)
|
2015-10-08 09:40:42 -04:00
|
|
|
@RedisManager.getOldestDocUpdates @doc_id, @batchSize, @callback
|
2014-02-25 11:27:14 -05:00
|
|
|
|
|
|
|
it "should read the updates from redis", ->
|
|
|
|
@rclient.lrange
|
|
|
|
.calledWith("UncompressedHistoryOps:#{@doc_id}", 0, @batchSize - 1)
|
|
|
|
.should.equal true
|
|
|
|
|
2015-10-08 09:40:42 -04:00
|
|
|
it "should call the callback with the unparsed ops", ->
|
|
|
|
@callback.calledWith(null, @jsonUpdates).should.equal true
|
2014-02-25 11:27:14 -05:00
|
|
|
|
|
|
|
|
2015-10-08 09:40:42 -04:00
|
|
|
describe "expandDocUpdates", ->
|
|
|
|
beforeEach ->
|
|
|
|
@RedisManager.expandDocUpdates @jsonUpdates, @callback
|
|
|
|
|
|
|
|
it "should call the callback with the parsed ops", ->
|
|
|
|
@callback.calledWith(null, @rawUpdates).should.equal true
|
|
|
|
|
|
|
|
|
|
|
|
describe "deleteAppliedDocUpdates", ->
|
|
|
|
beforeEach ->
|
|
|
|
@rclient.lrem = sinon.stub()
|
|
|
|
@rclient.srem = sinon.stub()
|
|
|
|
@rclient.exec = sinon.stub().callsArgWith(0)
|
|
|
|
@RedisManager.deleteAppliedDocUpdates @project_id, @doc_id, @jsonUpdates, @callback
|
|
|
|
|
|
|
|
it "should delete the first update from redis", ->
|
|
|
|
@rclient.lrem
|
2019-10-09 06:11:15 -04:00
|
|
|
.calledWith("UncompressedHistoryOps:#{@doc_id}", 1, @jsonUpdates[0])
|
2015-10-08 09:40:42 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should delete the second update from redis", ->
|
|
|
|
@rclient.lrem
|
2019-10-09 06:11:15 -04:00
|
|
|
.calledWith("UncompressedHistoryOps:#{@doc_id}", 1, @jsonUpdates[1])
|
2015-10-08 09:40:42 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should delete the doc from the set of docs with history ops", ->
|
|
|
|
@rclient.srem
|
|
|
|
.calledWith("DocsWithHistoryOps:#{@project_id}", @doc_id)
|
|
|
|
.should.equal true
|
2014-03-21 09:17:58 -04:00
|
|
|
|
2015-10-08 09:40:42 -04:00
|
|
|
it "should call the callback ", ->
|
|
|
|
@callback.called.should.equal true
|
2014-03-21 09:48:14 -04:00
|
|
|
|
|
|
|
describe "getDocIdsWithHistoryOps", ->
|
|
|
|
beforeEach ->
|
|
|
|
@doc_ids = ["mock-id-1", "mock-id-2"]
|
|
|
|
@rclient.smembers = sinon.stub().callsArgWith(1, null, @doc_ids)
|
|
|
|
@RedisManager.getDocIdsWithHistoryOps @project_id, @callback
|
|
|
|
|
|
|
|
it "should read the doc_ids from redis", ->
|
|
|
|
@rclient.smembers
|
|
|
|
.calledWith("DocsWithHistoryOps:#{@project_id}")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with the doc_ids", ->
|
2015-10-08 09:40:42 -04:00
|
|
|
@callback.calledWith(null, @doc_ids).should.equal true
|