2012-10-27 16:44:20 +00:00
|
|
|
local cron = require 'cron'
|
2011-04-24 13:24:18 +00:00
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
describe( 'cron', function()
|
2011-04-24 13:24:18 +00:00
|
|
|
|
2011-04-24 14:15:44 +00:00
|
|
|
local counter = 0
|
|
|
|
local function count(amount)
|
|
|
|
amount = amount or 1
|
|
|
|
counter = counter + amount
|
|
|
|
end
|
2011-05-01 21:46:48 +00:00
|
|
|
local countable = setmetatable({}, {__call = count})
|
2011-04-24 14:15:44 +00:00
|
|
|
|
|
|
|
before(function()
|
|
|
|
counter = 0
|
|
|
|
cron.reset()
|
|
|
|
end)
|
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
describe('.update', function()
|
|
|
|
it('throws an error if dt is a negative number', function()
|
2011-04-24 14:15:44 +00:00
|
|
|
assert_error(function() cron.update() end)
|
|
|
|
assert_error(function() cron.update(-1) end)
|
|
|
|
assert_not_error(function() cron.update(1) end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
describe('.reset', function()
|
|
|
|
it('Cancels all timed actions', function()
|
2011-04-24 14:15:44 +00:00
|
|
|
cron.after(1, count)
|
|
|
|
cron.after(2, count)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 1)
|
|
|
|
cron.reset()
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 1)
|
|
|
|
end)
|
2011-04-24 15:06:35 +00:00
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
it('Cancels all periodical actions', function()
|
2011-04-24 15:06:35 +00:00
|
|
|
cron.every(1, count)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 1)
|
|
|
|
cron.reset()
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 1)
|
|
|
|
end)
|
|
|
|
|
2011-04-24 14:15:44 +00:00
|
|
|
end)
|
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
describe('.after', function()
|
|
|
|
it('Throws error if time is not a positive number, or callback is not callable', function()
|
2011-04-24 14:15:44 +00:00
|
|
|
assert_error(function() cron.after('error', count) end)
|
|
|
|
assert_error(function() cron.after(2, 'error') end)
|
|
|
|
assert_error(function() cron.after(-2, count) end)
|
2011-05-01 21:46:48 +00:00
|
|
|
assert_error(function() cron.after(2, {}) end)
|
2011-04-24 14:15:44 +00:00
|
|
|
assert_not_error(function() cron.after(2, count) end)
|
2011-05-01 21:46:48 +00:00
|
|
|
assert_not_error(function() cron.after(2, countable) end)
|
2011-04-24 14:15:44 +00:00
|
|
|
end)
|
2012-10-27 16:44:20 +00:00
|
|
|
|
|
|
|
it('Executes timed actions only once, at the right time', function()
|
2011-04-24 14:15:44 +00:00
|
|
|
cron.after(2, count)
|
|
|
|
cron.after(4, count)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 0)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 1)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 1)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 2)
|
|
|
|
end)
|
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
it('Passes on parameters to the function, if specified', function()
|
2011-04-24 14:15:44 +00:00
|
|
|
cron.after(1, count, 2)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 2)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
describe('.every', function()
|
|
|
|
it('Throws errors if time is not a positive number, or callback is not function', function()
|
2011-04-24 14:29:51 +00:00
|
|
|
assert_error(function() cron.every('error', count) end)
|
|
|
|
assert_error(function() cron.every(2, 'error') end)
|
|
|
|
assert_error(function() cron.every(-2, count) end)
|
2011-05-01 21:46:48 +00:00
|
|
|
assert_error(function() cron.every(-2, {}) end)
|
2011-04-24 14:29:51 +00:00
|
|
|
assert_not_error(function() cron.every(2, count) end)
|
2011-05-01 21:46:48 +00:00
|
|
|
assert_not_error(function() cron.every(2, countable) end)
|
2011-04-24 14:29:51 +00:00
|
|
|
end)
|
2011-04-24 14:37:02 +00:00
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
it('Executes periodical actions periodically', function()
|
2011-04-24 14:56:54 +00:00
|
|
|
cron.every(3, count)
|
2011-04-24 14:37:02 +00:00
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 0)
|
2011-04-24 14:56:54 +00:00
|
|
|
cron.update(2)
|
2011-04-24 14:37:02 +00:00
|
|
|
assert_equal(counter, 1)
|
|
|
|
cron.update(2)
|
2011-04-24 14:56:54 +00:00
|
|
|
assert_equal(counter, 1)
|
2011-04-24 14:37:02 +00:00
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 2)
|
|
|
|
end)
|
2011-04-24 14:56:54 +00:00
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
it('Executes the same action multiple times on a single update if appropiate', function()
|
2011-04-24 14:56:54 +00:00
|
|
|
cron.every(1, count)
|
|
|
|
cron.update(2)
|
|
|
|
assert_equal(counter, 2)
|
|
|
|
end)
|
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
it('Respects parameters', function()
|
2011-04-24 15:06:35 +00:00
|
|
|
cron.every(1, count, 2)
|
|
|
|
cron.update(2)
|
|
|
|
assert_equal(counter, 4)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
describe('.cancel', function()
|
|
|
|
it('Cancels timed entries', function()
|
2011-04-24 15:06:35 +00:00
|
|
|
local id = cron.after(1, count)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 1)
|
|
|
|
cron.cancel(id)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 1)
|
|
|
|
end)
|
|
|
|
|
2012-10-27 16:44:20 +00:00
|
|
|
it('Cancels periodical entries', function()
|
2011-04-24 15:06:35 +00:00
|
|
|
local id = cron.every(1, count)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 1)
|
|
|
|
cron.cancel(id)
|
|
|
|
cron.update(1)
|
|
|
|
assert_equal(counter, 1)
|
|
|
|
end)
|
2011-04-24 14:29:51 +00:00
|
|
|
end)
|
2011-04-24 13:24:18 +00:00
|
|
|
end)
|