Remove unused test files for lua-resty-lrucache

This commit is contained in:
Théophile Diot 2025-01-16 10:25:44 +01:00
parent ab3013359d
commit 597aa7d2e3
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
18 changed files with 0 additions and 2125 deletions

View file

@ -1,198 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: sanity
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
collectgarbage()
c:set("dog", 32)
c:set("cat", 56)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
c:set("dog", 32)
c:set("cat", 56)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
c:delete("dog")
c:delete("cat")
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
';
}
--- response_body
dog: 32
cat: 56
dog: 32
cat: 56
dog: nil
cat: nil
=== TEST 2: evict existing items
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
if not c then
ngx.say("failed to init lrucace: ", err)
return
end
c:set("dog", 32)
c:set("cat", 56)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
c:set("bird", 76)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
ngx.say("bird: ", (c:get("bird")))
';
}
--- response_body
dog: 32
cat: 56
dog: nil
cat: 56
bird: 76
=== TEST 3: evict existing items (reordered, get should also count)
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
if not c then
ngx.say("failed to init lrucace: ", err)
return
end
c:set("cat", 56)
c:set("dog", 32)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
c:set("bird", 76)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
ngx.say("bird: ", (c:get("bird")))
';
}
--- response_body
dog: 32
cat: 56
dog: nil
cat: 56
bird: 76
=== TEST 4: ttl
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache"
local c = lrucache.new(1)
c:set("dog", 32, 0.5)
ngx.say("dog: ", (c:get("dog")))
ngx.sleep(0.25)
ngx.say("dog: ", (c:get("dog")))
ngx.sleep(0.26)
local v, err = c:get("dog")
ngx.say("dog: ", v, " ", err)
';
}
--- response_body
dog: 32
dog: 32
dog: nil 32
=== TEST 5: ttl
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache"
local lim = 5
local c = lrucache.new(lim)
local n = 1000
for i = 1, n do
c:set("dog" .. i, i)
c:delete("dog" .. i)
c:set("dog" .. i, i)
local cnt = 0
for k, v in pairs(c.hasht) do
cnt = cnt + 1
end
assert(cnt <= lim)
end
for i = 1, n do
local key = "dog" .. math.random(1, n)
c:get(key)
end
for i = 1, n do
local key = "dog" .. math.random(1, n)
c:get(key)
c:set("dog" .. i, i)
local cnt = 0
for k, v in pairs(c.hasht) do
cnt = cnt + 1
end
assert(cnt <= lim)
end
ngx.say("ok")
';
}
--- response_body
ok
--- timeout: 20
=== TEST 6: replace value
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache"
local c = lrucache.new(1)
c:set("dog", 32)
ngx.say("dog: ", (c:get("dog")))
c:set("dog", 33)
ngx.say("dog: ", (c:get("dog")))
';
}
--- response_body
dog: 32
dog: 33

View file

@ -1,32 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: should-store-false
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
collectgarbage()
c:set("false-value", false)
ngx.say("false-value: ", (c:get("false-value")))
c:delete("false-value")
ngx.say("false-value: ", (c:get("false-value")))
';
}
--- response_body
false-value: false
false-value: nil

View file

@ -1,94 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(1);
plan tests => repeat_each() * (blocks() * 2);
no_long_string();
run_tests();
__DATA__
=== TEST 1: sanity
--- http_config eval
"$t::TestLRUCache::HttpConfig"
. qq!
init_by_lua '
local function log(...)
print("[cache] ", ...)
end
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
collectgarbage()
c:set("dog", 32)
c:set("cat", 56)
log("dog: ", (c:get("dog")))
log("cat: ", (c:get("cat")))
c:set("dog", 32)
c:set("cat", 56)
log("dog: ", (c:get("dog")))
log("cat: ", (c:get("cat")))
c:delete("dog")
c:delete("cat")
log("dog: ", (c:get("dog")))
log("cat: ", (c:get("cat")))
';
!
--- config
location = /t {
return 200;
}
--- ignore_response
--- error_log
--- grep_error_log eval: qr/\[cache\] .*? (?:\d+|nil)/
--- grep_error_log_out
[cache] dog: 32
[cache] cat: 56
[cache] dog: 32
[cache] cat: 56
[cache] dog: nil
[cache] cat: nil
=== TEST 2: sanity
--- http_config eval
"$t::TestLRUCache::HttpConfig"
. qq!
init_by_lua '
lrucache = require "resty.lrucache"
flv_index, err = lrucache.new(200)
if not flv_index then
ngx.log(ngx.ERR, "failed to create the cache: ", err)
return
end
flv_meta, err = lrucache.new(200)
if not flv_meta then
ngx.log(ngx.ERR, "failed to create the cache: ", err)
return
end
flv_channel, err = lrucache.new(200)
if not flv_channel then
ngx.log(ngx.ERR, "failed to create the cache: ", err)
return
end
print("3 lrucache initialized.")
';
!
--- config
location = /t {
return 200;
}
--- ignore_response
--- error_log
3 lrucache initialized.

