-
Notifications
You must be signed in to change notification settings - Fork 253
/
testrpc.lua
101 lines (84 loc) · 2.6 KB
/
testrpc.lua
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
local sproto = require "sproto"
local print_r = require "print_r"
local server_proto = sproto.parse [[
.package {
type 0 : integer
session 1 : integer
}
foobar 1 {
request {
what 0 : string
}
response {
ok 0 : boolean
}
}
foo 2 {
response {
ok 0 : boolean
}
}
bar 3 {
response nil
}
blackhole 4 {
}
]]
local client_proto = sproto.parse [[
.package {
type 0 : integer
session 1 : integer
}
]]
assert(server_proto:exist_type "package")
assert(server_proto:exist_proto "foobar")
print("=== default table")
print_r(server_proto:default("package"))
print_r(server_proto:default("foobar", "REQUEST"))
assert(server_proto:default("foo", "REQUEST")==nil)
assert(server_proto:request_encode("foo")=="")
server_proto:response_encode("foo", { ok = true })
assert(server_proto:request_decode("blackhole")==nil)
assert(server_proto:response_decode("blackhole")==nil)
print("=== test 1")
-- The type package must has two field : type and session
local server = server_proto:host "package"
local client = client_proto:host "package"
local client_request = client:attach(server_proto)
print("client request foobar")
local req = client_request("foobar", { what = "foo" }, 1)
print("request foobar size =", #req)
local type, name, request, response = server:dispatch(req)
assert(type == "REQUEST" and name == "foobar")
print_r(request)
print("server response")
local resp = response { ok = true }
print("response package size =", #resp)
print("client dispatch")
local type, session, response = client:dispatch(resp)
assert(type == "RESPONSE" and session == 1)
print_r(response)
local req = client_request("foo", nil, 2)
print("request foo size =", #req)
local type, name, request, response = server:dispatch(req)
assert(type == "REQUEST" and name == "foo" and request == nil)
local resp = response { ok = false }
print("response package size =", #resp)
print("client dispatch")
local type, session, response = client:dispatch(resp)
assert(type == "RESPONSE" and session == 2)
print_r(response)
local req = client_request("bar", nil, 3)
print("request bar size =", #req)
local type, name, request, response = server:dispatch(req)
assert(type == "REQUEST" and name == "bar" and request == nil)
assert(select(2,client:dispatch(response())) == 3)
local req = client_request "blackhole" -- no response
print("request blackhole size = ", #req)
print("=== test 2")
local v, tag = server_proto:request_encode("foobar", { what = "hello"})
assert(tag == 1) -- foobar : 1
print("tag =", tag)
print_r(server_proto:request_decode("foobar", v))
local v = server_proto:response_encode("foobar", { ok = true })
print_r(server_proto:response_decode("foobar", v))