forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesttimeout.lua
52 lines (42 loc) · 1014 Bytes
/
testtimeout.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
local skynet = require "skynet"
local service = require "skynet.service"
local function test_service()
local skynet = require "skynet"
skynet.start(function()
skynet.dispatch("lua", function()
skynet.error("Wait for 1s")
skynet.sleep(100) -- response after 1s for any request
skynet.ret()
end)
end)
end
local function timeout_call(ti, ...)
local token = {}
local ret
skynet.fork(function(...)
ret = table.pack(pcall(skynet.call, ...))
skynet.wakeup(token)
end, ...)
skynet.sleep(ti, token)
if ret then
if ret[1] then
return table.unpack(ret, 1, ret.n)
else
error(ret[2])
end
else
-- timeout
return false
end
end
skynet.start(function()
local test = service.new("testtimeout", test_service)
skynet.error("1", skynet.now())
skynet.call(test, "lua")
skynet.error("2", skynet.now())
skynet.error(timeout_call(50, test, "lua"))
skynet.error("3", skynet.now())
skynet.error(timeout_call(150, test, "lua"))
skynet.error("4", skynet.now())
skynet.exit()
end)