forked from Kong/lua-resty-healthcheck
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(hostname) use IP if hostname is not provided Kong#48
- Loading branch information
Showing
3 changed files
with
142 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use Test::Nginx::Socket::Lua; | ||
use Cwd qw(cwd); | ||
|
||
workers(1); | ||
|
||
plan tests => repeat_each() * 4; | ||
|
||
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: add_target() without hostname, remove_target() with same ip:port | ||
--- http_config eval | ||
qq{ | ||
$::HttpConfig | ||
|
||
server { | ||
listen 2112; | ||
location = /status { | ||
return 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({ | ||
name = "testing", | ||
shm_name = "test_shm", | ||
checks = { | ||
active = { | ||
http_path = "/status", | ||
healthy = { | ||
interval = 0.1 | ||
}, | ||
unhealthy = { | ||
interval = 0.1 | ||
} | ||
} | ||
} | ||
}) | ||
ngx.sleep(0.2) -- wait twice the interval | ||
local ok, err = checker:add_target("127.0.0.1", 2112) | ||
ngx.say(ok) | ||
ngx.sleep(0.2) -- wait twice the interval | ||
ok, err = checker:remove_target("127.0.0.1", 2112) | ||
ngx.sleep(0.2) -- wait twice the interval | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
true | ||
|
||
=== TEST 2: add_target() with hostname, remove_target() on same target | ||
--- http_config eval | ||
qq{ | ||
$::HttpConfig | ||
|
||
server { | ||
listen 2112; | ||
location = /status { | ||
return 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({ | ||
name = "testing", | ||
shm_name = "test_shm", | ||
checks = { | ||
active = { | ||
http_path = "/status", | ||
healthy = { | ||
interval = 0.1 | ||
}, | ||
unhealthy = { | ||
interval = 0.1 | ||
} | ||
} | ||
} | ||
}) | ||
ngx.sleep(0.2) -- wait twice the interval | ||
local ok, err = checker:add_target("127.0.0.1", 2112, "localhost") | ||
ngx.say(ok) | ||
ngx.sleep(0.2) -- wait twice the interval | ||
ok, err = checker:remove_target("127.0.0.1", 2112, "localhost") | ||
ngx.sleep(0.2) -- wait twice the interval | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
true | ||
|