Skip to content

Commit

Permalink
misc: polished the module for initial release.
Browse files Browse the repository at this point in the history
We took care of various minor styling issues and introduced a sanity.t
test suite for the shared lib loading code.
  • Loading branch information
thibaultcha committed Feb 3, 2019
1 parent c914369 commit dc62ea6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
14 changes: 7 additions & 7 deletions lib/resty/signal.lua
Expand Up @@ -4,6 +4,7 @@ local _M = {


local ffi = require "ffi"
local base = require "resty.core.base"


local C = ffi.C
Expand All @@ -12,7 +13,6 @@ local tonumber = tonumber
local assert = assert
local errno = ffi.errno
local type = type
local base = require "resty.core.base"
local new_tab = base.new_tab
local error = error

Expand Down Expand Up @@ -40,6 +40,7 @@ do
io_close(f)
return ffi.load(fpath)
end

tried_paths[i] = fpath
i = i + 1
end
Expand All @@ -51,11 +52,8 @@ end -- do

local resty_signal, tried_paths = load_shared_lib("librestysignal.so")
if not resty_signal then
local len = #tried_paths
tried_paths[len + 1] = 'tried above paths but '
.. 'can not load librestysignal.so'
len = len + 1
error(table.concat(tried_paths, '\r\n', 1, len))
error("could not load librestysignal.so from the following paths:\n" ..
table.concat(tried_paths, "\n"), 2)
end


Expand Down Expand Up @@ -106,6 +104,7 @@ local signals = {
PROF = 27,
WINCH = 28,
IO = 29,
PWR = 30,
}


Expand All @@ -128,10 +127,11 @@ function _M.kill(pid, sig)
end
end

local rc = tonumber(C.kill(assert(pid), signum))
local rc = tonumber(C.kill(assert(pid), signum))
if rc == 0 then
return true
end

local err = ffi_str(C.strerror(errno()))
return nil, err
end
Expand Down
31 changes: 25 additions & 6 deletions t/kill.t
Expand Up @@ -12,7 +12,26 @@ run_tests();

__DATA__
=== TEST 1: send NONE to a nonexistent process
=== TEST 1: returns an error if signal is unknown
--- config
location = /t {
content_by_lua_block {
local resty_signal = require "resty.signal"
local ok, err = resty_signal.kill(pid, "FOO")
if not ok then
ngx.say("failed to send FOO signal: ", err)
return
end
ngx.say("ok")
}
}
--- response_body
failed to send FOO signal: unknown signal name
=== TEST 2: send NONE to a non-existing process
--- config
location = /t {
content_by_lua_block {
Expand All @@ -37,7 +56,7 @@ failed to send NONE signal: No such process
=== TEST 2: send TERM to a nonexistent process
=== TEST 3: send TERM to a non-existing process
--- config
location = /t {
content_by_lua_block {
Expand All @@ -62,7 +81,7 @@ failed to send TERM signal: No such process
=== TEST 3: send NONE to an existent process
=== TEST 4: send NONE to an existing process
--- config
location = /t {
content_by_lua_block {
Expand Down Expand Up @@ -93,7 +112,7 @@ ok
=== TEST 4: send TERM to an existent process
=== TEST 5: send TERM to an existing process
--- config
location = /t {
content_by_lua_block {
Expand Down Expand Up @@ -125,7 +144,7 @@ failed to send TERM signal: No such process
=== TEST 5: send KILL to an existent process
=== TEST 6: send KILL to an existing process
--- config
location = /t {
content_by_lua_block {
Expand Down Expand Up @@ -157,7 +176,7 @@ failed to send KILL signal: No such process
=== TEST 6: send TERM signal value, 15, directly to an existent process
=== TEST 7: send TERM signal value, 15, directly to an existing process
--- config
location = /t {
content_by_lua_block {
Expand Down
35 changes: 35 additions & 0 deletions t/sanity.t
@@ -0,0 +1,35 @@
# vi:ft=

use lib '.';
use t::TestKiller;

plan tests => 3 * blocks();

no_long_string();
#no_diff();

run_tests();

__DATA__
=== TEST 1: failure to load librestysignal.so
--- config
location = /t {
content_by_lua_block {
local cpath = package.cpath
package.cpath = "/foo/?.so;/bar/?.so;"
local ok, perr = pcall(require, "resty.signal")
if not ok then
ngx.say(perr)
end
package.cpath = cpath
}
}
--- response_body
could not load librestysignal.so from the following paths:
/foo/librestysignal.so
/bar/librestysignal.so
--- no_error_log
[error]

0 comments on commit dc62ea6

Please sign in to comment.