healthcheck-multiple-worker.t 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. BEGIN {
  18. if ($ENV{TEST_NGINX_CHECK_LEAK}) {
  19. $SkipReason = "unavailable for the hup tests";
  20. } else {
  21. $ENV{TEST_NGINX_USE_HUP} = 1;
  22. undef $ENV{TEST_NGINX_USE_STAP};
  23. }
  24. }
  25. use t::APISIX 'no_plan';
  26. repeat_each(1);
  27. log_level('info');
  28. no_root_location();
  29. no_shuffle();
  30. workers(2);
  31. worker_connections(256);
  32. run_tests();
  33. __DATA__
  34. === TEST 1: set route(two upstream node: one healthy + one unhealthy)
  35. --- config
  36. location /t {
  37. content_by_lua_block {
  38. local t = require("lib.test_admin").test
  39. local code, body = t('/apisix/admin/routes/1',
  40. ngx.HTTP_PUT,
  41. [[{
  42. "uri": "/server_port",
  43. "upstream": {
  44. "type": "roundrobin",
  45. "nodes": {
  46. "127.0.0.1:1980": 1,
  47. "127.0.0.1:1970": 1
  48. },
  49. "checks": {
  50. "active": {
  51. "http_path": "/status",
  52. "host": "foo.com",
  53. "healthy": {
  54. "interval": 1,
  55. "successes": 1
  56. },
  57. "unhealthy": {
  58. "interval": 1,
  59. "http_failures": 2
  60. }
  61. }
  62. }
  63. }
  64. }]]
  65. )
  66. if code >= 300 then
  67. ngx.status = code
  68. end
  69. ngx.say(body)
  70. }
  71. }
  72. --- request
  73. GET /t
  74. --- response_body
  75. passed
  76. --- grep_error_log eval
  77. qr/^.*?\[error\](?!.*process exiting).*/
  78. --- grep_error_log_out
  79. === TEST 2: hit routes (two upstream node: one healthy + one unhealthy)
  80. --- config
  81. location /t {
  82. content_by_lua_block {
  83. ngx.sleep(3) -- wait for new workers replacement to complete
  84. local http = require "resty.http"
  85. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  86. .. "/server_port"
  87. do
  88. local httpc = http.new()
  89. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  90. end
  91. ngx.sleep(2.5)
  92. local ports_count = {}
  93. for i = 1, 12 do
  94. local httpc = http.new()
  95. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  96. if not res then
  97. ngx.say(err)
  98. return
  99. end
  100. ports_count[res.body] = (ports_count[res.body] or 0) + 1
  101. end
  102. local ports_arr = {}
  103. for port, count in pairs(ports_count) do
  104. table.insert(ports_arr, {port = port, count = count})
  105. end
  106. local function cmd(a, b)
  107. return a.port > b.port
  108. end
  109. table.sort(ports_arr, cmd)
  110. ngx.say(require("toolkit.json").encode(ports_arr))
  111. ngx.exit(200)
  112. }
  113. }
  114. --- request
  115. GET /t
  116. --- response_body
  117. [{"count":12,"port":"1980"}]
  118. --- grep_error_log eval
  119. qr/unhealthy TCP increment/
  120. --- grep_error_log_out
  121. unhealthy TCP increment
  122. unhealthy TCP increment
  123. --- timeout: 20