File: balancer-timeout.t

package info (click to toggle)
lua-resty-core 0.1.31-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,176 kB
  • sloc: perl: 143; sh: 59; makefile: 26
file content (341 lines) | stat: -rw-r--r-- 8,397 bytes parent folder | download | duplicates (2)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# vim:set ft= ts=4 sw=4 et fdm=marker:

BEGIN {
    if (!defined $ENV{LD_PRELOAD}) {
        $ENV{LD_PRELOAD} = '';
    }

    if ($ENV{LD_PRELOAD} !~ /\bmockeagain\.so\b/) {
        $ENV{LD_PRELOAD} = "mockeagain.so $ENV{LD_PRELOAD}";
    }

    if (defined $ENV{MOCKEAGAIN} && $ENV{MOCKEAGAIN} eq 'r') {
        $ENV{MOCKEAGAIN} = 'rw';

    } else {
        $ENV{MOCKEAGAIN} = 'w';
    }

    $ENV{TEST_NGINX_EVENT_TYPE} = 'poll';
    $ENV{TEST_NGINX_POSTPONE_OUTPUT} = 1;
}

use lib '.';
use t::TestCore::Stream;

#worker_connections(1014);
#master_on();
#workers(2);
#log_level('warn');

repeat_each(2);

plan tests => repeat_each() * (blocks() * 3 + 5);

$ENV{TEST_NGINX_LUA_PACKAGE_PATH} = "$t::TestCore::Stream::lua_package_path";

#worker_connections(1024);
#no_diff();
no_long_string();
run_tests();

__DATA__

=== TEST 1: set_timeouts
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";

    server {
        listen $TEST_NGINX_RAND_PORT_1;
        return "fake origin\n";
    }

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local b = require "ngx.balancer"
            assert(b.set_timeouts(1.234, 5.678, 7.689))
            assert(b.set_current_peer("127.0.0.1", tonumber($TEST_NGINX_RAND_PORT_1)))
        }
    }
--- stream_server_config
    proxy_pass backend;
--- response_body
fake origin
--- grep_error_log eval: qr/event timer add: \d+: (?:1234|5678|7689):/
--- grep_error_log_out eval
qr/\Aevent timer add: \d+: 1234:
event timer add: \d+: 7689:
\z/
--- no_error_log
[warn]



=== TEST 2: set_timeouts (nil connect timeout)
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
    proxy_connect_timeout 1234ms;

    server {
        listen $TEST_NGINX_RAND_PORT_1;
        return "fake origin\n";
    }

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local b = require "ngx.balancer"
            assert(b.set_timeouts(nil, 5.678, 7.689))
            assert(b.set_current_peer("127.0.0.1", tonumber($TEST_NGINX_RAND_PORT_1)))
        }
    }
--- stream_server_config
        proxy_pass backend;
--- stream_response
fake origin
--- grep_error_log eval: qr/event timer add: \d+: (?:1234|5678|7689):/
--- grep_error_log_out eval
qr/\Aevent timer add: \d+: 1234:
event timer add: \d+: 7689:
\z/
--- no_error_log
[warn]



=== TEST 3: set_timeouts (nil send timeout)
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
    proxy_timeout 5678ms;

    server {
        listen $TEST_NGINX_RAND_PORT_1;
        return "fake origin\n";
    }

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local b = require "ngx.balancer"
            assert(b.set_timeouts(1.234, nil, 7.689))
            assert(b.set_current_peer("127.0.0.1", tonumber($TEST_NGINX_RAND_PORT_1)))
        }
    }
--- stream_server_config
        proxy_pass backend;
--- stream_response
fake origin
--- grep_error_log eval: qr/event timer add: \d+: (?:1234|5678|7689):/
--- grep_error_log_out eval
qr/\Aevent timer add: \d+: 1234:
event timer add: \d+: 7689:
\z/
--- no_error_log
[warn]



=== TEST 4: set_timeouts (nil read timeout)
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
    proxy_timeout 7689ms;

    server {
        listen $TEST_NGINX_RAND_PORT_1;
        return "fake origin\n";
    }

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local b = require "ngx.balancer"
            assert(b.set_timeouts(1.234, 5.678, nil))
            assert(b.set_current_peer("127.0.0.1", tonumber($TEST_NGINX_RAND_PORT_1)))
        }
    }
