From d2730c8d6b7da77acb408d22c573b3b97ddae1b1 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 12 Jul 2017 10:45:44 +0100 Subject: [PATCH] unit tests for locking timeouts --- .../coffee/LockManager/tryLockTests.coffee | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/services/document-updater/test/unit/coffee/LockManager/tryLockTests.coffee b/services/document-updater/test/unit/coffee/LockManager/tryLockTests.coffee index 7b52f416ab..82de2f45b8 100644 --- a/services/document-updater/test/unit/coffee/LockManager/tryLockTests.coffee +++ b/services/document-updater/test/unit/coffee/LockManager/tryLockTests.coffee @@ -19,7 +19,7 @@ describe 'LockManager - trying the lock', -> key_schema: blockingKey: ({doc_id}) -> "Blocking:#{doc_id}" } - "./Profiler": class Profiler + "./Profiler": @Profiler = class Profiler log: sinon.stub().returns { end: sinon.stub() } end: sinon.stub() @@ -48,3 +48,39 @@ describe 'LockManager - trying the lock', -> it "should return the callback with false", -> @callback.calledWith(null, false).should.equal true + describe "when it takes a long time for redis to set the lock", -> + beforeEach -> + @Profiler.prototype.end = () -> 7000 # take a long time + @Profiler.prototype.log = sinon.stub().returns { end: @Profiler.prototype.end } + @lockValue = "mock-lock-value" + @LockManager.randomLock = sinon.stub().returns @lockValue + @LockManager.releaseLock = sinon.stub().callsArgWith(2,null) + @set.callsArgWith(5, null, "OK") + + describe "in all cases", -> + beforeEach -> + @LockManager.tryLock @doc_id, @callback + + it "should set the lock key with an expiry if it is not set", -> + @set.calledWith("Blocking:#{@doc_id}", @lockValue, "EX", 30, "NX") + .should.equal true + + it "should try to release the lock", -> + @LockManager.releaseLock.calledWith(@doc_id, @lockValue).should.equal true + + describe "if the lock is released successfully", -> + beforeEach -> + @LockManager.releaseLock = sinon.stub().callsArgWith(2,null) + @LockManager.tryLock @doc_id, @callback + + it "should return the callback with false", -> + @callback.calledWith(null, false).should.equal true + + describe "if the lock has already timed out", -> + beforeEach -> + @LockManager.releaseLock = sinon.stub().callsArgWith(2, new Error("tried to release timed out lock")) + @LockManager.tryLock @doc_id, @callback + + it "should return the callback with an error", -> + e = new Error("tried to release timed out lock") + @callback.calledWith(e).should.equal true