|
| 1 | +use Test::Nginx::Socket::Lua; |
| 2 | +use Cwd qw(cwd); |
| 3 | + |
| 4 | +workers(1); |
| 5 | + |
| 6 | +plan tests => repeat_each() * blocks() * 2; |
| 7 | + |
| 8 | +my $pwd = cwd(); |
| 9 | + |
| 10 | +our $HttpConfig = qq{ |
| 11 | + lua_package_path "$pwd/lib/?.lua;;"; |
| 12 | + lua_shared_dict test_shm 8m; |
| 13 | + lua_shared_dict my_worker_events 8m; |
| 14 | +}; |
| 15 | + |
| 16 | +run_tests(); |
| 17 | + |
| 18 | +__DATA__ |
| 19 | + |
| 20 | +=== TEST 1: set_all_target_statuses_for_hostname() updates statuses |
| 21 | +--- http_config eval |
| 22 | +qq{ |
| 23 | + $::HttpConfig |
| 24 | +} |
| 25 | +--- config |
| 26 | + location = /t { |
| 27 | + content_by_lua_block { |
| 28 | + local we = require "resty.worker.events" |
| 29 | + assert(we.configure{ shm = "my_worker_events", interval = 0.1 }) |
| 30 | + local healthcheck = require("resty.healthcheck") |
| 31 | + local checker = healthcheck.new({ |
| 32 | + name = "testing", |
| 33 | + shm_name = "test_shm", |
| 34 | + }) |
| 35 | + ngx.sleep(0.1) -- wait for initial timers to run once |
| 36 | + checker:add_target("127.0.0.1", 2112, "rush", true) |
| 37 | + checker:add_target("127.0.0.2", 2112, "rush", true) |
| 38 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true |
| 39 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true |
| 40 | + checker:set_all_target_statuses_for_hostname("rush", 2112, false) |
| 41 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- false |
| 42 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- false |
| 43 | + checker:set_all_target_statuses_for_hostname("rush", 2112, true) |
| 44 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true |
| 45 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true |
| 46 | + } |
| 47 | + } |
| 48 | +--- request |
| 49 | +GET /t |
| 50 | +--- response_body |
| 51 | +true |
| 52 | +true |
| 53 | +false |
| 54 | +false |
| 55 | +true |
| 56 | +true |
| 57 | +=== TEST 2: set_all_target_statuses_for_hostname() restores node after passive check disables it |
| 58 | +--- http_config eval |
| 59 | +qq{ |
| 60 | + $::HttpConfig |
| 61 | +} |
| 62 | +--- config |
| 63 | + location = /t { |
| 64 | + content_by_lua_block { |
| 65 | + local we = require "resty.worker.events" |
| 66 | + assert(we.configure{ shm = "my_worker_events", interval = 0.1 }) |
| 67 | + local healthcheck = require("resty.healthcheck") |
| 68 | + local checker = healthcheck.new({ |
| 69 | + name = "testing", |
| 70 | + shm_name = "test_shm", |
| 71 | + checks = { |
| 72 | + passive = { |
| 73 | + unhealthy = { |
| 74 | + tcp_failures = 2, |
| 75 | + http_failures = 2, |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + }) |
| 80 | + ngx.sleep(0.1) -- wait for initial timers to run once |
| 81 | + checker:add_target("127.0.0.1", 2112, "rush", true) |
| 82 | + checker:add_target("127.0.0.2", 2112, "rush", true) |
| 83 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true |
| 84 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true |
| 85 | + checker:report_http_status("127.0.0.1", 2112, "rush", 500) |
| 86 | + checker:report_http_status("127.0.0.1", 2112, "rush", 500) |
| 87 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- false |
| 88 | + checker:set_all_target_statuses_for_hostname("rush", 2112, true) |
| 89 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true |
| 90 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true |
| 91 | + } |
| 92 | + } |
| 93 | +--- request |
| 94 | +GET /t |
| 95 | +--- response_body |
| 96 | +true |
| 97 | +true |
| 98 | +false |
| 99 | +true |
| 100 | +true |
| 101 | +=== TEST 3: set_all_target_statuses_for_hostname() resets failure counters |
| 102 | +--- http_config eval |
| 103 | +qq{ |
| 104 | + $::HttpConfig |
| 105 | +} |
| 106 | +--- config |
| 107 | + location = /t { |
| 108 | + content_by_lua_block { |
| 109 | + local we = require "resty.worker.events" |
| 110 | + assert(we.configure{ shm = "my_worker_events", interval = 0.1 }) |
| 111 | + local healthcheck = require("resty.healthcheck") |
| 112 | + local checker = healthcheck.new({ |
| 113 | + name = "testing", |
| 114 | + shm_name = "test_shm", |
| 115 | + checks = { |
| 116 | + passive = { |
| 117 | + healthy = { |
| 118 | + successes = 2, |
| 119 | + }, |
| 120 | + unhealthy = { |
| 121 | + tcp_failures = 2, |
| 122 | + http_failures = 2, |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + }) |
| 127 | + ngx.sleep(0.1) -- wait for initial timers to run once |
| 128 | + checker:add_target("127.0.0.1", 2112, "rush", true) |
| 129 | + checker:add_target("127.0.0.2", 2112, "rush", true) |
| 130 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true |
| 131 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true |
| 132 | + checker:report_http_status("127.0.0.1", 2112, "rush", 500) |
| 133 | + checker:set_all_target_statuses_for_hostname("rush", 2112, true) |
| 134 | + checker:report_http_status("127.0.0.1", 2112, "rush", 500) |
| 135 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true |
| 136 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true |
| 137 | + checker:report_http_status("127.0.0.1", 2112, "rush", 500) |
| 138 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- false |
| 139 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- true |
| 140 | + } |
| 141 | + } |
| 142 | +--- request |
| 143 | +GET /t |
| 144 | +--- response_body |
| 145 | +true |
| 146 | +true |
| 147 | +true |
| 148 | +true |
| 149 | +false |
| 150 | +true |
| 151 | +=== TEST 4: set_target_status() resets the success counters |
| 152 | +--- http_config eval |
| 153 | +qq{ |
| 154 | + $::HttpConfig |
| 155 | +} |
| 156 | +--- config |
| 157 | + location = /t { |
| 158 | + content_by_lua_block { |
| 159 | + local we = require "resty.worker.events" |
| 160 | + assert(we.configure{ shm = "my_worker_events", interval = 0.1 }) |
| 161 | + local healthcheck = require("resty.healthcheck") |
| 162 | + local checker = healthcheck.new({ |
| 163 | + name = "testing", |
| 164 | + shm_name = "test_shm", |
| 165 | + checks = { |
| 166 | + passive = { |
| 167 | + healthy = { |
| 168 | + successes = 2, |
| 169 | + }, |
| 170 | + unhealthy = { |
| 171 | + tcp_failures = 2, |
| 172 | + http_failures = 2, |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + }) |
| 177 | + ngx.sleep(0.1) -- wait for initial timers to run once |
| 178 | + checker:add_target("127.0.0.1", 2112, "rush", true) |
| 179 | + checker:add_target("127.0.0.2", 2112, "rush", true) |
| 180 | + checker:set_all_target_statuses_for_hostname("rush", 2112, false) |
| 181 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- false |
| 182 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- false |
| 183 | + checker:report_http_status("127.0.0.1", 2112, "rush", 200) |
| 184 | + checker:set_all_target_statuses_for_hostname("rush", 2112, false) |
| 185 | + checker:report_http_status("127.0.0.1", 2112, "rush", 200) |
| 186 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- false |
| 187 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- false |
| 188 | + checker:report_http_status("127.0.0.1", 2112, "rush", 200) |
| 189 | + ngx.say(checker:get_target_status("127.0.0.1", 2112, "rush")) -- true |
| 190 | + ngx.say(checker:get_target_status("127.0.0.2", 2112, "rush")) -- false |
| 191 | + } |
| 192 | + } |
| 193 | +--- request |
| 194 | +GET /t |
| 195 | +--- response_body |
| 196 | +false |
| 197 | +false |
| 198 | +false |
| 199 | +false |
| 200 | +true |
| 201 | +false |
0 commit comments