1
print("The test 'framework' used here is still a touch wonky. A test failure need not mean that things are actually broken...")
2
require "httpest" --fyi, the test "framework" used here is still wonky.
3
math.randomseed(os.time())
4
local request=httpest.request
5
local sendurl, listenurl = "http://localhost:8082/broadcast/pub?channel=%s", "http://localhost:8082/broadcast/sub?channel=%s"
6
local function send(channel, message, callback)
8
url=sendurl:format(channel),
11
headers={['content-type']="text/foo"},
12
complete=function(r, status, sock)
13
assert(not status, "fail: " .. (status or "?"))
14
assert(r.status==201 or r.status==202, tostring(r))
16
callback(r, status, sock)
22
local channeltags = {}
23
local function listen(channel, callback, timeout, headers)
24
if not channeltags[channel] then channeltags[channel] = {} end
26
local subscriber_timeout = function()
27
return callback(nil, "timeout", s)
30
url=listenurl:format(channel),
32
headers = headers or {
33
['if-none-match']=channeltags[channel]['etag'],
34
['if-modified-since']=channeltags[channel]['last-modified']
36
complete = function(r, status, s)
37
httpest.killtimer(subscriber_timeout)
39
channeltags[channel]=nil
40
return callback(nil, status, s)
42
channeltags[channel].etag=r:getheader("etag")
43
channeltags[channel]['last-modified']=r:getheader("last-modified")
45
return callback(r, status, s)
51
httpest.timer(timeout or 500, subscriber_timeout)
55
local function batchsend(channel, times, msg, callback, done)
56
send(channel, type(msg)=="function" and msg() or msg, function(...)
61
return batchsend(channel, times-1, msg, callback, done)
63
return done and done()
69
local function batchlisten(channel, callback, timeout)
70
local function subscriber(r, err, s)
73
result = callback(r, err, s)
75
if (not r and not err) or result then
76
return listen(channel, subscriber, timeout)
82
local function shortmsg(base)
83
return (tostring(base)):rep(3)
86
local function testqueuing(channel, done)
87
local s, i, messages = nil, 0, {}
88
local function subscriber(resp, status, s)
90
table.insert(messages, 1, resp:getbody())
92
if status=="timeout" then
93
httpest.abort_request(s)
94
print(" message buffer length is " .. #messages)
95
for j, v in ipairs(messages) do
96
assert(v==shortmsg(i-j+1), #v .. " " .. #shortmsg(i-j+1))
102
batchsend(channel, 10,
109
return batchlisten(channel, subscriber, 100)
116
local channel = math.random()
117
httpest.addtest("queuing " .. i .. "(10 messages)", function() testqueuing(channel) end)
121
local channel="deltest"
122
httpest.addtest("delete", function()
123
batchsend(channel, 20, "hey", nil, function()
125
url=sendurl:format(channel),
127
complete=function(r,st,s)
128
assert(r.status==200, r.status)
130
url=sendurl:format(channel),
132
complete=function(r,st,s)
133
assert(r.status==404)
143
local num, reps, observed = 100, 5, 0
145
local channel=math.random()
146
httpest.addtest(('broadcast to %s (%s)'):format(num, channel), function()
147
local msg = math.random() .. "yesyes"
149
batchlisten(channel, function(resp, status, sock)
151
httpest.abort_request(sock)
152
assert(resp:getbody()==msg, "unexpected message: " .. resp:getbody() .. " (expected " .. msg)
153
observed = observed + 1
160
httpest.timer(2000, function()
165
httpest.timer(5000*reps, function()
166
local total = reps*num
168
assert(responses==total, ("expected %d responses, got %d"):format(total, observed))
b'\\ No newline at end of file'