File: test.lua

package info (click to toggle)
nginx 1.2.1-2.2%2Bwheezy4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 36,644 kB
  • sloc: ansic: 126,205; python: 1,695; sh: 1,018; perl: 523; makefile: 283; php: 122; asm: 48; ruby: 31; cpp: 17
file content (171 lines) | stat: -rw-r--r-- 4,254 bytes parent folder | download
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
print("The test 'framework' used here is still a touch wonky. A test failure need not mean that things are actually broken...")
require "httpest" --fyi, the test "framework" used here is still wonky.
math.randomseed(os.time())
local request=httpest.request
local sendurl, listenurl = "http://localhost:8082/broadcast/pub?channel=%s", "http://localhost:8082/broadcast/sub?channel=%s"
local function send(channel, message, callback)
	assert(request{
		url=sendurl:format(channel),
		method="post",
		data=message,
		headers={['content-type']="text/foo"},
		complete=function(r, status, sock)
			assert(not status, "fail: " .. (status or "?"))
			assert(r.status==201 or r.status==202, tostring(r))
			if callback then
				callback(r, status, sock)
			end
		end
	})
end

local channeltags = {}
local function listen(channel, callback, timeout, headers)
	if not channeltags[channel] then channeltags[channel] = {} end
	local s
	local subscriber_timeout = function()
		return callback(nil, "timeout", s)
	end
	s = request{
		url=listenurl:format(channel),
		method="get",
		headers = headers or {
			['if-none-match']=channeltags[channel]['etag'],
			['if-modified-since']=channeltags[channel]['last-modified']
		},
		complete = function(r, status, s)
			httpest.killtimer(subscriber_timeout)
			if not r then 
				channeltags[channel]=nil
				return callback(nil, status, s)
			end
			channeltags[channel].etag=r:getheader("etag")
			channeltags[channel]['last-modified']=r:getheader("last-modified")
			if callback then
				return callback(r, status, s)
			else
				return channel
			end
		end
	}
	httpest.timer(timeout or 500, subscriber_timeout)
	return s
end

local function batchsend(channel, times, msg, callback, done)
	send(channel, type(msg)=="function" and msg() or msg, function(...)
		if callback then
			callback(...)
		end
		if times>1 then
			return batchsend(channel, times-1, msg, callback, done)
		else
			return done and done()
		end
	end)
end


local function batchlisten(channel, callback, timeout)
	local function subscriber(r, err, s)
		local result
		if r or err then
			result = callback(r, err, s)
		end
		if (not r and not err) or result then
			return listen(channel, subscriber, timeout)
		end
	end
	return subscriber()
end

local function shortmsg(base)
	return (tostring(base)):rep(3)
end

local function testqueuing(channel, done)
	local s, i, messages = nil, 0, {}
	local function subscriber(resp, status, s)
		if resp then
			table.insert(messages, 1, resp:getbody())
		end
		if status=="timeout" then
			httpest.abort_request(s)
			print("  message buffer length is " .. #messages)
			for j, v in ipairs(messages) do
				assert(v==shortmsg(i-j+1), #v .. "	" .. #shortmsg(i-j+1))
			end
			return nil
		end
		return true
	end
	batchsend(channel, 10, 
		function()
			i=i+1
			return shortmsg(i)
		end, 
		nil,
		function()
			return batchlisten(channel, subscriber, 100) 
		end
	)
end

--queuing
for i=1, 5 do
	local channel = math.random()
	httpest.addtest("queuing " .. i .. "(10 messages)", function() testqueuing(channel) end)
end

--deleting
local channel="deltest"
httpest.addtest("delete", function()
	batchsend(channel, 20, "hey", nil, function()
		request{
			url=sendurl:format(channel),
			method='delete',
			complete=function(r,st,s)
				assert(r.status==200, r.status)
				request{
					url=sendurl:format(channel),
					method='delete',
					complete=function(r,st,s)
						assert(r.status==404)
					end
				}
			end
		}
	end)
end)


--broadcasting
local num, reps, observed = 100, 5, 0
for i=0,reps do
	local channel=math.random()
	httpest.addtest(('broadcast to %s (%s)'):format(num, channel), function()
		local msg = math.random() .. "yesyes"
		for j=1, num do
			batchlisten(channel, function(resp, status, sock)
				if resp then
					httpest.abort_request(sock)
					assert(resp:getbody()==msg, "unexpected message: " .. resp:getbody() .. " (expected " .. msg)
					observed = observed + 1
					print(observed)
					return nil
				end
				return true
			end)
		end
		httpest.timer(2000, function()
			send(channel, msg)
		end)
	end)
end
httpest.timer(5000*reps, function()
	local total = reps*num

	assert(responses==total, ("expected %d responses, got %d"):format(total, observed))
end)

httpest.run()