View file

@ -1,152 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: flush_all() deletes all keys (cache partial occupied)
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local N = 4
local c = lrucache.new(N)
for i = 1, N / 2 do
c:set("key " .. i, i)
end
c:flush_all()
for i = 1, N do
local key = "key " .. i
local v = c:get(key)
ngx.say(i, ": ", v)
end
ngx.say("++")
for i = 1, N + 1 do
c:set("key " .. i, i)
end
for i = 1, N + 1 do
ngx.say(i, ": ", (c:get("key " .. i)))
end
}
}
--- response_body
1: nil
2: nil
3: nil
4: nil
++
1: nil
2: 2
3: 3
4: 4
5: 5
=== TEST 2: flush_all() deletes all keys (cache fully occupied)
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local N = 4
local c = lrucache.new(N)
for i = 1, N + 1 do
c:set("key " .. i, i)
end
ngx.say(c:count())
c:flush_all()
ngx.say(c:count())
for i = 1, N + 1 do
local key = "key " .. i
local v = c:get(key)
ngx.say(i, ": ", v)
end
ngx.say("++")
for i = 1, N + 1 do
c:set("key " .. i, i)
end
for i = 1, N + 1 do
ngx.say(i, ": ", (c:get("key " .. i)))
end
}
}
--- response_body
4
0
1: nil
2: nil
3: nil
4: nil
5: nil
++
1: nil
2: 2
3: 3
4: 4
5: 5
=== TEST 3: flush_all() flush empty cache store
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local N = 4
local c = lrucache.new(4)
c:flush_all()
for i = 1, N do
local key = "key " .. i
local v = c:get(key)
ngx.say(i, ": ", v)
end
ngx.say("++")
for i = 1, N + 1 do
c:set("key " .. i, i)
end
for i = 1, N + 1 do
ngx.say(i, ": ", (c:get("key " .. i)))
end
}
}
--- response_body
1: nil
2: nil
3: nil
4: nil
++
1: nil
2: 2
3: 3
4: 4
5: 5

View file

@ -1,25 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: capacity() returns total cache capacity
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
ngx.say("capacity: ", c:capacity())
}
}
--- response_body
capacity: 2

View file

@ -1,53 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: count() returns current cache size
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
ngx.say("count: ", c:count())
c:set("dog", 32)
ngx.say("count: ", c:count())
c:set("dog", 33)
ngx.say("count: ", c:count())
c:set("cat", 33)
ngx.say("count: ", c:count())
c:set("pig", 33)
ngx.say("count: ", c:count())
c:delete("dog")
ngx.say("count: ", c:count())
c:delete("pig")
ngx.say("count: ", c:count())
c:delete("cat")
ngx.say("count: ", c:count())
}
}
--- response_body
count: 0
count: 1
count: 1
count: 2
count: 2
count: 2
count: 1
count: 0

View file

