Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(active-probes) interval is respected #73

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,10 @@ end
-- @param self the checker object this timer runs on
-- @param health_mode either "healthy" or "unhealthy" to indicate what check
local function checker_callback(self, health_mode)
if self.checker_callback_count then
self.checker_callback_count = self.checker_callback_count + 1
end

local list_to_check = {}
local targets = fetch_target_list(self)
for _, target in ipairs(targets) do
Expand Down Expand Up @@ -1036,7 +1040,6 @@ local function checker_callback(self, health_mode)
expire = function()
self:log(DEBUG, "checking ", health_mode, " targets: #", #list_to_check)
self:active_check_targets(list_to_check)
self.checks.active[health_mode].last_run = ngx_now()
end,
})
if timer == nil then
Expand Down Expand Up @@ -1349,6 +1352,7 @@ function _M.new(opts)

if opts.test then
self.test_get_counter = test_get_counter
self.checker_callback_count = 0
end

assert(self.name, "required option 'name' is missing")
Expand Down Expand Up @@ -1479,13 +1483,15 @@ function _M.new(opts)
(checker_obj.checks.active.healthy.last_run +
checker_obj.checks.active.healthy.interval <= cur_time)
then
checker_obj.checks.active.healthy.last_run = cur_time
checker_callback(checker_obj, "healthy")
end

if checker_obj.checks.active.unhealthy.active and
(checker_obj.checks.active.unhealthy.last_run +
checker_obj.checks.active.unhealthy.interval <= cur_time)
then
checker_obj.checks.active.unhealthy.last_run = cur_time
checker_callback(checker_obj, "unhealthy")
end
end
Expand Down
60 changes: 59 additions & 1 deletion t/09-active_probes.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use Cwd qw(cwd);

workers(1);

plan tests => repeat_each() * 56;
plan tests => repeat_each() * 59;

my $pwd = cwd();

Expand Down Expand Up @@ -462,3 +462,61 @@ event: target status 'example.com(127.0.0.1:2114)' from 'false' to 'true'
checking unhealthy targets: nothing to do



=== TEST 9: active probes, interval is respected
--- http_config eval
qq{
$::HttpConfig

server {
listen 2114;
location = /status {
access_by_lua_block {
ngx.sleep(0.3)
ngx.exit(200)
}
}
}
}
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local checker = healthcheck.new({
test = true,
name = "testing",
shm_name = "test_shm",
type = "http",
checks = {
active = {
http_path = "/status",
healthy = {
interval = 1,
successes = 1,
},
unhealthy = {
interval = 1,
http_failures = 1,
}
},
}
})
ngx.sleep(1) -- active healthchecks might take up to 1s to start
local ok, err = checker:add_target("127.0.0.1", 2114, nil, true)
ngx.sleep(1) -- wait for the check interval
-- checker callback should not be called more than 5 times
if checker.checker_callback_count < 5 then
Copy link
Contributor

@onematchfox onematchfox Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite sure I understand how you came to the number 5. interval is set to 1 second and the code has slept for 2 second so shouldn't this be 2? Or, perhaps in some edge case where the checker ran immediately before the first sleep 3?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could be 2 or 3 or 4, but I went with 5 as a "pessimistic" number

if the intervals are 1 second, we slept for 1 second but the checker ran more than 5 times something badly wrong occurred, which is what's happening in release/1.4.1

ngx.say("OK")
else
ngx.say("BAD")
end
}
}
--- request
GET /t
--- response_body
OK
--- no_error_log
[error]