From bed64d26c89d430efb0a6f2792bb4fd19f08b50e Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 31 May 2017 15:33:59 +0100 Subject: [PATCH 1/3] check if doc is missing from DocsIn set --- .../document-updater/app/coffee/RedisManager.coffee | 12 +++++++++++- .../coffee/RedisManager/RedisManagerTests.coffee | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/services/document-updater/app/coffee/RedisManager.coffee b/services/document-updater/app/coffee/RedisManager.coffee index 5e2cc5c84b..365156ebf6 100644 --- a/services/document-updater/app/coffee/RedisManager.coffee +++ b/services/document-updater/app/coffee/RedisManager.coffee @@ -113,7 +113,17 @@ module.exports = RedisManager = if doc_project_id? and doc_project_id isnt project_id logger.error project_id: project_id, doc_id: doc_id, doc_project_id: doc_project_id, "doc not in project" return callback(new Errors.NotFoundError("document not found")) - callback null, docLines, version, ranges + + # doc is not in redis, bail out + if !lines? + return callback null, docLines, version, ranges + + # doc should be in project set, check if missing (workaround for missing docs from putDoc) + rclient.sadd keys.docsInProject(project_id:project_id), doc_id, (error, result) -> + return callback(error) if error? + if result isnt 0 # doc should already be in set + logger.error project_id: project_id, doc_id: doc_id, doc_project_id: doc_project_id, "doc missing from docsInProject set" + callback null, docLines, version, ranges getDocVersion: (doc_id, callback = (error, version) ->) -> rclient.get keys.docVersion(doc_id: doc_id), (error, version) -> diff --git a/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee b/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee index 26eaaf0892..b97bdd5a0d 100644 --- a/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee +++ b/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee @@ -58,6 +58,7 @@ describe "RedisManager", -> @json_ranges = JSON.stringify @ranges @rclient.get = sinon.stub() @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @hash, @project_id, @json_ranges]) + @rclient.sadd = sinon.stub().yields() describe "successfully", -> beforeEach -> From 12e8eaa9b6a8b240c27940a9acf90695c06833a6 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 31 May 2017 16:08:33 +0100 Subject: [PATCH 2/3] fix bug in doclines check --- services/document-updater/app/coffee/RedisManager.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/document-updater/app/coffee/RedisManager.coffee b/services/document-updater/app/coffee/RedisManager.coffee index 365156ebf6..935c569d94 100644 --- a/services/document-updater/app/coffee/RedisManager.coffee +++ b/services/document-updater/app/coffee/RedisManager.coffee @@ -115,7 +115,7 @@ module.exports = RedisManager = return callback(new Errors.NotFoundError("document not found")) # doc is not in redis, bail out - if !lines? + if !docLines? return callback null, docLines, version, ranges # doc should be in project set, check if missing (workaround for missing docs from putDoc) From 729216c9b147a66498073a519481a31eef281782 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 31 May 2017 16:08:45 +0100 Subject: [PATCH 3/3] add unit tests for DocsIn check --- .../RedisManager/RedisManagerTests.coffee | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee b/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee index b97bdd5a0d..d57f507865 100644 --- a/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee +++ b/services/document-updater/test/unit/coffee/RedisManager/RedisManagerTests.coffee @@ -58,7 +58,7 @@ describe "RedisManager", -> @json_ranges = JSON.stringify @ranges @rclient.get = sinon.stub() @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @hash, @project_id, @json_ranges]) - @rclient.sadd = sinon.stub().yields() + @rclient.sadd = sinon.stub().yields(null, 0) describe "successfully", -> beforeEach -> @@ -84,6 +84,11 @@ describe "RedisManager", -> .calledWith("Ranges:#{@doc_id}") .should.equal true + it "should check if the document is in the DocsIn set", -> + @rclient.sadd + .calledWith("DocsIn:#{@project_id}") + .should.equal true + it 'should return the document', -> @callback .calledWith(null, @lines, @version, @ranges) @@ -93,6 +98,40 @@ describe "RedisManager", -> @logger.error.calledWith() .should.equal false + describe "when the document is not present", -> + beforeEach -> + @rclient.exec = sinon.stub().callsArgWith(0, null, [null, null, null, null, null]) + @rclient.sadd = sinon.stub().yields() + @RedisManager.getDoc @project_id, @doc_id, @callback + + it "should not check if the document is in the DocsIn set", -> + @rclient.sadd + .calledWith("DocsIn:#{@project_id}") + .should.equal false + + it 'should return an empty result', -> + @callback + .calledWith(null, null, 0, {}) + .should.equal true + + it 'should not log any errors', -> + @logger.error.calledWith() + .should.equal false + + describe "when the document is missing from the DocsIn set", -> + beforeEach -> + @rclient.sadd = sinon.stub().yields(null, 1) + @RedisManager.getDoc @project_id, @doc_id, @callback + + it 'should log an error', -> + @logger.error.calledWith() + .should.equal true + + it 'should return the document', -> + @callback + .calledWith(null, @lines, @version, @ranges) + .should.equal true + describe "with a corrupted document", -> beforeEach -> @badHash = "INVALID-HASH-VALUE"