@ -1,234 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: get_keys() with some keys
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(100)
c:set("hello", true)
c:set("world", false)
local keys = c:get_keys()
ngx.say("size: ", #keys)
for i = 1, #keys do
ngx.say(keys[i])
end
}
}
--- response_body
size: 2
world
hello
=== TEST 2: get_keys() with no keys
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(100)
local keys = c:get_keys()
ngx.say("size: ", #keys)
for i = 1, #keys do
ngx.say(keys[i])
end
}
}
--- response_body
size: 0
=== TEST 3: get_keys() with full cache
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(100)
for i = 1, 100 do
c:set("key-" .. i, true)
end
c:set("extra-key", true)
local keys = c:get_keys()
ngx.say("size: ", #keys)
ngx.say("MRU: ", keys[1])
ngx.say("LRU: ", keys[#keys])
}
}
--- response_body
size: 100
MRU: extra-key
LRU: key-2
=== TEST 4: get_keys() max_count = 5
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(100)
for i = 1, 100 do
c:set("key-" .. i, true)
end
local keys = c:get_keys(5)
ngx.say("size: ", #keys)
ngx.say("MRU: ", keys[1])
ngx.say("latest: ", keys[#keys])
}
}
--- response_body
size: 5
MRU: key-100
latest: key-96
=== TEST 5: get_keys() max_count = 0 disables max returns
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(100)
for i = 1, 100 do
c:set("key-" .. i, true)
end
local keys = c:get_keys(0)
ngx.say("size: ", #keys)
ngx.say("MRU: ", keys[1])
ngx.say("LRU: ", keys[#keys])
}
}
--- response_body
size: 100
MRU: key-100
LRU: key-1
=== TEST 6: get_keys() user-fed res table
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c1 = lrucache.new(3)
local c2 = lrucache.new(2)
for i = 1, 3 do
c1:set("c1-" .. i, true)
end
for i = 1, 2 do
c2:set("c2-" .. i, true)
end
local res = {}
local keys_1 = c1:get_keys(0, res)
ngx.say("res is user-fed: ", keys_1 == res)
for _, k in ipairs(keys_1) do
ngx.say(k)
end
res = {}
local keys_2 = c2:get_keys(0, res)
for _, k in ipairs(keys_2) do
ngx.say(k)
end
}
}
--- response_body
res is user-fed: true
c1-3
c1-2
c1-1
c2-2
c2-1
=== TEST 7: get_keys() user-fed res table + max_count
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c1 = lrucache.new(3)
for i = 1, 3 do
c1:set("key-" .. i, true)
end
local res = {}
local keys = c1:get_keys(2, res)
for _, k in ipairs(keys) do
ngx.say(k)
end
}
}
--- response_body
key-3
key-2
=== TEST 8: get_keys() user-fed res table gets a trailing hole
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c1 = lrucache.new(3)
for i = 1, 3 do
c1:set("key-" .. i, true)
end
local res = {}
for i = 1, 10 do
res[i] = true
end
local keys = c1:get_keys(2, res)
for _, k in ipairs(keys) do
ngx.say(k)
end
}
}
--- response_body
key-3
key-2

View file

@ -1,175 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: no user flags by default
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
c:set("dog", 32)
c:set("cat", 56)
local v, err, flags = c:get("dog")
ngx.say("dog: ", v, " ", err, " ", flags)
local v, err, flags = c:get("cat")
ngx.say("cat: ", v, " ", err, " ", flags)
}
}
--- response_body
dog: 32 nil 0
cat: 56 nil 0
=== TEST 2: stores user flags if specified
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
c:set("dog", 32, nil, 0x01)
c:set("cat", 56, nil, 0x02)
local v, err, flags = c:get("dog")
ngx.say("dog: ", v, " ", err, " ", flags)
local v, err, flags = c:get("cat")
ngx.say("cat: ", v, " ", err, " ", flags)
}
}
--- response_body
dog: 32 nil 1
cat: 56 nil 2
=== TEST 3: user flags cannot be negative
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(3)
c:set("dog", 32, nil, 0)
c:set("cat", 56, nil, -1)
local v, err, flags = c:get("dog")
ngx.say("dog: ", v, " ", err, " ", flags)
local v, err, flags = c:get("cat")
ngx.say("cat: ", v, " ", err, " ", flags)
}
}
--- response_body
dog: 32 nil 0
cat: 56 nil 0
=== TEST 4: user flags not number is ignored
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
c:set("dog", 32, nil, "")
local v, err, flags = c:get("dog")
ngx.say(v, " ", err, " ", flags)
}
}
--- response_body
32 nil 0
=== TEST 5: all nodes from double-ended queue have flags
--- config
location = /t {
content_by_lua_block {
local len = 10
local lrucache = require "resty.lrucache"
local c = lrucache.new(len)
for i = 1, len do
c:set(i, 32, nil, 1)
end
for i = 1, len do
local v, _, flags = c:get(i)
if not flags then
ngx.say("item ", i, " does not have flags")
return
end
end
ngx.say("ok")
}
}
--- response_body
ok
=== TEST 6: user flags are preserved when item is stale
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(1)
c:set("dogs", 32, 0.2, 3)
ngx.sleep(0.21)
local v, err, flags = c:get("dogs")
ngx.say(v, " ", err, " ", flags)
}
}
--- response_body
nil 32 3
=== TEST 7: user flags are not preserved upon eviction
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache"
local c = lrucache.new(1)
for i = 1, 10 do
local flags = i % 2 == 0 and i
c:set(i, true, nil, flags)
local v, err, flags = c:get(i)
ngx.say(v, " ", err, " ", flags)
end
}
}
--- response_body
true nil 0
true nil 2
true nil 0
true nil 4
true nil 0
true nil 6
true nil 0
true nil 8
true nil 0
true nil 10

