Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions queue/abstract/driver/fifo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,21 @@ function tube.create_space(space_name, opts)
return space
end

-- validate space of queue
local function validate_space(space)
Copy link
Contributor

@LeonidVas LeonidVas Feb 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, WET practice can be used here.
But I suggest using something like:

local function validate_space(space, indexes)
	local indexes = {'task_id', 'status'}
	for _, index in pairs(indexes) do
		if space.index[index] == nil then
			error('space does not have ' .. index .. ' index')
		end
	end

-- 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,
Expand Down
16 changes: 16 additions & 0 deletions queue/abstract/driver/fifottl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions queue/abstract/driver/utube.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions queue/abstract/driver/utubettl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 33 additions & 1 deletion t/010-fifo.t
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not proper use of the index lua api and it is not guaranteed to work in a future. OTOH, I don't see anything that would block usage of <index_object>:drop() and then re-creation of the space for a next test.

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 :
48 changes: 47 additions & 1 deletion t/020-fifottl.t
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 48 additions & 1 deletion t/030-utube.t
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 :
66 changes: 65 additions & 1 deletion t/040-utubettl.t
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 :