You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.1 KiB

require("lunit")
local network = require("network")
local serialization = require("serialization")
local BaseControl = require("bc")
module("network", package.seeall, lunit.testcase)
function test_multinode()
local bc1 = BaseControl:finalize()
local bc2 = BaseControl:new()
bc2:register("multinode1", 123)
bc2:register("multinode2v", function() end)
bc2:finalize()
local bc3 = BaseControl:finalize()
assert_true(bc1:has_noun("multinode1"), "d1: remote noun missing")
assert_true(bc1:has_verb("multinode2v"), "d1: remote verb missing")
assert_true(bc3:has_noun("multinode1"), "d2: remote noun missing")
assert_true(bc3:has_verb("multinode2v"), "d2: remote verb missing")
end
function test_get_network()
local bc1 = BaseControl:new()
bc1:register("getnw1", 123)
bc1:finalize()
local bc2 = BaseControl:new()
assert_equal(123, bc2:get("getnw1"), "wrong value")
assert_error_match("unknown", function()
bc2:unknown("getnw2")
end)
end
function test_call_network()
local bc1 = BaseControl:new()
local tmp, n = 0, 0
bc1:register("callnw1", function(param, p2)
assert_equal(p2, 1234)
tmp = param
n = n + 1
end)
bc1:finalize()
local bc2 = BaseControl:new()
bc2:call("callnw1", 4321, 1234)
assert_equal(4321, tmp, "call incomplete")
assert_equal(1, n, "call not correct")
end
function test_call_sync()
local bc1 = BaseControl:new()
local n = 0
bc1:register("sync1", function(p1, p2)
assert_equal(p2, 12)
n = n + 1
return p1 + 10, "hello"
end)
bc1:finalize()
-- Test locally
assert_nil(bc1:call("sync1", 20, 12), "call not nil")
local ret, val = bc1:call_sync("sync1", 30, 12)
assert_equal(40, ret, "local sync not correct")
assert_equal("hello", val, "local sync not multiple")
assert_equal(2, n, "wrong invokation number")
-- Test remote
local bc2 = BaseControl:new()
assert_nil(bc2:call("sync1", 20, 12), "call not nil")
local ret, val = bc2:call_sync("sync1", 30, 12)
assert_equal(40, ret, "local sync not correct")
assert_equal("hello", val, "local sync not multiple")
assert_equal(4, n, "wrong invokation number")
end