Просмотр исходного кода

Merge pull request #25 from sharelatex/bg-error-on-out-of-order-ops

return error when out-of-order ops detected
Brian Gough 9 лет назад
Родитель
Сommit
5989d8f27f

+ 7 - 0
services/track-changes/app/coffee/UpdatesManager.coffee

@@ -16,6 +16,13 @@ module.exports = UpdatesManager =
 		if length == 0
 			return callback()
 
+		# check that ops are in the correct order
+		for op, i in rawUpdates when i > 0
+			thisVersion = op?.v
+			prevVersion = rawUpdates[i-1]?.v
+			if not (prevVersion < thisVersion)
+				logger.error project_id: project_id, doc_id: doc_id, rawUpdates:rawUpdates, temporary: temporary, thisVersion:thisVersion, prevVersion:prevVersion, "op versions out of order"
+
 		# FIXME: we no longer need the lastCompressedUpdate, so change functions not to need it
 		# CORRECTION:  we do use it to log the time in case of error
 		MongoManager.peekLastCompressedUpdate doc_id, (error, lastCompressedUpdate, lastVersion) ->

+ 14 - 0
services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee

@@ -137,6 +137,20 @@ describe "UpdatesManager", ->
 				it "should not insert any update into mongo", ->
 					@PackManager.insertCompressedUpdates.called.should.equal false
 
+			describe "when the raw ops are out of order", ->
+				beforeEach ->
+					@rawUpdates = [{ v: 13, op: "mock-op-13" }, { v: 12, op: "mock-op-12" }]
+					@UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback
+
+				it "should call the callback with an error", ->
+					@callback
+						.calledWith(new Error)
+						.should.equal true
+
+				it "should not insert any update into mongo", ->
+					@PackManager.insertCompressedUpdates.called.should.equal false
+
+
 		describe "when the raw ops need appending to existing history which is in S3", ->
 			beforeEach ->
 				@lastCompressedUpdate = null