-
Notifications
You must be signed in to change notification settings - Fork 1
/
autobahn_wclient_test.lua
133 lines (105 loc) · 2.68 KB
/
autobahn_wclient_test.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
local uv = require "lluv"
local websocket = require "websocket"
local Autobahn = require "./autobahn"
local deflate = require "websocket.extensions.permessage-deflate"
local ctx do
local ok, ssl = pcall(require, "lluv.ssl")
if ok then
ctx = assert(ssl.context{
protocol = "tlsv1",
certificate = "./wss/server.crt",
})
end
end
local Client = function() return websocket.client.lluv{
ssl = ctx,
utf8 = true,
extensions = {deflate},
auto_ping_response = true,
}end
local URI = arg[1] or "ws://127.0.0.1:9001"
local reportDir = "./reports/clients"
local agent = string.format("websocket.client.lluv (%s / %s)",
jit and jit.version or _VERSION,
URI:lower():match("^wss:") and "WSS" or "WS"
)
local caseCount = 0
local currentCaseId = 0
local function getCaseCount(cont)
local cli = Client() do
cli:on_open(function() end)
cli:on_error(function(...)
print("WS ERROR:", ...)
end)
cli:on_message(function(ws, msg)
caseCount = tonumber(msg)
ws:close()
end)
cli:on_close(function(ws)
cont()
end)
end
cli:connect(Autobahn.Server.getCaseCount(URI))
end
local function runTestCase(no, cb)
local cli = Client() do
cli:on_open(function()
print("Executing test case " .. no .. "/" .. caseCount)
end)
cli:on_error(function(ws, ...)
assert(cli == ws)
cli = nil
print("WS ERROR:", ...)
end)
cli:on_message(function(ws, message, opcode)
if opcode == websocket.TEXT or opcode == websocket.BINARY then
cli:send(message, opcode)
end
end)
cli:on_close(function(ws, ...)
assert(cli == ws)
cli = nil
cb(...)
end)
end
cli:connect(Autobahn.Server.runTestCase(URI, no, agent))
end
local function updateReports()
local cli = Client() do
cli:on_open(function()
print("Updating reports ...");
end)
cli:on_error(function(...)
print("WS ERROR:", ...)
end)
cli:on_message(function(ws, message, opcode)
print("Report:", message)
end)
cli:on_close(function(ws)
print("Reports updated.");
print("Test suite finished!");
end)
end
cli:connect(Autobahn.Server.updateReports(URI, agent))
end
local function runNextCase()
runTestCase(currentCaseId, function(_, err, code, reason)
currentCaseId = currentCaseId + 1
if currentCaseId <= caseCount then
runNextCase()
else
print("All test cases executed.")
updateReports()
end
end)
end
local function runAll()
currentCaseId = 1
Autobahn.cleanReports(reportDir)
getCaseCount(runNextCase)
uv.run(debug.traceback)
if not Autobahn.verifyReport(reportDir, agent, true) then
return os.exit(-1)
end
end
runAll()