File: shared.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 (434 lines) | stat: -rw-r--r-- 9,050 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestCore;

repeat_each(2);

plan tests => repeat_each() * blocks() * 4;

our $HttpConfig = <<_EOC_;
    lua_shared_dict dogs 1m;
    $t::TestCore::HttpConfig
_EOC_

no_diff();
no_long_string();
check_accum_error_log();
run_tests();

__DATA__

=== TEST 1: shared.ttl errors on nil key
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local ttl, err = ngx.shared.dogs:ttl()
            if not ttl then
                ngx.say("failed to get ttl: ", err)
            end
        }
    }
--- request
GET /t
--- response_body
failed to get ttl: nil key
--- no_error_log
[error]
[alert]



=== TEST 2: shared.ttl errors on empty key
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local ttl, err = ngx.shared.dogs:ttl("")
            if not ttl then
                ngx.say("failed to get ttl: ", err)
            end
        }
    }
--- request
GET /t
--- response_body
failed to get ttl: empty key
--- no_error_log
[error]
[alert]



=== TEST 3: shared.ttl returns error on not found key
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local ttl, err = ngx.shared.dogs:ttl("key")
            if not ttl then
                ngx.say("failed to get ttl: ", err)
            end
        }
    }
--- request
GET /t
--- response_body
failed to get ttl: not found
--- no_error_log
[error]
[alert]



=== TEST 4: shared.ttl returns key ttl for non-default (positive) ttl
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local ok, err = ngx.shared.dogs:set("key", true, 0.2)

            local ttl, err = ngx.shared.dogs:ttl("key")
            if not ttl then
                ngx.log(ngx.ERR, "failed to get ttl: ", err)
            end

            ngx.say(ttl)

            ngx.say("sleep for 0.1s...")
            ngx.sleep(0.1)

            ttl, err = ngx.shared.dogs:ttl("key")
            if not ttl then
                ngx.log(ngx.ERR, "failed to get ttl: ", err)
            end

            ngx.say(ttl)
        }
    }
--- request
GET /t
--- response_body_like chomp
\A0.2
sleep for 0.1s...
0.\d*
\z
--- no_error_log
[error]
[alert]



=== TEST 5: shared.ttl returns key ttl for non-default (negative) ttl
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local ok, err = ngx.shared.dogs:set("key", true, 0.1)

            local ttl, err = ngx.shared.dogs:ttl("key")
            if not ttl then
                ngx.log(ngx.ERR, "failed to get ttl: ", err)
            end

            ngx.say(ttl)

            ngx.say("sleep for 0.2s...")
            ngx.sleep(0.2)

            ttl, err = ngx.shared.dogs:ttl("key")
            if not ttl then
                ngx.log(ngx.ERR, "failed to get ttl: ", err)
            end

            ngx.say(ttl)
        }
    }
--- request
GET /t
--- response_body_like chomp
\A0.1
sleep for 0.2s...
-0.\d*
\z
--- no_error_log
[error]
[alert]



=== TEST 6: shared.ttl returns key ttl for default ttl (0)
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local ok, err = ngx.shared.dogs:set("key", true)

            local ttl, err = ngx.shared.dogs:ttl("key")
            if not ttl then
                ngx.log(ngx.ERR, "failed to get ttl: ", err)
            end

            ngx.say(ttl)

            ngx.say("sleep for 0.1s...")
            ngx.sleep(0.11)

            ttl, err = ngx.shared.dogs:ttl("key")
            if not ttl then
                ngx.log(ngx.ERR, "failed to get ttl: ", err)
            end

            ngx.say(ttl)
        }
    }
--- request
GET /t
--- response_body
0
sleep for 0.1s...
0
--- no_error_log
[error]
[alert]



=== TEST 7: shared.ttl JIT compiles
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local ok, err = ngx.shared.dogs:set("key", true)

            for i = 1, 30 do
                local ttl, err = ngx.shared.dogs:ttl("key")
                if not ttl then
                    ngx.log(ngx.ERR, "failed to get ttl: ", err)
                end
            end
        }
    }
--- request
GET /t
--- response_body

