cancel is also working, with tests

This commit is contained in:
Enrique García Cota 2011-04-24 17:06:35 +02:00
parent aaa9741b3c
commit 63152e9d1b
2 changed files with 40 additions and 1 deletions

View File

@ -48,6 +48,10 @@ function cron.reset()
entries = {} entries = {}
end end
function cron.cancel(id)
entries[id] = nil
end
function cron.after(time, callback, ...) function cron.after(time, callback, ...)
checkTimeAndCallback(time, callback) checkTimeAndCallback(time, callback)
return newEntry(time, callback, updateTimedEntry, ...) return newEntry(time, callback, updateTimedEntry, ...)

View File

@ -22,7 +22,7 @@ context( 'cron', function()
end) end)
context('reset', function() context('reset', function()
test('Should cancel all actions', function() test('Should cancel all timed actions', function()
cron.after(1, count) cron.after(1, count)
cron.after(2, count) cron.after(2, count)
cron.update(1) cron.update(1)
@ -31,6 +31,16 @@ context( 'cron', function()
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert_equal(counter, 1)
end) end)
test('Should cancel all periodical actions', function()
cron.every(1, count)
cron.update(1)
assert_equal(counter, 1)
cron.reset()
cron.update(1)
assert_equal(counter, 1)
end)
end) end)
context( 'after', function() context( 'after', function()
@ -87,6 +97,31 @@ context( 'cron', function()
assert_equal(counter, 2) assert_equal(counter, 2)
end) end)
test( 'Should respect parameters', function()
cron.every(1, count, 2)
cron.update(2)
assert_equal(counter, 4)
end)
end)
context( 'cancel', function()
test( 'Should allow the cancelation of timed actions', function()
local id = cron.after(1, count)
cron.update(1)
assert_equal(counter, 1)
cron.cancel(id)
cron.update(1)
assert_equal(counter, 1)
end)
test( 'Should allow the cancelation of periodical actions', function()
local id = cron.every(1, count)
cron.update(1)
assert_equal(counter, 1)
cron.cancel(id)
cron.update(1)
assert_equal(counter, 1)
end)
end) end)