Implement listen cancelling

Signed-off-by: Rahix <rahix@rahix.de>
This commit is contained in:
rahix 2019-04-17 02:46:24 +02:00
parent 719201e950
commit c33fdd41a6
3 changed files with 86 additions and 5 deletions

View file

@ -134,3 +134,64 @@ do
end
end
end
function test_listen_cancel()
local bc = BaseControl:new()
bc:register("lcan1", 123)
bc:finalize()
local n = 0
local id = bc:listen("lcan1", BaseControl.Query.Change, function()
n = n + 1
end)
assert_equal(0, n, "wrong number of listener invokations")
bc:set("lcan1", 321)
assert_equal(1, n, "wrong number of listener invokations")
-- Cancel the listener
bc:cancel("lcan1", id)
bc:set("lcan1", 12)
assert_equal(1, n, "wrong number of listener invokations")
assert_error_match("unknown", function()
bc:cancel("lcan1", id)
end)
assert_error_match("unknown", function()
bc:cancel("lcan2", "fooid")
end)
end
function test_listen_cancel_remote()
local bc1 = BaseControl:new()
local addr1 = network.get_scene()
bc1:register("lcanr1", 123)
bc1:finalize()
local bc2 = BaseControl:new()
local addr2 = network.get_scene()
local n = 0
local id, ok = bc2:listen("lcanr1", BaseControl.Query.Change, function()
n = n + 1
end)
assert_true(ok, "failed installing")
network.set_scene(addr1)
assert_equal(0, n, "wrong number of listener invokations")
bc1:set("lcanr1", 34)
assert_equal(1, n, "wrong number of listener invokations")
-- Dirty hack to get the test working
local save = bc2.listeners["lcanr1"][id]
network.set_scene(addr2)
bc2:cancel("lcanr1", id)
bc2.listeners["lcanr1"][id] = save
network.set_scene(addr1)
bc1:set("lcanr1", 21)
assert_equal(1, n, "wrong number of listener invokations")
end