public
Description: Lua-RPC for Lua 5.1.x
Homepage:
Clone URL: git://github.com/jsnyder/luarpc.git
luarpc / test-client.lua
100644 63 lines (43 sloc) 1.821 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require("luarpc")
 
function error_handler (message)
io.write ("Err: " .. message .. "\n");
end
 
rpc.on_error (error_handler);
 
if rpc.mode == "tcpip" then
slave, err = rpc.connect ("localhost",12345);
elseif rpc.mode == "serial" then
slave, err = rpc.connect ("/dev/ttys0");
end
 
-- Local Dataset
 
tab = {a=1.4142, b=2};
 
test_local = {1, 2, 3, 3.143, "234"}
test_local.sval = 23
 
function squareval(x) return x^2 end
 
 
--
-- BEGIN TESTS
--
 
-- check that our connection exists
assert( slave, "connection failed" )
 
-- reflect parameters off mirror
assert(slave.mirror(42) == 42, "integer return failed")
-- print(slave.mirror("012345678901234")) -- why the heck does this fail for things of length 15 (16 w/ null)?
-- assert(slave.mirror("this is a test!") == "this is a test!", "string return failed")
-- print(slave.mirror(squareval))
-- assert(string.dump(slave.mirror(squareval)) == string.dump(squareval), "function return failed")
assert(slave.mirror(true) == true, "function return failed")
 
-- basic remote call with returned data
assert( slave.foo1 (123,3.14159,"hello") == 456, "basic call and return failed" )
 
-- execute function remotely
assert(slave.execfunc( string.dump(squareval), 8 ) == 64, "couldn't serialize and execute dumped function")
 
-- get remote table
assert(slave.test:get(), "couldn't get remote table")
 
 
-- check that we can get entry on remote table
assert(test_local.sval == slave.test:get().sval, "table field not equivalent")
 
slave.yarg.blurg = 23
assert(slave.yarg.blurg:get() == 23, "not equal")
 
-- function assigment
slave.squareval = squareval
assert(type(slave.squareval) == "userdata", "function assigment failed")
 
-- remote execution of assigned function
assert(slave.squareval(99) == squareval(99), "remote setting and evaluation of function failed")
 
rpc.close (slave);