2016-06-17 07:17:22 -04:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
2017-05-02 10:38:33 -04:00
|
|
|
modulePath = "../../../../app/js/RealTimeRedisManager.js"
|
2016-06-17 07:17:22 -04:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
Errors = require "../../../../app/js/Errors"
|
|
|
|
|
2017-05-02 10:38:33 -04:00
|
|
|
describe "RealTimeRedisManager", ->
|
2016-06-17 07:17:22 -04:00
|
|
|
beforeEach ->
|
|
|
|
@rclient =
|
|
|
|
auth: () ->
|
|
|
|
exec: sinon.stub()
|
|
|
|
@rclient.multi = () => @rclient
|
2017-05-02 10:38:33 -04:00
|
|
|
@RealTimeRedisManager = SandboxedModule.require modulePath, requires:
|
2016-06-17 07:17:22 -04:00
|
|
|
"redis-sharelatex": createClient: () => @rclient
|
2017-04-13 12:00:42 -04:00
|
|
|
"settings-sharelatex":
|
|
|
|
redis:
|
2017-05-02 10:38:33 -04:00
|
|
|
realtime: @settings =
|
2017-04-13 12:00:42 -04:00
|
|
|
key_schema:
|
|
|
|
pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
|
2017-04-12 09:53:03 -04:00
|
|
|
"logger-sharelatex": { log: () -> }
|
2019-03-21 08:10:15 -04:00
|
|
|
"crypto": @crypto = { randomBytes: sinon.stub().withArgs(4).returns(Buffer.from([0x1, 0x2, 0x3, 0x4])) }
|
|
|
|
"os": @os = {hostname: sinon.stub().returns("somehost")}
|
|
|
|
|
2016-06-17 07:17:22 -04:00
|
|
|
@doc_id = "doc-id-123"
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
|
|
describe "getPendingUpdatesForDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@rclient.lrange = sinon.stub()
|
2017-05-12 08:11:04 -04:00
|
|
|
@rclient.ltrim = sinon.stub()
|
2016-06-17 07:17:22 -04:00
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@updates = [
|
|
|
|
{ op: [{ i: "foo", p: 4 }] }
|
|
|
|
{ op: [{ i: "foo", p: 4 }] }
|
|
|
|
]
|
|
|
|
@jsonUpdates = @updates.map (update) -> JSON.stringify update
|
|
|
|
@rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
|
2017-05-02 10:38:33 -04:00
|
|
|
@RealTimeRedisManager.getPendingUpdatesForDoc @doc_id, @callback
|
2016-06-17 07:17:22 -04:00
|
|
|
|
|
|
|
it "should get the pending updates", ->
|
|
|
|
@rclient.lrange
|
2017-05-12 08:11:04 -04:00
|
|
|
.calledWith("PendingUpdates:#{@doc_id}", 0, 7)
|
2016-06-17 07:17:22 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should delete the pending updates", ->
|
2017-05-12 08:11:04 -04:00
|
|
|
@rclient.ltrim
|
|
|
|
.calledWith("PendingUpdates:#{@doc_id}", 8, -1)
|
2016-06-17 07:17:22 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with the updates", ->
|
|
|
|
@callback.calledWith(null, @updates).should.equal true
|
|
|
|
|
|
|
|
describe "when the JSON doesn't parse", ->
|
|
|
|
beforeEach ->
|
|
|
|
@jsonUpdates = [
|
|
|
|
JSON.stringify { op: [{ i: "foo", p: 4 }] }
|
|
|
|
"broken json"
|
|
|
|
]
|
|
|
|
@rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
|
2017-05-02 10:38:33 -04:00
|
|
|
@RealTimeRedisManager.getPendingUpdatesForDoc @doc_id, @callback
|
2016-06-17 07:17:22 -04:00
|
|
|
|
|
|
|
it "should return an error to the callback", ->
|
|
|
|
@callback.calledWith(new Error("JSON parse error")).should.equal true
|
|
|
|
|
|
|
|
|
|
|
|
describe "getUpdatesLength", ->
|
|
|
|
beforeEach ->
|
|
|
|
@rclient.llen = sinon.stub().yields(null, @length = 3)
|
2017-05-02 10:38:33 -04:00
|
|
|
@RealTimeRedisManager.getUpdatesLength @doc_id, @callback
|
2016-06-17 07:17:22 -04:00
|
|
|
|
|
|
|
it "should look up the length", ->
|
|
|
|
@rclient.llen.calledWith("PendingUpdates:#{@doc_id}").should.equal true
|
|
|
|
|
|
|
|
it "should return the length", ->
|
|
|
|
@callback.calledWith(null, @length).should.equal true
|
2019-03-21 08:10:15 -04:00
|
|
|
|
|
|
|
describe "sendData", ->
|
|
|
|
beforeEach ->
|
|
|
|
@message_id = "doc:somehost:01020304-0"
|
|
|
|
@rclient.publish = sinon.stub()
|
|
|
|
@RealTimeRedisManager.sendData({op: "thisop"})
|
|
|
|
|
|
|
|
it "should send the op with a message id", ->
|
|
|
|
@rclient.publish.calledWith("applied-ops", JSON.stringify({op:"thisop",_id:@message_id})).should.equal true
|