From bf2620ee0cb2939b643b174e0c6e09a46dbcd5ce Mon Sep 17 00:00:00 2001 From: James Allen Date: Thu, 8 Dec 2016 11:37:31 +0000 Subject: [PATCH] Return ranges from docupdater to client --- .../app/coffee/DocumentUpdaterManager.coffee | 2 +- .../app/coffee/WebsocketController.coffee | 4 +-- .../acceptance/coffee/JoinDocTests.coffee | 25 ++++++++++--------- .../coffee/helpers/FixturesManager.coffee | 4 +-- .../coffee/DocumentUpdaterManagerTests.coffee | 8 ++++-- .../coffee/WebsocketControllerTests.coffee | 7 +++--- 6 files changed, 28 insertions(+), 22 deletions(-) diff --git a/services/real-time/app/coffee/DocumentUpdaterManager.coffee b/services/real-time/app/coffee/DocumentUpdaterManager.coffee index 9e25585b1c..9d47a3ffc1 100644 --- a/services/real-time/app/coffee/DocumentUpdaterManager.coffee +++ b/services/real-time/app/coffee/DocumentUpdaterManager.coffee @@ -22,7 +22,7 @@ module.exports = DocumentUpdaterManager = body = JSON.parse(body) catch error return callback(error) - callback null, body?.lines, body?.version, body?.ops + callback null, body?.lines, body?.version, body?.ranges, body?.ops else if res.statusCode == 422 # Unprocessable Entity err = new Error("doc updater could not load requested ops") err.statusCode = res.statusCode diff --git a/services/real-time/app/coffee/WebsocketController.coffee b/services/real-time/app/coffee/WebsocketController.coffee index ad7825f595..c6aec7379d 100644 --- a/services/real-time/app/coffee/WebsocketController.coffee +++ b/services/real-time/app/coffee/WebsocketController.coffee @@ -79,7 +79,7 @@ module.exports = WebsocketController = AuthorizationManager.assertClientCanViewProject client, (error) -> return callback(error) if error? - DocumentUpdaterManager.getDocument project_id, doc_id, fromVersion, (error, lines, version, ops) -> + DocumentUpdaterManager.getDocument project_id, doc_id, fromVersion, (error, lines, version, ranges, ops) -> return callback(error) if error? # Encode any binary bits of data so it can go via WebSockets # See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html @@ -93,7 +93,7 @@ module.exports = WebsocketController = escapedLines.push line AuthorizationManager.addAccessToDoc client, doc_id client.join(doc_id) - callback null, escapedLines, version, ops + callback null, escapedLines, version, ops, ranges logger.log {user_id, project_id, doc_id, fromVersion, client_id: client.id}, "client joined doc" leaveDoc: (client, doc_id, callback = (error) ->) -> diff --git a/services/real-time/test/acceptance/coffee/JoinDocTests.coffee b/services/real-time/test/acceptance/coffee/JoinDocTests.coffee index 056909a627..a9d5406345 100644 --- a/services/real-time/test/acceptance/coffee/JoinDocTests.coffee +++ b/services/real-time/test/acceptance/coffee/JoinDocTests.coffee @@ -13,6 +13,7 @@ describe "joinDoc", -> @lines = ["test", "doc", "lines"] @version = 42 @ops = ["mock", "doc", "ops"] + @ranges = {"mock": "ranges"} describe "when authorised readAndWrite", -> before (done) -> @@ -24,7 +25,7 @@ describe "joinDoc", -> cb(e) (cb) => - FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) => + FixturesManager.setUpDoc @project_id, {@lines, @version, @ops, @ranges}, (e, {@doc_id}) => cb(e) (cb) => @@ -43,8 +44,8 @@ describe "joinDoc", -> .calledWith(@project_id, @doc_id, -1) .should.equal true - it "should return the doc lines, version and ops", -> - @returnedArgs.should.deep.equal [@lines, @version, @ops] + it "should return the doc lines, version, ranges and ops", -> + @returnedArgs.should.deep.equal [@lines, @version, @ops, @ranges] it "should have joined the doc room", (done) -> RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) => @@ -61,7 +62,7 @@ describe "joinDoc", -> cb(e) (cb) => - FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) => + FixturesManager.setUpDoc @project_id, {@lines, @version, @ops, @ranges}, (e, {@doc_id}) => cb(e) (cb) => @@ -80,8 +81,8 @@ describe "joinDoc", -> .calledWith(@project_id, @doc_id, -1) .should.equal true - it "should return the doc lines, version and ops", -> - @returnedArgs.should.deep.equal [@lines, @version, @ops] + it "should return the doc lines, version, ranges and ops", -> + @returnedArgs.should.deep.equal [@lines, @version, @ops, @ranges] it "should have joined the doc room", (done) -> RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) => @@ -98,7 +99,7 @@ describe "joinDoc", -> cb(e) (cb) => - FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) => + FixturesManager.setUpDoc @project_id, {@lines, @version, @ops, @ranges}, (e, {@doc_id}) => cb(e) (cb) => @@ -117,8 +118,8 @@ describe "joinDoc", -> .calledWith(@project_id, @doc_id, -1) .should.equal true - it "should return the doc lines, version and ops", -> - @returnedArgs.should.deep.equal [@lines, @version, @ops] + it "should return the doc lines, version, ranges and ops", -> + @returnedArgs.should.deep.equal [@lines, @version, @ops, @ranges] it "should have joined the doc room", (done) -> RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) => @@ -140,7 +141,7 @@ describe "joinDoc", -> cb(e) (cb) => - FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) => + FixturesManager.setUpDoc @project_id, {@lines, @version, @ops, @ranges}, (e, {@doc_id}) => cb(e) (cb) => @@ -159,8 +160,8 @@ describe "joinDoc", -> .calledWith(@project_id, @doc_id, @fromVersion) .should.equal true - it "should return the doc lines, version and ops", -> - @returnedArgs.should.deep.equal [@lines, @version, @ops] + it "should return the doc lines, version, ranges and ops", -> + @returnedArgs.should.deep.equal [@lines, @version, @ops, @ranges] it "should have joined the doc room", (done) -> RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) => diff --git a/services/real-time/test/acceptance/coffee/helpers/FixturesManager.coffee b/services/real-time/test/acceptance/coffee/helpers/FixturesManager.coffee index 9ee5f4ef6f..0889b45c2a 100644 --- a/services/real-time/test/acceptance/coffee/helpers/FixturesManager.coffee +++ b/services/real-time/test/acceptance/coffee/helpers/FixturesManager.coffee @@ -32,9 +32,9 @@ module.exports = FixturesManager = options.lines ||= ["doc", "lines"] options.version ||= 42 options.ops ||= ["mock", "ops"] - {doc_id, lines, version, ops} = options + {doc_id, lines, version, ops, ranges} = options - MockDocUpdaterServer.createMockDoc project_id, doc_id, {lines, version, ops} + MockDocUpdaterServer.createMockDoc project_id, doc_id, {lines, version, ops, ranges} MockDocUpdaterServer.run (error) => throw error if error? callback null, {project_id, doc_id, lines, version, ops} diff --git a/services/real-time/test/unit/coffee/DocumentUpdaterManagerTests.coffee b/services/real-time/test/unit/coffee/DocumentUpdaterManagerTests.coffee index 8e75da1d0c..fbf44aeffe 100644 --- a/services/real-time/test/unit/coffee/DocumentUpdaterManagerTests.coffee +++ b/services/real-time/test/unit/coffee/DocumentUpdaterManagerTests.coffee @@ -20,6 +20,9 @@ describe 'DocumentUpdaterManager', -> 'logger-sharelatex': @logger = {log: sinon.stub(), error: sinon.stub(), warn: sinon.stub()} 'request': @request = {} 'redis-sharelatex' : createClient: () => @rclient + 'metrics-sharelatex': @Metrics = + Timer: class Timer + done: () -> describe "getDocument", -> beforeEach -> @@ -31,6 +34,7 @@ describe 'DocumentUpdaterManager', -> lines: @lines version: @version ops: @ops = ["mock-op-1", "mock-op-2"] + ranges: @ranges = {"mock": "ranges"} @fromVersion = 2 @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body) @DocumentUpdaterManager.getDocument @project_id, @doc_id, @fromVersion, @callback @@ -39,8 +43,8 @@ describe 'DocumentUpdaterManager', -> url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}?fromVersion=#{@fromVersion}" @request.get.calledWith(url).should.equal true - it "should call the callback with the lines and version", -> - @callback.calledWith(null, @lines, @version, @ops).should.equal true + it "should call the callback with the lines, version, ranges and ops", -> + @callback.calledWith(null, @lines, @version, @ranges, @ops).should.equal true describe "when the document updater API returns an error", -> beforeEach -> diff --git a/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee b/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee index 48eccd4bee..7777327282 100644 --- a/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee +++ b/services/real-time/test/unit/coffee/WebsocketControllerTests.coffee @@ -170,11 +170,12 @@ describe 'WebsocketController', -> @doc_lines = ["doc", "lines"] @version = 42 @ops = ["mock", "ops"] + @ranges = { "mock": "ranges" } @client.params.project_id = @project_id @AuthorizationManager.addAccessToDoc = sinon.stub() @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null) - @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(3, null, @doc_lines, @version, @ops) + @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(3, null, @doc_lines, @version, @ranges, @ops) describe "with a fromVersion", -> beforeEach -> @@ -201,9 +202,9 @@ describe 'WebsocketController', -> .calledWith(@doc_id) .should.equal true - it "should call the callback with the lines, version and ops", -> + it "should call the callback with the lines, version, ranges and ops", -> @callback - .calledWith(null, @doc_lines, @version, @ops) + .calledWith(null, @doc_lines, @version, @ops, @ranges) .should.equal true it "should increment the join-doc metric", ->