View file

@ -1,307 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: sanity
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
collectgarbage()
c:set("dog", 32)
c:set("cat", 56)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
c:set("dog", 32)
c:set("cat", 56)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
c:delete("dog")
c:delete("cat")
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
';
}
--- response_body
dog: 32
cat: 56
dog: 32
cat: 56
dog: nil
cat: nil
=== TEST 2: evict existing items
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
if not c then
ngx.say("failed to init lrucace: ", err)
return
end
c:set("dog", 32)
c:set("cat", 56)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
c:set("bird", 76)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
ngx.say("bird: ", (c:get("bird")))
';
}
--- response_body
dog: 32
cat: 56
dog: nil
cat: 56
bird: 76
=== TEST 3: evict existing items (reordered, get should also count)
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
if not c then
ngx.say("failed to init lrucace: ", err)
return
end
c:set("cat", 56)
c:set("dog", 32)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
c:set("bird", 76)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
ngx.say("bird: ", (c:get("bird")))
';
}
--- response_body
dog: 32
cat: 56
dog: nil
cat: 56
bird: 76
=== TEST 4: ttl
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(1)
c:set("dog", 32, 0.6)
ngx.say("dog: ", (c:get("dog")))
ngx.sleep(0.3)
ngx.say("dog: ", (c:get("dog")))
ngx.sleep(0.31)
local v, err = c:get("dog")
ngx.say("dog: ", v, " ", err)
';
}
--- response_body
dog: 32
dog: 32
dog: nil 32
=== TEST 5: load factor
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(1, 0.25)
ngx.say(c.bucket_sz)
';
}
--- response_body
4
=== TEST 6: load factor clamped to 0.1
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(3, 0.05)
ngx.say(c.bucket_sz)
';
}
--- response_body
32
=== TEST 7: load factor saturated to 1
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(3, 2.1)
ngx.say(c.bucket_sz)
';
}
--- response_body
4
=== TEST 8: non-string keys
--- config
location = /t {
content_by_lua '
local function log(...)
ngx.say(...)
end
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
collectgarbage()
local tab1 = {1, 2}
local tab2 = {3, 4}
c:set(tab1, 32)
c:set(tab2, 56)
log("tab1: ", (c:get(tab1)))
log("tab2: ", (c:get(tab2)))
c:set(tab1, 32)
c:set(tab2, 56)
log("tab1: ", (c:get(tab1)))
log("tab2: ", (c:get(tab2)))
c:delete(tab1)
c:delete(tab2)
log("tab1: ", (c:get(tab1)))
log("tab2: ", (c:get(tab2)))
';
}
--- response_body
tab1: 32
tab2: 56
tab1: 32
tab2: 56
tab1: nil
tab2: nil
=== TEST 9: replace value
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(1)
c:set("dog", 32)
ngx.say("dog: ", (c:get("dog")))
c:set("dog", 33)
ngx.say("dog: ", (c:get("dog")))
';
}
--- response_body
dog: 32
dog: 33
=== TEST 10: replace value 2
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(1)
c:set("dog", 32, 1.0)
ngx.say("dog: ", (c:get("dog")))
c:set("dog", 33, 0.3)
ngx.say("dog: ", (c:get("dog")))
ngx.sleep(0.4)
local v, err = c:get("dog")
ngx.say("dog: ", v, " ", err)
';
}
--- response_body
dog: 32
dog: 33
dog: nil 33
=== TEST 11: replace value 3 (the old value has longer expire time)
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(1)
c:set("dog", 32, 1.2)
c:set("dog", 33, 0.6)
ngx.sleep(0.2)
ngx.say("dog: ", (c:get("dog")))
ngx.sleep(0.5)
local v, err = c:get("dog")
ngx.say("dog: ", v, " ", err)
';
}
--- response_body
dog: 33
dog: nil 33
=== TEST 12: replace value 4
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(1)
c:set("dog", 32, 0.1)
ngx.sleep(0.2)
c:set("dog", 33)
ngx.sleep(0.2)
ngx.say("dog: ", (c:get("dog")))
';
}
--- response_body
dog: 33

View file

@ -1,32 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: should-store-false
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
collectgarbage()
c:set("false-value", false)
ngx.say("false-value: ", (c:get("false-value")))
c:delete("false-value")
ngx.say("false-value: ", (c:get("false-value")))
';
}
--- response_body
false-value: false
false-value: nil

View file

@ -1,94 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(1);
plan tests => repeat_each() * (blocks() * 2);
no_long_string();
run_tests();
__DATA__
=== TEST 1: sanity
--- http_config eval
"$t::TestLRUCache::HttpConfig"
. qq!
init_by_lua '
local function log(...)
print("[cache] ", ...)
end
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
collectgarbage()
c:set("dog", 32)
c:set("cat", 56)
log("dog: ", (c:get("dog")))
log("cat: ", (c:get("cat")))
c:set("dog", 32)
c:set("cat", 56)
log("dog: ", (c:get("dog")))
log("cat: ", (c:get("cat")))
c:delete("dog")
c:delete("cat")
log("dog: ", (c:get("dog")))
log("cat: ", (c:get("cat")))
';
!
--- config
location = /t {
return 200;
}
--- ignore_response
--- error_log
--- grep_error_log eval: qr/\[cache\] .*? (?:\d+|nil)/
--- grep_error_log_out
[cache] dog: 32
[cache] cat: 56
[cache] dog: 32
[cache] cat: 56
[cache] dog: nil
[cache] cat: nil
=== TEST 2: sanity
--- http_config eval
"$t::TestLRUCache::HttpConfig"
. qq!
init_by_lua '
lrucache = require "resty.lrucache.pureffi"
flv_index, err = lrucache.new(200)
if not flv_index then
ngx.log(ngx.ERR, "failed to create the cache: ", err)
return
end
flv_meta, err = lrucache.new(200)
if not flv_meta then
ngx.log(ngx.ERR, "failed to create the cache: ", err)
return
end
flv_channel, err = lrucache.new(200)
if not flv_channel then
ngx.log(ngx.ERR, "failed to create the cache: ", err)
return
end
print("3 lrucache initialized.")
';
!
--- config
location = /t {
return 200;
}
--- ignore_response
--- error_log
3 lrucache initialized.

View file

@ -1,152 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: flush_all() deletes all keys (cache partial occupied)
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local N = 4
local c = lrucache.new(N)
for i = 1, N / 2 do
c:set("key " .. i, i)
end
c:flush_all()
for i = 1, N do
local key = "key " .. i
local v = c:get(key)
ngx.say(i, ": ", v)
end
ngx.say("++")
for i = 1, N + 1 do
c:set("key " .. i, i)
end
for i = 1, N + 1 do
ngx.say(i, ": ", (c:get("key " .. i)))
end
}
}
--- response_body
1: nil
2: nil
3: nil
4: nil
++
1: nil
2: 2
3: 3
4: 4
5: 5
=== TEST 2: flush_all() deletes all keys (cache fully occupied)
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local N = 4
local c = lrucache.new(N)
for i = 1, N + 1 do
c:set("key " .. i, i)
end
ngx.say(c:count())
c:flush_all()
ngx.say(c:count())
for i = 1, N + 1 do
local key = "key " .. i
local v = c:get(key)
ngx.say(i, ": ", v)
end
ngx.say("++")
for i = 1, N + 1 do
c:set("key " .. i, i)
end
for i = 1, N + 1 do
ngx.say(i, ": ", (c:get("key " .. i)))
end
}
}
--- response_body
4
0
1: nil
2: nil
3: nil
4: nil
5: nil
++
1: nil
2: 2
3: 3
4: 4
5: 5
=== TEST 3: flush_all() flush empty cache store
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local N = 4
local c = lrucache.new(4)
c:flush_all()
for i = 1, N do
local key = "key " .. i
local v = c:get(key)
ngx.say(i, ": ", v)
end
ngx.say("++")
for i = 1, N + 1 do
c:set("key " .. i, i)
end
for i = 1, N + 1 do
ngx.say(i, ": ", (c:get("key " .. i)))
end
}
}
--- response_body
1: nil
2: nil
3: nil
4: nil
++
1: nil
2: 2
3: 3
4: 4
5: 5

View file

@ -1,25 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: capacity() returns total cache capacity
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
ngx.say("capacity: ", c:capacity())
}
}
--- response_body
capacity: 2

View file

@ -1,53 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: count() returns current cache size
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
ngx.say("count: ", c:count())
c:set("dog", 32)
ngx.say("count: ", c:count())
c:set("dog", 33)
ngx.say("count: ", c:count())
c:set("cat", 33)
ngx.say("count: ", c:count())
c:set("pig", 33)
ngx.say("count: ", c:count())
c:delete("dog")
ngx.say("count: ", c:count())
c:delete("pig")
ngx.say("count: ", c:count())
c:delete("cat")
ngx.say("count: ", c:count())
}
}
--- response_body
count: 0
count: 1
count: 1
count: 2
count: 2
count: 2
count: 1
count: 0

View file

@ -1,234 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: get_keys() with some keys
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(100)
c:set("hello", true)
c:set("world", false)
local keys = c:get_keys()
ngx.say("size: ", #keys)
for i = 1, #keys do
ngx.say(keys[i])
end
}
}
--- response_body
size: 2
world
hello
=== TEST 2: get_keys() with no keys
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(100)
local keys = c:get_keys()
ngx.say("size: ", #keys)
for i = 1, #keys do
ngx.say(keys[i])
end
}
}
--- response_body
size: 0
=== TEST 3: get_keys() with full cache
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(100)
for i = 1, 100 do
c:set("key-" .. i, true)
end
c:set("extra-key", true)
local keys = c:get_keys()
ngx.say("size: ", #keys)
ngx.say("MRU: ", keys[1])
ngx.say("LRU: ", keys[#keys])
}
}
--- response_body
size: 100
MRU: extra-key
LRU: key-2
=== TEST 4: get_keys() max_count = 5
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(100)
for i = 1, 100 do
c:set("key-" .. i, true)
end
local keys = c:get_keys(5)
ngx.say("size: ", #keys)
ngx.say("MRU: ", keys[1])
ngx.say("latest: ", keys[#keys])
}
}
--- response_body
size: 5
MRU: key-100
latest: key-96
=== TEST 5: get_keys() max_count = 0 disables max returns
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(100)
for i = 1, 100 do
c:set("key-" .. i, true)
end
local keys = c:get_keys(0)
ngx.say("size: ", #keys)
ngx.say("MRU: ", keys[1])
ngx.say("LRU: ", keys[#keys])
}
}
--- response_body
size: 100
MRU: key-100
LRU: key-1
=== TEST 6: get_keys() user-fed res table
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c1 = lrucache.new(3)
local c2 = lrucache.new(2)
for i = 1, 3 do
c1:set("c1-" .. i, true)
end
for i = 1, 2 do
c2:set("c2-" .. i, true)
end
local res = {}
local keys_1 = c1:get_keys(0, res)
ngx.say("res is user-fed: ", keys_1 == res)
for _, k in ipairs(keys_1) do
ngx.say(k)
end
res = {}
local keys_2 = c2:get_keys(0, res)
for _, k in ipairs(keys_2) do
ngx.say(k)
end
}
}
--- response_body
res is user-fed: true
c1-3
c1-2
c1-1
c2-2
c2-1
=== TEST 7: get_keys() user-fed res table + max_count
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c1 = lrucache.new(3)
for i = 1, 3 do
c1:set("key-" .. i, true)
end
local res = {}
local keys = c1:get_keys(2, res)
for _, k in ipairs(keys) do
ngx.say(k)
end
}
}
--- response_body
key-3
key-2
=== TEST 8: get_keys() user-fed res table gets a trailing hole
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c1 = lrucache.new(3)
for i = 1, 3 do
c1:set("key-" .. i, true)
end
local res = {}
for i = 1, 10 do
res[i] = true
end
local keys = c1:get_keys(2, res)
for _, k in ipairs(keys) do
ngx.say(k)
end
}
}
--- response_body
key-3
key-2

View file

