diff --git a/queue/abstract/driver/fifo.lua b/queue/abstract/driver/fifo.lua index 2440cb94..684c0028 100644 --- a/queue/abstract/driver/fifo.lua +++ b/queue/abstract/driver/fifo.lua @@ -33,8 +33,21 @@ function tube.create_space(space_name, opts) return space end +-- validate space of queue +local function validate_space(space) + -- check indexes + if space.index.task_id == nil then + error('space does not have task_id index') + end + if space.index.status == nil then + error("space does not have status index") + end +end + -- start tube on space function tube.new(space, on_task_change) + validate_space(space) + on_task_change = on_task_change or (function() end) local self = setmetatable({ space = space, diff --git a/queue/abstract/driver/fifottl.lua b/queue/abstract/driver/fifottl.lua index bacfa854..bb38eea5 100644 --- a/queue/abstract/driver/fifottl.lua +++ b/queue/abstract/driver/fifottl.lua @@ -177,8 +177,24 @@ local function fifottl_fiber(self) end end +-- validate space of queue +local function validate_space(space) + -- check indexes + if space.index.task_id == nil then + error("space does not have task_id index") + end + if space.index.status == nil then + error("space does not have status index") + end + if space.index.watch == nil then + error("space does not have watch index") + end +end + -- start tube on space function tube.new(space, on_task_change, opts) + validate_space(space) + on_task_change = on_task_change or (function() end) local self = setmetatable({ space = space, diff --git a/queue/abstract/driver/utube.lua b/queue/abstract/driver/utube.lua index bca41b79..d99f3105 100644 --- a/queue/abstract/driver/utube.lua +++ b/queue/abstract/driver/utube.lua @@ -41,8 +41,24 @@ function tube.create_space(space_name, opts) return space end +-- validate space of queue +local function validate_space(space) + -- check indexes + if space.index.task_id == nil then + error("space does not have task_id index") + end + if space.index.status == nil then + error("space does not have status index") + end + if space.index.utube == nil then + error("space does not have utube index") + end +end + -- start tube on space function tube.new(space, on_task_change) + validate_space(space) + on_task_change = on_task_change or (function() end) local self = setmetatable({ space = space, diff --git a/queue/abstract/driver/utubettl.lua b/queue/abstract/driver/utubettl.lua index 83ea5b7c..58850490 100644 --- a/queue/abstract/driver/utubettl.lua +++ b/queue/abstract/driver/utubettl.lua @@ -186,8 +186,27 @@ local function utubettl_fiber(self) end end +-- validate space of queue +local function validate_space(space) + -- check indexes + if space.index.task_id == nil then + error("space does not have task_id index") + end + if space.index.status == nil then + error("space does not have status index") + end + if space.index.watch == nil then + error("space does not have watch index") + end + if space.index.utube == nil then + error("space does not have utube index") + end +end + -- start tube on space function tube.new(space, on_task_change, opts) + validate_space(space) + on_task_change = on_task_change or (function() end) local self = setmetatable({ space = space, diff --git a/t/010-fifo.t b/t/010-fifo.t index 300b892f..c496676e 100755 --- a/t/010-fifo.t +++ b/t/010-fifo.t @@ -3,7 +3,7 @@ local yaml = require('yaml') local fiber = require('fiber') local test = require('tap').test() -test:plan(14) +test:plan(15) local queue = require('queue') local state = require('queue.abstract.state') @@ -292,6 +292,38 @@ test:test('if_not_exists test', function(test) test:isnt(tube, tube_new, "if_not_exists if tube doesn't exists") end) +test:test('Space of queue is corrupted', function(test) + test:plan(2) + + local fifo = require('queue.abstract.driver.fifo') + local space = fifo.create_space('corrupted_fifo_space', { engine = engine }) + + local task_id = space.index.task_id + local status = space.index.status + + test:test('task_id index does not exist', function(test) + test:plan(2) + + space.index.task_id = nil + space.index.status = status + + local q, e = pcall(fifo.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have task_id index') ~= nil, 'text of exception') + end) + + test:test('status index does not exist', function(test) + test:plan(2) + + space.index.task_id = task_id + space.index.status = nil + + local q, e = pcall(fifo.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have status index') ~= nil, 'text of exception') + end) +end) + tnt.finish() os.exit(test:check() == true and 0 or -1) -- vim: set ft=lua : diff --git a/t/020-fifottl.t b/t/020-fifottl.t index dfeceafc..57137710 100755 --- a/t/020-fifottl.t +++ b/t/020-fifottl.t @@ -2,7 +2,7 @@ local fiber = require('fiber') local test = require('tap').test() -test:plan(15) +test:plan(16) local queue = require('queue') local state = require('queue.abstract.state') @@ -262,6 +262,52 @@ test:test('buried task in a dropped queue', function(test) test:ok(true, 'queue does not hang') end) +test:test('Space of queue is corrupted', function(test) + test:plan(3) + + local fifottl = require('queue.abstract.driver.fifottl') + local space = fifottl.create_space('corrupted_fifottl_space', { engine = engine }) + + local task_id = space.index.task_id + local status = space.index.status + local watch = space.index.watch + + test:test('task_id index does not exist', function(test) + test:plan(2) + + space.index.task_id = nil + space.index.status = status + space.index.watch = watch + + local q, e = pcall(fifottl.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have task_id index') ~= nil, 'text of exception') + end) + + test:test('status index does not exist', function(test) + test:plan(2) + + space.index.task_id = task_id + space.index.status = nil + space.index.watch = watch + + local q, e = pcall(fifottl.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have status index') ~= nil, 'text of exception') + end) + + test:test('watch index does not exist', function(test) + test:plan(2) + + space.index.task_id = task_id + space.index.status = status + space.index.watch = nil + + local q, e = pcall(fifottl.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have watch index') ~= nil, 'text of exception') + end) +end) tnt.finish() os.exit(test:check() == true and 0 or -1) diff --git a/t/030-utube.t b/t/030-utube.t index 384db5ce..76b77738 100755 --- a/t/030-utube.t +++ b/t/030-utube.t @@ -3,7 +3,7 @@ local yaml = require('yaml') local fiber = require('fiber') local test = (require('tap')).test() -test:plan(11) +test:plan(12) local queue = require('queue') local state = require('queue.abstract.state') @@ -156,6 +156,53 @@ test:test('if_not_exists test', function(test) test:isnt(tube, tube_new, "if_not_exists if tube doesn't exists") end) +test:test('Space of queue is corrupted', function(test) + test:plan(3) + + local ctube = require('queue.abstract.driver.utube') + local space = ctube.create_space('corrupted_utube_space', { engine = engine }) + + local task_id = space.index.task_id + local status = space.index.status + local utube = space.index.utube + + test:test('task_id index does not exist', function(test) + test:plan(2) + + space.index.task_id = nil + space.index.status = status + space.index.utube = utube + + local q, e = pcall(ctube.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have task_id index') ~= nil, 'text of exception') + end) + + test:test('status index does not exist', function(test) + test:plan(2) + + space.index.task_id = task_id + space.index.status = nil + space.index.utube = utube + + local q, e = pcall(ctube.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have status index') ~= nil, 'text of exception') + end) + + test:test('utube index does not exist', function(test) + test:plan(2) + + space.index.task_id = task_id + space.index.status = status + space.index.utube = nil + + local q, e = pcall(ctube.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have utube index') ~= nil, 'text of exception') + end) +end) + tnt.finish() os.exit(test:check() == true and 0 or -1) -- vim: set ft=lua : diff --git a/t/040-utubettl.t b/t/040-utubettl.t index a9d37f10..57d59bf6 100755 --- a/t/040-utubettl.t +++ b/t/040-utubettl.t @@ -3,7 +3,7 @@ local yaml = require('yaml') local fiber = require('fiber') local test = (require('tap')).test() -test:plan(16) +test:plan(17) local queue = require('queue') local state = require('queue.abstract.state') @@ -257,6 +257,70 @@ test:test('ttl after delay test', function(test) test:is(task.ttr, TTR * 1000000, 'check TTR after release') end) +test:test('Space of queue is corrupted', function(test) + test:plan(4) + + local ctubettl = require('queue.abstract.driver.utubettl') + local space = ctubettl.create_space('corrupted_utubettl_space', { engine = engine }) + + local task_id = space.index.task_id + local status = space.index.status + local watch = space.index.watch + local utube = space.index.utube + + test:test('task_id index does not exist', function(test) + test:plan(2) + + space.index.task_id = nil + space.index.status = status + space.index.watch = watch + space.index.utube = utube + + local q, e = pcall(ctubettl.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have task_id index') ~= nil, 'text of exception') + end) + + test:test('status index does not exist', function(test) + test:plan(2) + + space.index.task_id = task_id + space.index.status = nil + space.index.watch = watch + space.index.utube = utube + + local q, e = pcall(ctubettl.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have status index') ~= nil, 'text of exception') + end) + + test:test('watch index does not exist', function(test) + test:plan(2) + + space.index.task_id = task_id + space.index.status = status + space.index.watch = nil + space.index.utube = utube + + local q, e = pcall(ctubettl.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have watch index') ~= nil, 'text of exception') + end) + + test:test('utube index does not exist', function(test) + test:plan(2) + + space.index.task_id = task_id + space.index.status = status + space.index.watch = watch + space.index.utube = nil + + local q, e = pcall(ctubettl.new, space) + test:ok(not q, 'exception was thrown') + test:ok(e:match('space does not have utube index') ~= nil, 'text of exception') + end) +end) + tnt.finish() os.exit(test:check() == true and 0 or -1) -- vim: set ft=lua :