~ubuntu-branches/ubuntu/oneiric/nginx/oneiric-updates

« back to all changes in this revision

Viewing changes to debian/modules/nginx-http-push/tests/test.lua

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2011-04-16 13:47:58 UTC
  • mfrom: (4.2.31 sid)
  • Revision ID: james.westby@ubuntu.com-20110416134758-yqca2qp5crh2hw2f
Tags: 1.0.0-2
* debian/rules:
  + Removed --with-file-aio support. Fixed FTBFS on kFreeBSD-* arch
    (Closes: #621882)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
7
        assert(request{
 
8
                url=sendurl:format(channel),
 
9
                method="post",
 
10
                data=message,
 
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))
 
15
                        if callback then
 
16
                                callback(r, status, sock)
 
17
                        end
 
18
                end
 
19
        })
 
20
end
 
21
 
 
22
local channeltags = {}
 
23
local function listen(channel, callback, timeout, headers)
 
24
        if not channeltags[channel] then channeltags[channel] = {} end
 
25
        local s
 
26
        local subscriber_timeout = function()
 
27
                return callback(nil, "timeout", s)
 
28
        end
 
29
        s = request{
 
30
                url=listenurl:format(channel),
 
31
                method="get",
 
32
                headers = headers or {
 
33
                        ['if-none-match']=channeltags[channel]['etag'],
 
34
                        ['if-modified-since']=channeltags[channel]['last-modified']
 
35
                },
 
36
                complete = function(r, status, s)
 
37
                        httpest.killtimer(subscriber_timeout)
 
38
                        if not r then 
 
39
                                channeltags[channel]=nil
 
40
                                return callback(nil, status, s)
 
41
                        end
 
42
                        channeltags[channel].etag=r:getheader("etag")
 
43
                        channeltags[channel]['last-modified']=r:getheader("last-modified")
 
44
                        if callback then
 
45
                                return callback(r, status, s)
 
46
                        else
 
47
                                return channel
 
48
                        end
 
49
                end
 
50
        }
 
51
        httpest.timer(timeout or 500, subscriber_timeout)
 
52
        return s
 
53
end
 
54
 
 
55
local function batchsend(channel, times, msg, callback, done)
 
56
        send(channel, type(msg)=="function" and msg() or msg, function(...)
 
57
                if callback then
 
58
                        callback(...)
 
59
                end
 
60
                if times>1 then
 
61
                        return batchsend(channel, times-1, msg, callback, done)
 
62
                else
 
63
                        return done and done()
 
64
                end
 
65
        end)
 
66
end
 
67
 
 
68
 
 
69
local function batchlisten(channel, callback, timeout)
 
70
        local function subscriber(r, err, s)
 
71
                local result
 
72
                if r or err then
 
73
                        result = callback(r, err, s)
 
74
                end
 
75
                if (not r and not err) or result then
 
76
                        return listen(channel, subscriber, timeout)
 
77
                end
 
78
        end
 
79
        return subscriber()
 
80
end
 
81
 
 
82
local function shortmsg(base)
 
83
        return (tostring(base)):rep(3)
 
84
end
 
85
 
 
86
local function testqueuing(channel, done)
 
87
        local s, i, messages = nil, 0, {}
 
88
        local function subscriber(resp, status, s)
 
89
                if resp then
 
90
                        table.insert(messages, 1, resp:getbody())
 
91
                end
 
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))
 
97
                        end
 
98
                        return nil
 
99
                end
 
100
                return true
 
101
        end
 
102
        batchsend(channel, 10, 
 
103
                function()
 
104
                        i=i+1
 
105
                        return shortmsg(i)
 
106
                end, 
 
107
                nil,
 
108
                function()
 
109
                        return batchlisten(channel, subscriber, 100) 
 
110
                end
 
111
        )
 
112
end
 
113
 
 
114
--queuing
 
115
for i=1, 5 do
 
116
        local channel = math.random()
 
117
        httpest.addtest("queuing " .. i .. "(10 messages)", function() testqueuing(channel) end)
 
118
end
 
119
 
 
120
--deleting
 
121
local channel="deltest"
 
122
httpest.addtest("delete", function()
 
123
        batchsend(channel, 20, "hey", nil, function()
 
124
                request{
 
125
                        url=sendurl:format(channel),
 
126
                        method='delete',
 
127
                        complete=function(r,st,s)
 
128
                                assert(r.status==200, r.status)
 
129
                                request{
 
130
                                        url=sendurl:format(channel),
 
131
                                        method='delete',
 
132
                                        complete=function(r,st,s)
 
133
                                                assert(r.status==404)
 
134
                                        end
 
135
                                }
 
136
                        end
 
137
                }
 
138
        end)
 
139
end)
 
140
 
 
141
 
 
142
--broadcasting
 
143
local num, reps, observed = 100, 5, 0
 
144
for i=0,reps do
 
145
        local channel=math.random()
 
146
        httpest.addtest(('broadcast to %s (%s)'):format(num, channel), function()
 
147
                local msg = math.random() .. "yesyes"
 
148
                for j=1, num do
 
149
                        batchlisten(channel, function(resp, status, sock)
 
150
                                if resp then
 
151
                                        httpest.abort_request(sock)
 
152
                                        assert(resp:getbody()==msg, "unexpected message: " .. resp:getbody() .. " (expected " .. msg)
 
153
                                        observed = observed + 1
 
154
                                        print(observed)
 
155
                                        return nil
 
156
                                end
 
157
                                return true
 
158
                        end)
 
159
                end
 
160
                httpest.timer(2000, function()
 
161
                        send(channel, msg)
 
162
                end)
 
163
        end)
 
164
end
 
165
httpest.timer(5000*reps, function()
 
166
        local total = reps*num
 
167
 
 
168
        assert(responses==total, ("expected %d responses, got %d"):format(total, observed))
 
169
end)
 
170
 
 
171
httpest.run()
 
 
b'\\ No newline at end of file'