3
0
mirror of https://github.com/britzl/monarch.git synced 2025-06-27 10:27:49 +02:00

Added callback tracker tests

This commit is contained in:
Björn Ritzl 2019-07-31 18:20:06 +02:00
parent f5879a5f63
commit d74efaf186
3 changed files with 77 additions and 0 deletions

View File

@ -18,9 +18,15 @@ function M.create()
-- @return Callback function -- @return Callback function
function instance.track() function instance.track()
callback_count = callback_count + 1 callback_count = callback_count + 1
local done = false
return function() return function()
if done then
return false, "The callback has already been invoked once"
end
done = true
callback_count = callback_count - 1 callback_count = callback_count - 1
invoke_if_done() invoke_if_done()
return true
end end
end end

View File

@ -1,10 +1,12 @@
local deftest = require "deftest.deftest" local deftest = require "deftest.deftest"
local test_monarch = require "test.test_monarch" local test_monarch = require "test.test_monarch"
local test_callback_tracker = require "test.test_callback_tracker"
function init(self) function init(self)
deftest.add(test_monarch) deftest.add(test_monarch)
deftest.add(test_callback_tracker)
deftest.run({ deftest.run({
coverage = { enabled = true }, coverage = { enabled = true },
--pattern = "preload", --pattern = "preload",

View File

@ -0,0 +1,69 @@
local unload = require "deftest.util.unload"
local cowat = require "test.cowait"
local callback_tracker = require "monarch.utils.callback_tracker"
return function()
describe("callback tracker", function()
before(function()
callback_tracker = require "monarch.utils.callback_tracker"
end)
after(function()
unload.unload("monarch%..*")
end)
it("should be able to tell when all callbacks are done", function()
local tracker = callback_tracker.create()
local t1 = tracker.track()
local t2 = tracker.track()
local done = false
tracker.when_done(function() done = true end)
assert(not done, "It should not be done yet - No callback has completed")
t1()
assert(not done, "It should not be done yet - Only one callback has completed")
t2()
assert(done, "It should be done now - All callbacks have completed")
end)
it("should indicate if a tracked callback has been invoked more than once", function()
local tracker = callback_tracker.create()
local t = tracker.track()
local ok, err = t()
assert(ok == true and err == nil, "It should return true when successful")
ok, err = t()
assert(ok == false and err, "It should return false and a message when invoked multiple times")
end)
it("should not be possible to track the same callback more than one time", function()
local tracker = callback_tracker.create()
local t1 = tracker.track()
local t2 = tracker.track()
local done = false
tracker.when_done(function() done = true end)
assert(not done, "It should not be done yet - No callback has completed")
t1()
t1()
assert(not done, "It should not be done yet - Even if one callback has been invoked twice")
t2()
assert(done, "It should be done now - All callbacks have completed")
end)
it("should handle when callbacks are done before calling when_done()", function()
local tracker = callback_tracker.create()
local t1 = tracker.track()
local t2 = tracker.track()
t1()
t2()
local done = false
tracker.when_done(function() done = true end)
assert(done, "It should be done now - All callbacks have completed")
end)
end)
end