diff --git a/services/track-changes/app/coffee/UpdatesManager.coffee b/services/track-changes/app/coffee/UpdatesManager.coffee index fa4016f632..f01681e5f0 100644 --- a/services/track-changes/app/coffee/UpdatesManager.coffee +++ b/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) -> diff --git a/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee b/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee index 4a7d463b37..c422fffec0 100644 --- a/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee +++ b/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