Skip to content

Commit

Permalink
tests(*) add JIT tests to relevant methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed May 19, 2017
1 parent 26de049 commit ab231ef
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 42 deletions.
7 changes: 1 addition & 6 deletions lib/resty/mlcache/ipc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,11 @@ function _M.new(shm, debug)

local self = {
dict = dict,
pid = worker_pid(),
pid = debug and 0 or worker_pid(),
idx = 0,
callbacks = {},
}

if debug then
self.marshall = marshall
self.unmarshall = unmarshall
end

return setmetatable(self, mt)
end

Expand Down
147 changes: 146 additions & 1 deletion t/02-get.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,32 @@ workers(2);

#repeat_each(2);

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

my $pwd = cwd();

our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_shared_dict cache 1m;
init_by_lua_block {
-- local verbose = true
local verbose = false
local outfile = "$Test::Nginx::Util::ErrLogFile"
-- local outfile = "/tmp/v.log"
if verbose then
local dump = require "jit.dump"
dump.on(nil, outfile)
else
local v = require "jit.v"
v.on(outfile)
end
require "resty.core"
-- jit.opt.start("hotloop=1")
-- jit.opt.start("loopunroll=1000000")
-- jit.off()
}
};

run_tests();
Expand Down Expand Up @@ -1013,3 +1032,129 @@ is not expired in LRU: table: \S+
is stale in LRU: table: \S+
--- no_error_log
[error]



=== TEST 23: get() JITs when hit coming from LRU
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local mlcache = require "resty.mlcache"
local cache = assert(mlcache.new("cache"))
local function cb()
return 123456
end
for i = 1, 10e3 do
local data = assert(cache:get("key", nil, cb))
assert(data == 123456)
end
}
}
--- request
GET /t
--- response_body

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



=== TEST 24: get() JITs when hit coming from shm
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local mlcache = require "resty.mlcache"
local cache = assert(mlcache.new("cache"))
local function cb()
return 123456
end
for i = 1, 10e3 do
local data = assert(cache:get("key", nil, cb))
assert(data == 123456)
cache.lru:delete("key")
end
}
}
--- request
GET /t
--- response_body

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



=== TEST 25: get() JITs when miss coming from LRU
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local mlcache = require "resty.mlcache"
local cache = assert(mlcache.new("cache"))
local function cb()
return nil
end
for i = 1, 10e3 do
local data, err = cache:get("key", nil, cb)
assert(err == nil)
assert(data == nil)
end
}
}
--- request
GET /t
--- response_body

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



=== TEST 26: get() JITs when miss coming from shm
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local mlcache = require "resty.mlcache"
local cache = assert(mlcache.new("cache"))
local function cb()
return nil
end
for i = 1, 10e3 do
local data, err = cache:get("key", nil, cb)
assert(err == nil)
assert(data == nil)
cache.lru:delete("key")
end
}
}
--- request
GET /t
--- response_body

--- error_log eval
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):10 loop\]/
--- no_error_log
[error]
105 changes: 81 additions & 24 deletions t/03-ipc.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Cwd qw(cwd);

workers(1);

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

my $pwd = cwd();

Expand All @@ -15,14 +15,22 @@ our $HttpConfig = qq{
lua_shared_dict ipc 1m;
init_by_lua_block {
-- tamper with shm record and set a different pid
function unset_pid(ipc, idx)
local v = assert(ngx.shared.ipc:get(idx))
if not v then return end
local pid, channel, data = ipc.unmarshall(v)
pid = 0
assert(ngx.shared.ipc:set(idx, ipc.marshall(pid, channel, data)))
-- local verbose = true
local verbose = false
local outfile = "$Test::Nginx::Util::ErrLogFile"
-- local outfile = "/tmp/v.log"
if verbose then
local dump = require "jit.dump"
dump.on(nil, outfile)
else
local v = require "jit.v"
v.on(outfile)
end
require "resty.core"
-- jit.opt.start("hotloop=1")
-- jit.opt.start("loopunroll=1000000")
-- jit.off()
}
};

Expand Down Expand Up @@ -70,8 +78,6 @@ qq{
content_by_lua_block {
assert(ipc:broadcast("my_channel", "hello world"))
unset_pid(ipc, 1)
assert(ipc:poll())
}
}
Expand Down Expand Up @@ -106,8 +112,6 @@ qq{
content_by_lua_block {
assert(ipc:broadcast("my_channel", "hello world"))
unset_pid(ipc, 1)
assert(ipc:poll())
}
}
Expand Down Expand Up @@ -173,10 +177,6 @@ qq{
assert(ipc:broadcast("my_channel", "msg 2"))
assert(ipc:broadcast("my_channel", "msg 3"))
unset_pid(ipc, 1)
unset_pid(ipc, 2)
unset_pid(ipc, 3)
assert(ipc:poll())
}
}
Expand All @@ -201,7 +201,7 @@ qq{
init_worker_by_lua_block {
local mlcache_ipc = require "resty.mlcache.ipc"
ipc = assert(mlcache_ipc.new("ipc", true))
ipc = assert(mlcache_ipc.new("ipc"))
ipc:subscribe("my_channel", function(data)
ngx.log(ngx.NOTICE, "received event from my_channel: ", data)
Expand Down Expand Up @@ -254,8 +254,6 @@ qq{
content_by_lua_block {
assert(ipc:broadcast("my_channel", "hello world"))
unset_pid(ipc, 1)
assert(ipc:poll())
}
}
Expand Down Expand Up @@ -337,10 +335,6 @@ qq{
assert(ipc:broadcast("other_channel", "hello ipc"))
assert(ipc:broadcast("other_channel", "hello ipc 2"))
unset_pid(ipc, 1)
unset_pid(ipc, 2)
unset_pid(ipc, 3)
assert(ipc:poll())
}
}
Expand Down Expand Up @@ -495,7 +489,6 @@ qq{
assert(ipc:broadcast("my_channel", "msg 2"))
assert(ngx.shared.ipc:set(1, false))
unset_pid(ipc, 2)
assert(ipc:poll())
}
Expand Down Expand Up @@ -550,3 +543,67 @@ GET /t
qr/\[warn\] .*? \[ipc\] could not sleep before retry: API disabled in the context of log_by_lua/,
qr/\[error\] .*? \[ipc\] could not get event at index: '1'/,
]



=== TEST 15: poll() JITs
--- http_config eval
qq{
$::HttpConfig
init_worker_by_lua_block {
local mlcache_ipc = require "resty.mlcache.ipc"
ipc = assert(mlcache_ipc.new("ipc", true))
ipc:subscribe("my_channel", function(data)
ngx.log(ngx.NOTICE, "callback from my_channel: ", data)
end)
}
}
--- config
location = /t {
content_by_lua_block {
for i = 1, 10e3 do
assert(ipc:poll())
end
}
}
--- request
GET /t
--- response_body

--- error_log eval
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):2 loop\]/



=== TEST 16: broadcast() JITs
--- http_config eval
qq{
$::HttpConfig
init_worker_by_lua_block {
local mlcache_ipc = require "resty.mlcache.ipc"
ipc = assert(mlcache_ipc.new("ipc", true))
ipc:subscribe("my_channel", function(data)
ngx.log(ngx.NOTICE, "callback from my_channel: ", data)
end)
}
}
--- config
location = /t {
content_by_lua_block {
for i = 1, 10e3 do
assert(ipc:broadcast("my_channel", "hello world"))
end
}
}
--- request
GET /t
--- response_body

--- error_log eval
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):2 loop\]/
Loading

0 comments on commit ab231ef

Please sign in to comment.