Make cleanup more robust
Signed-off-by: Rahix <rahix@rahix.de>
This commit is contained in:
parent
af97600952
commit
0d852d7aac
3 changed files with 43 additions and 3 deletions
13
bc.lua
13
bc.lua
|
|
@ -116,9 +116,20 @@ function BaseControl:new(network)
|
||||||
}
|
}
|
||||||
setmetatable(self, {__index=BaseControl, __call=BaseControl.new})
|
setmetatable(self, {__index=BaseControl, __call=BaseControl.new})
|
||||||
|
|
||||||
|
-- Create a weak reference to self. This allows us to uninstall the
|
||||||
|
-- network handler automatically once this instance gets garbage-collected.
|
||||||
|
do
|
||||||
|
local weak = setmetatable({self=self}, {__mode="v"})
|
||||||
|
local network = self.network
|
||||||
|
|
||||||
self.network:start(function(remote, msg)
|
self.network:start(function(remote, msg)
|
||||||
self:_network_handler(remote, msg)
|
if weak.self ~= nil then
|
||||||
|
weak.self:_network_handler(remote, msg)
|
||||||
|
else
|
||||||
|
network:stop()
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
end
|
||||||
self.network:broadcast{ty=Message.Hello}
|
self.network:broadcast{ty=Message.Hello}
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,10 @@ function network.deregister(addr)
|
||||||
nodes[addr] = nil
|
nodes[addr] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function network.get_callback(addr)
|
||||||
|
return nodes[addr]
|
||||||
|
end
|
||||||
|
|
||||||
function network.send(addr, port, msg)
|
function network.send(addr, port, msg)
|
||||||
local callback = nodes[addr]
|
local callback = nodes[addr]
|
||||||
if callback == nil then
|
if callback == nil then
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,14 @@ function test_clean_simple()
|
||||||
bc:close()
|
bc:close()
|
||||||
|
|
||||||
local bc = BaseControl:finalize()
|
local bc = BaseControl:finalize()
|
||||||
|
assert_true(network.get_callback(addr) ~= nil, "callback not registered")
|
||||||
bc:close()
|
bc:close()
|
||||||
|
|
||||||
assert_error(function()
|
assert_error(function()
|
||||||
bc:nouns()
|
bc:nouns()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
assert_nil(network.get_callback(addr), "callback still in network")
|
||||||
end
|
end
|
||||||
|
|
||||||
function test_clean_network()
|
function test_clean_network()
|
||||||
|
|
@ -66,3 +69,25 @@ function test_clean_network2()
|
||||||
bc2:get("cln3")
|
bc2:get("cln3")
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function test_unclean_deregister()
|
||||||
|
local bc = BaseControl:new()
|
||||||
|
local addr = network.get_scene()
|
||||||
|
|
||||||
|
network.set_scene("UNCL-DEREG")
|
||||||
|
network.send(
|
||||||
|
addr,
|
||||||
|
BaseControl.Network.default_port,
|
||||||
|
serialization.serialize{ty=BaseControl.Message.Hello}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_true(network.get_callback(addr) ~= nil, "callback not registered")
|
||||||
|
bc = nil
|
||||||
|
collectgarbage()
|
||||||
|
network.send(
|
||||||
|
addr,
|
||||||
|
BaseControl.Network.default_port,
|
||||||
|
serialization.serialize{ty=BaseControl.Message.Hello}
|
||||||
|
)
|
||||||
|
assert_nil(network.get_callback(addr), "callback still in network")
|
||||||
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue