Implement close()
Signed-off-by: Rahix <rahix@rahix.de>
This commit is contained in:
parent
d6aa1637f1
commit
8fb07e22cc
3 changed files with 101 additions and 0 deletions
29
bc.lua
29
bc.lua
|
|
@ -205,6 +205,24 @@ function BaseControl:finalize(waits, timeout)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function BaseControl:close()
|
||||||
|
if self.live == true then
|
||||||
|
-- Deregister own nouns and verbs if we were live
|
||||||
|
local nouns, verbs = self:nouns(true), self:verbs(true)
|
||||||
|
if #nouns > 0 or #verbs > 0 then
|
||||||
|
self.network:broadcast{
|
||||||
|
ty=Message.Deregister,
|
||||||
|
nouns=nouns,
|
||||||
|
verbs=verbs,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self.network:stop()
|
||||||
|
|
||||||
|
setmetatable(self, nil)
|
||||||
|
end
|
||||||
|
|
||||||
-- Nouns {{{
|
-- Nouns {{{
|
||||||
function BaseControl:has_noun(noun)
|
function BaseControl:has_noun(noun)
|
||||||
return self.local_nouns[noun] ~= nil or self.remote_nouns[noun] ~= nil
|
return self.local_nouns[noun] ~= nil or self.remote_nouns[noun] ~= nil
|
||||||
|
|
@ -347,6 +365,17 @@ function BaseControl:_network_handler(remote, msg)
|
||||||
for _, verb in ipairs(msg.verbs or {}) do
|
for _, verb in ipairs(msg.verbs or {}) do
|
||||||
self.remote_verbs[verb] = remote
|
self.remote_verbs[verb] = remote
|
||||||
end
|
end
|
||||||
|
elseif msg.ty == Message.Deregister then
|
||||||
|
for _, noun in ipairs(msg.nouns or {}) do
|
||||||
|
if self.remote_nouns[noun] == remote then
|
||||||
|
self.remote_nouns[noun] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
for _, verb in ipairs(msg.verbs or {}) do
|
||||||
|
if self.remote_verbs[verb] == remote then
|
||||||
|
self.remote_verbs[verb] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
elseif msg.ty == Message.NounRequest then
|
elseif msg.ty == Message.NounRequest then
|
||||||
self.network:send(remote, {
|
self.network:send(remote, {
|
||||||
ty=Message.NounResponse,
|
ty=Message.NounResponse,
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,10 @@ function modem.send(addr, port, msg)
|
||||||
network.send(addr, port, msg)
|
network.send(addr, port, msg)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function modem.close(port)
|
||||||
|
-- Nothing
|
||||||
|
end
|
||||||
|
|
||||||
function modem.broadcast(port, msg)
|
function modem.broadcast(port, msg)
|
||||||
network.broadcast(port, msg)
|
network.broadcast(port, msg)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
68
tests/cleanup.lua
Normal file
68
tests/cleanup.lua
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
require("lunit")
|
||||||
|
|
||||||
|
local network = require("network")
|
||||||
|
local serialization = require("serialization")
|
||||||
|
local BaseControl = require("bc")
|
||||||
|
|
||||||
|
module("cleanup", package.seeall, lunit.testcase)
|
||||||
|
|
||||||
|
function test_clean_simple()
|
||||||
|
local bc = BaseControl:new()
|
||||||
|
bc:close()
|
||||||
|
|
||||||
|
local bc = BaseControl:finalize()
|
||||||
|
bc:close()
|
||||||
|
|
||||||
|
assert_error(function()
|
||||||
|
bc:nouns()
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
function test_clean_network()
|
||||||
|
local bc1 = BaseControl:new()
|
||||||
|
local addr1 = network.get_scene()
|
||||||
|
bc1:register("cln1", 123)
|
||||||
|
bc1:register("cln2v", function() end)
|
||||||
|
bc1:finalize()
|
||||||
|
|
||||||
|
local bc2 = BaseControl:new()
|
||||||
|
local addr2 = network.get_scene()
|
||||||
|
|
||||||
|
assert_true(bc2:has_noun("cln1"), "init failed")
|
||||||
|
assert_true(bc2:has_verb("cln2v"), "init failed")
|
||||||
|
|
||||||
|
network.set_scene(addr1)
|
||||||
|
bc1:close()
|
||||||
|
|
||||||
|
network.set_scene(addr2)
|
||||||
|
assert_false(bc2:has_noun("cln1"), "close failed")
|
||||||
|
assert_false(bc2:has_verb("cln2v"), "close failed")
|
||||||
|
end
|
||||||
|
|
||||||
|
function test_clean_network2()
|
||||||
|
local bc1 = BaseControl:new()
|
||||||
|
local addr1 = network.get_scene()
|
||||||
|
local n = 0
|
||||||
|
bc1:register("cln3", 1231)
|
||||||
|
bc1:register("cln4v", function()
|
||||||
|
n = n + 1
|
||||||
|
end)
|
||||||
|
bc1:finalize()
|
||||||
|
|
||||||
|
local bc2 = BaseControl:new()
|
||||||
|
local addr2 = network.get_scene()
|
||||||
|
|
||||||
|
assert_equal(0, n, "verb called too much/little")
|
||||||
|
assert_true(bc2:call("cln4v"), "verb not called")
|
||||||
|
assert_equal(1, n, "verb called too much/little")
|
||||||
|
assert_equal(1231, bc2:get("cln3"), "noun not retrieved correctly")
|
||||||
|
|
||||||
|
network.set_scene(addr1)
|
||||||
|
bc1:close()
|
||||||
|
|
||||||
|
assert_false(bc2:call("cln4v"), "verb called")
|
||||||
|
assert_equal(1, n, "verb called too much/little")
|
||||||
|
assert_error_match("unknown", function()
|
||||||
|
bc2:get("cln3")
|
||||||
|
end)
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue