1# vim:set ft= ts=4 sw=4 et fdm=marker:
2
3use Test::Nginx::Socket::Lua;
4
5#worker_connections(1014);
6#master_on();
7#workers(2);
8log_level('debug');
9
10repeat_each(2);
11
12plan tests => repeat_each() * 33;
13
14#no_diff();
15#no_long_string();
16run_tests();
17
18__DATA__
19
20=== TEST 1: sleep 0.5
21--- config
22    location /test {
23        access_by_lua '
24            ngx.update_time()
25            local before = ngx.now()
26            ngx.sleep(0.5)
27            local now = ngx.now()
28            ngx.say(now - before)
29            ngx.exit(200)
30        ';
31    }
32--- request
33GET /test
34--- response_body_like chop
35^0\.(?:4[5-9]\d*|5[0-5]\d*|5)$
36--- error_log
37lua ready to sleep for
38lua sleep timer expired: "/test?"
39
40
41
42=== TEST 2: sleep ag
43--- config
44    location /test {
45        access_by_lua '
46            ngx.update_time()
47            local before = ngx.now()
48            ngx.sleep("a")
49            local now = ngx.now()
50            ngx.say(now - before)
51            ngx.exit(200)
52        ';
53    }
54--- request
55GET /test
56--- error_code: 500
57--- response_body_like: 500 Internal Server Error
58--- error_log
59bad argument #1 to 'sleep'
60
61
62
63=== TEST 3: sleep 0.5 in subrequest
64--- config
65    location /test {
66        access_by_lua '
67            ngx.update_time()
68            local before = ngx.now()
69            ngx.location.capture("/sleep")
70            local now = ngx.now()
71            local delay = now - before
72            ngx.say(delay)
73            ngx.exit(200)
74        ';
75    }
76    location /sleep {
77        content_by_lua 'ngx.sleep(0.5)';
78    }
79--- request
80GET /test
81--- response_body_like chop
82^0\.(?:4[5-9]\d*|5[0-9]\d*|5)$
83--- error_log
84lua ready to sleep for
85lua sleep timer expired: "/sleep?"
86--- no_error_log
87[error]
88
89
90
91=== TEST 4: sleep a in subrequest with bad argument
92--- config
93    location /test {
94        access_by_lua '
95            local res = ngx.location.capture("/sleep");
96            ngx.say(res.status)
97            ngx.exit(200)
98        ';
99    }
100    location /sleep {
101        content_by_lua 'ngx.sleep("a")';
102    }
103--- request
104GET /test
105--- response_body
106500
107--- error_log
108bad argument #1 to 'sleep'
109
110
111
112=== TEST 5: sleep 0.5 - multi-times
113--- config
114    location /test {
115        access_by_lua '
116            ngx.update_time()
117            local start = ngx.now()
118            ngx.sleep(0.3)
119            ngx.sleep(0.3)
120            ngx.sleep(0.3)
121            ngx.say(ngx.now() - start)
122            ngx.exit(200)
123        ';
124    }
125--- request
126GET /test
127--- response_body_like chop
128^0\.(?:8[5-9]\d*|9[0-9]\d*|9)$
129--- error_log
130lua ready to sleep for
131lua sleep timer expired: "/test?"
132--- no_error_log
133[error]
134
135
136
137=== TEST 6: sleep 0.5 - interleaved by ngx.say() - ended by ngx.sleep
138--- config
139    location /test {
140        access_by_lua '
141            ngx.send_headers()
142            -- ngx.location.capture("/sleep")
143            ngx.sleep(1)
144            ngx.say("blah")
145            ngx.sleep(1)
146            -- ngx.location.capture("/sleep")
147            ngx.exit(200)
148        ';
149    }
150    location = /sleep {
151        echo_sleep 0.1;
152    }
153--- request
154GET /test
155--- response_body
156blah
157--- error_log
158lua ready to sleep
159lua sleep timer expired: "/test?"
160--- no_error_log
161[error]
162
163
164
165=== TEST 7: sleep 0.5 - interleaved by ngx.say() - not ended by ngx.sleep
166--- config
167    location /test {
168        access_by_lua '
169            ngx.send_headers()
170            -- ngx.location.capture("/sleep")
171            ngx.sleep(0.3)
172            ngx.say("blah")
173            ngx.sleep(0.5)
174            -- ngx.location.capture("/sleep")
175            ngx.say("hiya")
176            ngx.exit(200)
177        ';
178    }
179    location = /sleep {
180        echo_sleep 0.1;
181    }
182--- request
183GET /test
184--- response_body
185blah
186hiya
187--- error_log
188lua ready to sleep for
189lua sleep timer expired: "/test?"
190--- no_error_log
191[error]
192
193
194
195=== TEST 8: ngx.location.capture before and after ngx.sleep
196--- config
197    location /test {
198        access_by_lua '
199            local res = ngx.location.capture("/sub")
200            ngx.print(res.body)
201
202            ngx.sleep(0.1)
203
204            res = ngx.location.capture("/sub")
205            ngx.print(res.body)
206            ngx.exit(200)
207        ';
208    }
209    location = /hello {
210        echo hello world;
211    }
212    location = /sub {
213        proxy_pass http://127.0.0.1:$server_port/hello;
214    }
215--- request
216GET /test
217--- response_body
218hello world
219hello world
220--- no_error_log
221[error]
222