forked from cloudwu/sproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testrpc.lua
77 lines (64 loc) · 1.79 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
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 {}
blackhole 4 {
request {}
}
]]
local client_proto = sproto.parse [[
.package {
type 0 : integer
session 1 : integer
}
]]
-- 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") -- bar has no response
print("request bar size =", #req)
local type, name, request, response = server:dispatch(req)
assert(type == "REQUEST" and name == "bar" and request == nil and response == nil)
local req = client_request "blackhole"
print("request blackhole size = ", #req)