From aaa9741b3c9cf36cd203d2ac077217ffaf34bbd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Sun, 24 Apr 2011 16:56:54 +0200 Subject: [PATCH] every is working. minor tests outstanding --- cron.lua | 5 ++--- spec/cron_spec.lua | 13 ++++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cron.lua b/cron.lua index 0ef5cb9..ff55597 100644 --- a/cron.lua +++ b/cron.lua @@ -36,13 +36,12 @@ end local function updatePeriodicEntry(self, dt) self.running = self.running + dt - if self.running >= self.time then + while self.running >= self.time do self.callback(unpack(self.args)) - self.running=0 + self.running = self.running - self.time end end - local cron = {} function cron.reset() diff --git a/spec/cron_spec.lua b/spec/cron_spec.lua index 6af5081..4598ec9 100644 --- a/spec/cron_spec.lua +++ b/spec/cron_spec.lua @@ -70,16 +70,23 @@ context( 'cron', function() end) test( 'Should execute periodical actions periodically', function() - cron.every(2, count) + cron.every(3, count) cron.update(1) assert_equal(counter, 0) - cron.update(1) + cron.update(2) assert_equal(counter, 1) cron.update(2) - assert_equal(counter, 2) + assert_equal(counter, 1) cron.update(1) assert_equal(counter, 2) end) + + test( 'Should execute the same action multiple times on a single update if appropiate', function() + cron.every(1, count) + cron.update(2) + assert_equal(counter, 2) + end) + end)