Move check of zero length op array up a level

This commit is contained in:
James Allen 2016-09-08 11:41:59 +01:00
parent 993aab7a78
commit 5ce15c4d60
4 changed files with 15 additions and 4 deletions

View file

@ -22,7 +22,9 @@ module.exports = TrackChangesManager =
return callback(error)
FLUSH_EVERY_N_OPS: 50
pushUncompressedHistoryOps: (project_id, doc_id, ops, callback = (error) ->) ->
pushUncompressedHistoryOps: (project_id, doc_id, ops = [], callback = (error) ->) ->
if ops.length == 0
return callback()
WebRedisManager.pushUncompressedHistoryOps project_id, doc_id, ops, (error, length) ->
return callback(error) if error?
# We want to flush every 50 ops, i.e. 50, 100, 150, etc

View file

@ -24,7 +24,7 @@ module.exports = WebRedisManager =
pushUncompressedHistoryOps: (project_id, doc_id, ops = [], callback = (error, length) ->) ->
if ops.length == 0
return callback(null, 0)
return callback(new Error("cannot push no ops")) # This should never be called with no ops, but protect against a redis error if we sent an empty array to rpush
jsonOps = ops.map (op) -> JSON.stringify op
async.parallel [
(cb) -> rclient.rpush "UncompressedHistoryOps:#{doc_id}", jsonOps..., cb

View file

@ -100,4 +100,13 @@ describe "TrackChangesManager", ->
"error flushing doc to track changes api"
)
.should.equal true
describe "with no ops", ->
beforeEach ->
@WebRedisManager.pushUncompressedHistoryOps = sinon.stub().callsArgWith(3, null, 1)
@TrackChangesManager.pushUncompressedHistoryOps @project_id, @doc_id, [], @callback
it "should not call WebRedisManager.pushUncompressedHistoryOps", ->
@WebRedisManager.pushUncompressedHistoryOps.called.should.equal false

View file

@ -111,5 +111,5 @@ describe "WebRedisManager", ->
.called
.should.equal false
it "should call the callback with the length", ->
@callback.calledWith(null, 0).should.equal true
it "should call the callback with an error", ->
@callback.calledWith(new Error("cannot push no ops")).should.equal true