--- error_log eval
qr/\[TRACE\s+\d+ content_by_lua\(nginx\.conf:\d+\):4 loop\]/
--- no_error_log
[error]



=== TEST 8: shared.expire errors on invalid exptime
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local dogs = ngx.shared.dogs

            local ok, err = pcall(dogs.expire, dogs)
            if not ok then
                ngx.say(err)
            end
        }
    }
--- request
GET /t
--- response_body
bad "exptime" argument
--- no_error_log
[error]
[alert]



=== TEST 9: shared.expire returns error on nil key
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local dogs = ngx.shared.dogs

            local ok, err = dogs:expire(nil, 1)
            if not ok then
                ngx.say("failed to set ttl: ", err)
            end
        }
    }
--- request
GET /t
--- response_body
failed to set ttl: nil key
--- no_error_log
[error]
[alert]



=== TEST 10: shared.expire returns error on empty key
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local dogs = ngx.shared.dogs

            local ok, err = dogs:expire("", 1)
            if not ok then
                ngx.say("failed to set ttl: ", err)
            end
        }
    }
--- request
GET /t
--- response_body
failed to set ttl: empty key
--- no_error_log
[error]
[alert]



=== TEST 11: shared.expire returns error on not found key
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local dogs = ngx.shared.dogs

            local ok, err = dogs:expire("key", 1)
            if not ok then
                ngx.say("failed to set ttl: ", err)
            end
        }
    }
--- request
GET /t
--- response_body
failed to set ttl: not found
--- no_error_log
[error]
[alert]



=== TEST 12: shared.expire updates ttl of key with non-default ttl
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local dogs = ngx.shared.dogs

            local ok, err = dogs:set("key", true, 0.1)
            if not ok then
                ngx.log(ngx.ERR, "failed to set: ", err)
            end

            ok, err = dogs:expire("key", 0.3)
            if not ok then
                ngx.say("failed to set ttl: ", err)
            end

            ngx.sleep(0.2)

            local val, err = dogs:get("key")
            if err then
                ngx.log(ngx.ERR, "failed to get: ", err)
            end

            ngx.say("after 0.2s: ", val)

            ngx.sleep(0.2)

            val, err = dogs:get("key")
            if err then
                ngx.log(ngx.ERR, "failed to get: ", err)
            end

            ngx.say("after 0.4s: ", val)
        }
    }
--- request
GET /t
--- response_body
after 0.2s: true
after 0.4s: nil
--- no_error_log
[error]
[alert]



=== TEST 13: shared.expire updates ttl of key with default ttl (0)
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local dogs = ngx.shared.dogs

            local ok, err = dogs:set("key", true)
            if not ok then
                ngx.log(ngx.ERR, "failed to set: ", err)
            end

            local val, err = dogs:get("key")
            if err then
                ngx.log(ngx.ERR, "failed to get: ", err)
            end

            ngx.say("after set: ", val)

            ok, err = dogs:expire("key", 0.3)
            if not ok then
                ngx.say("failed to set ttl: ", err)
            end

            ngx.sleep(0.4)

            val, err = dogs:get("key")
            if err then
                ngx.log(ngx.ERR, "failed to get: ", err)
            end

            ngx.say("after 0.4s: ", val)
        }
    }
--- request
GET /t
--- response_body
after set: true
after 0.4s: nil
--- no_error_log
[error]
[alert]



=== TEST 14: shared.expire JIT compiles
--- http_config eval: $::HttpConfig
--- config
    location = /t {
        content_by_lua_block {
            local dogs = ngx.shared.dogs

            local ok, err = dogs:set("key", true, 0.1)
            if not ok then
                ngx.log(ngx.ERR, "failed to set: ", err)
            end

            for i = 1, 30 do
                local ok, err = dogs:expire("key", 0.3)
                if not ok then
                    ngx.say("failed to set ttl: ", err)
                end
            end
        }
    }
--- request
GET /t
--- response_body

--- error_log eval
qr/\[TRACE\s+\d+ content_by_lua\(nginx\.conf:\d+\):9 loop\]/
--- no_error_log
[error]