@ -1,175 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: no user flags by default
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
c:set("dog", 32)
c:set("cat", 56)
local v, err, flags = c:get("dog")
ngx.say("dog: ", v, " ", err, " ", flags)
local v, err, flags = c:get("cat")
ngx.say("cat: ", v, " ", err, " ", flags)
}
}
--- response_body
dog: 32 nil 0
cat: 56 nil 0
=== TEST 2: stores user flags if specified
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
c:set("dog", 32, nil, 0x01)
c:set("cat", 56, nil, 0x02)
local v, err, flags = c:get("dog")
ngx.say("dog: ", v, " ", err, " ", flags)
local v, err, flags = c:get("cat")
ngx.say("cat: ", v, " ", err, " ", flags)
}
}
--- response_body
dog: 32 nil 1
cat: 56 nil 2
=== TEST 3: user flags cannot be negative
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(3)
c:set("dog", 32, nil, 0)
c:set("cat", 56, nil, -1)
local v, err, flags = c:get("dog")
ngx.say("dog: ", v, " ", err, " ", flags)
local v, err, flags = c:get("cat")
ngx.say("cat: ", v, " ", err, " ", flags)
}
}
--- response_body
dog: 32 nil 0
cat: 56 nil 0
=== TEST 4: user flags not number is ignored
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(2)
c:set("dog", 32, nil, "")
local v, err, flags = c:get("dog")
ngx.say(v, " ", err, " ", flags)
}
}
--- response_body
32 nil 0
=== TEST 5: all nodes from double-ended queue have flags
--- config
location = /t {
content_by_lua_block {
local len = 10
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(len)
for i = 1, len do
c:set(i, 32, nil, 1)
end
for i = 1, len do
local v, _, flags = c:get(i)
if not flags then
ngx.say("item ", i, " does not have flags")
return
end
end
ngx.say("ok")
}
}
--- response_body
ok
=== TEST 6: user flags are preserved when item is stale
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(1)
c:set("dogs", 32, 0.2, 3)
ngx.sleep(0.21)
local v, err, flags = c:get("dogs")
ngx.say(v, " ", err, " ", flags)
}
}
--- response_body
nil 32 3
=== TEST 7: user flags are not preserved upon eviction
--- config
location = /t {
content_by_lua_block {
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(1)
for i = 1, 10 do
local flags = i % 2 == 0 and i
c:set(i, true, nil, flags)
local v, err, flags = c:get(i)
ngx.say(v, " ", err, " ", flags)
end
}
}
--- response_body
true nil 0
true nil 2
true nil 0
true nil 4
true nil 0
true nil 6
true nil 0
true nil 8
true nil 0
true nil 10

View file

@ -1,52 +0,0 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: sanity
--- config
location = /t {
content_by_lua '
local lrucache = require "resty.lrucache"
local c = lrucache.new(2)
collectgarbage()
c:set("dog", 32)
c:set("cat", 56)
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
local lrucache = require "resty.lrucache.pureffi"
local c2 = lrucache.new(2)
ngx.say("dog: ", (c2:get("dog")))
ngx.say("cat: ", (c2:get("cat")))
c2:set("dog", 9)
c2:set("cat", "hi")
ngx.say("dog: ", (c2:get("dog")))
ngx.say("cat: ", (c2:get("cat")))
ngx.say("dog: ", (c:get("dog")))
ngx.say("cat: ", (c:get("cat")))
';
}
--- response_body
dog: 32
cat: 56
dog: nil
cat: nil
dog: 9
cat: hi
dog: 32
cat: 56

View file

@ -1,38 +0,0 @@
package t::TestLRUCache;
use Test::Nginx::Socket::Lua -Base;
use Cwd qw(cwd);
$ENV{TEST_NGINX_HOTLOOP} ||= 10;
our $pwd = cwd();
our $lua_package_path = './lib/?.lua;;';
our $HttpConfig = <<_EOC_;
lua_package_path '$lua_package_path';
_EOC_
our @EXPORT = qw(
$pwd
$lua_package_path
$HttpConfig
);
add_block_preprocessor(sub {
my $block = shift;
if (!defined $block->http_config) {
$block->set_value("http_config", $HttpConfig);
}
if (!defined $block->request) {
$block->set_value("request", "GET /t");
}
if (!defined $block->no_error_log) {
$block->set_value("no_error_log", "[error]");
}
});
1;