--- stream_server_config
        proxy_pass backend;
--- stream_response
fake origin
--- grep_error_log eval: qr/event timer add: \d+: (?:1234|5678|7689):/
--- grep_error_log_out eval
qr/\Aevent timer add: \d+: 1234:
event timer add: \d+: 5678:
\z/
--- no_error_log
[warn]



=== TEST 5: set connect timeout to 0
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local b = require "ngx.balancer"
            assert(b.set_timeouts(0, 1.234, 5.678))
            assert(b.set_current_peer("127.0.0.1", tonumber($TEST_NGINX_RAND_PORT_1)))
        }
    }
--- stream_server_config
    proxy_pass backend;
--- error_log eval
qr/\[error\] .*? balancer_by_lua:3: bad connect timeout/
--- no_error_log
[warn]



=== TEST 6: set connect timeout to -1
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local b = require "ngx.balancer"
            assert(b.set_timeouts(-1, 1.234, 5.678))
            assert(b.set_current_peer("127.0.0.1", tonumber($TEST_NGINX_RAND_PORT_1)))
        }
    }
--- stream_server_config
        proxy_pass backend;
--- error_log eval
qr/\[error\] .*? balancer_by_lua:3: bad connect timeout/
--- no_error_log
[warn]



=== TEST 7: set send timeout to 0
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local b = require "ngx.balancer"
            assert(b.set_timeouts(1.234, 0, 5.678))
            assert(b.set_current_peer("127.0.0.1", tonumber($TEST_NGINX_RAND_PORT_1)))
        }
    }
--- stream_server_config
        proxy_pass backend;
--- error_log eval
qr/\[error\] .*? balancer_by_lua:3: bad send timeout/
--- no_error_log
[warn]



=== TEST 8: set send timeout to -1
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local b = require "ngx.balancer"
            assert(b.set_timeouts(1.234, -1, 5.678))
            assert(b.set_current_peer("127.0.0.1", tonumber($TEST_NGINX_RAND_PORT_1)))
        }
    }
--- stream_server_config
        proxy_pass backend;
--- error_log eval
qr/\[error\] .*? balancer_by_lua:3: bad send timeout/
--- no_error_log
[warn]



=== TEST 9: set read timeout to 0
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local b = require "ngx.balancer"
            assert(b.set_timeouts(1.234, 4.567, 0))
            assert(b.set_current_peer("127.0.0.1", tonumber($TEST_NGINX_RAND_PORT_1)))
        }
    }
--- stream_server_config
        proxy_pass backend;
--- error_log eval
qr/\[error\] .*? balancer_by_lua:3: bad read timeout/
--- no_error_log
[warn]



=== TEST 10 set read timeout to -1
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local b = require "ngx.balancer"
            assert(b.set_timeouts(1.234, 4.567, -1))
            assert(b.set_current_peer("127.0.0.1", tonumber($TEST_NGINX_RAND_PORT_1)))
        }
    }
--- stream_server_config
        proxy_pass backend;
--- error_log eval
qr/\[error\] .*? balancer_by_lua:3: bad read timeout/
--- no_error_log
[warn]



=== TEST 11: set_timeouts called in a wrong context
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";

--- stream_server_config

       content_by_lua_block {
            local balancer = require "ngx.balancer"
            local ok, err = balancer.set_timeouts(1, 1, 1)
            if not ok then
                ngx.say("failed to call: ", err)
                return
            end
            ngx.say("unexpected success!")
        }

--- stream_response
failed to call: API disabled in the current context
--- no_error_log
[error]
[alert]



=== TEST 12: set_timeouts called with a non-numerical parameter
--- stream_config
    lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";

    upstream backend {
        server 0.0.0.1:1234;
        balancer_by_lua_block {
            local balancer = require "ngx.balancer"
            local ok, err = balancer.set_timeouts("1.234", 1, 1)
            if not ok then
                ngx.log(ngx.ERR, "failed to call: ", err)
            end
        }
    }

--- stream_server_config
        proxy_pass backend;
--- error_log eval
qr/\[error\] .*? bad connect timeout/
--- no_error_log
[alert]