Skip to content

Commit

Permalink
tests(*): add number of timers check test
Browse files Browse the repository at this point in the history
  • Loading branch information
windmgc committed May 10, 2024
1 parent 7319ffd commit 29f3ec5
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
92 changes: 92 additions & 0 deletions t/with_resty-events/19-timer.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);

workers(1);

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

my $pwd = cwd();
$ENV{TEST_NGINX_SERVROOT} = server_root();

our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_shared_dict test_shm 8m;

init_worker_by_lua_block {
local we = require "resty.events.compat"
assert(we.configure({
unique_timeout = 5,
broker_id = 0,
listening = "unix:$ENV{TEST_NGINX_SERVROOT}/worker_events.sock"
}))
assert(we.configured())
}

server {
server_name kong_worker_events;
listen unix:$ENV{TEST_NGINX_SERVROOT}/worker_events.sock;
access_log off;
location / {
content_by_lua_block {
require("resty.events.compat").run()
}
}
}
};

run_tests();

__DATA__




=== TEST 1: active probes, http node failing
--- http_config eval
qq{
$::HttpConfig

server {
listen 2130;
location = /status {
content_by_lua_block {
ngx.sleep(2)
ngx.exit(500);
}
}
}
}
--- config
location = /t {
content_by_lua_block {
local healthcheck = require("resty.healthcheck")
local checker = healthcheck.new({
name = "testing",
shm_name = "test_shm",
type = "http",
checks = {
active = {
timeout = 1,
http_path = "/status",
healthy = {
interval = 0.1,
successes = 3,
},
unhealthy = {
interval = 0.1,
http_failures = 3,
}
},
}
})
local ok, err = checker:add_target("127.0.0.1", 2130, nil, true)
ngx.sleep(4) -- wait for some time to let the checks run
-- There should be no more than 3 timers running atm, but
-- add a few spaces for worker events
ngx.say(tonumber(ngx.timer.running_count()) <= 5)
}
}
--- request
GET /t
--- response_body
true
72 changes: 72 additions & 0 deletions t/with_worker-events/19-timer.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);

workers(1);

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

my $pwd = cwd();

our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_shared_dict test_shm 8m;
lua_shared_dict my_worker_events 8m;
};

run_tests();

__DATA__



=== TEST 1: active probes, http node failing
--- http_config eval
qq{
$::HttpConfig

server {
listen 2130;
location = /status {
content_by_lua_block {
ngx.sleep(2)
ngx.exit(500);
}
}
}
}
--- 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({
name = "testing",
shm_name = "test_shm",
type = "http",
checks = {
active = {
timeout = 1,
http_path = "/status",
healthy = {
interval = 0.1,
successes = 3,
},
unhealthy = {
interval = 0.1,
http_failures = 3,
}
},
}
})
local ok, err = checker:add_target("127.0.0.1", 2130, nil, true)
ngx.sleep(4) -- wait for some time to let the checks run
-- There should be no more than 3 timers running atm, but
-- add a few spaces for worker events
ngx.say(tonumber(ngx.timer.running_count()) <= 5)
}
}
--- request
GET /t
--- response_body
true

0 comments on commit 29f3ec5

Please sign in to comment.