healthcheck3.t 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. use t::APISIX 'no_plan';
  18. repeat_each(1);
  19. log_level('info');
  20. no_root_location();
  21. no_shuffle();
  22. worker_connections(256);
  23. run_tests();
  24. __DATA__
  25. === TEST 1: set route(two healthy upstream nodes)
  26. --- config
  27. location /t {
  28. content_by_lua_block {
  29. local t = require("lib.test_admin").test
  30. local code, body = t('/apisix/admin/routes/1',
  31. ngx.HTTP_PUT,
  32. [[{
  33. "uri": "/server_port",
  34. "upstream": {
  35. "type": "roundrobin",
  36. "nodes": {
  37. "127.0.0.1:1980": 1,
  38. "127.0.0.1:1981": 1
  39. },
  40. "checks": {
  41. "active": {
  42. "http_path": "/status",
  43. "host": "foo.com",
  44. "healthy": {
  45. "interval": 1,
  46. "successes": 1
  47. },
  48. "unhealthy": {
  49. "interval": 1,
  50. "http_failures": 2
  51. }
  52. }
  53. }
  54. }
  55. }]]
  56. )
  57. if code >= 300 then
  58. ngx.status = code
  59. end
  60. ngx.say(body)
  61. }
  62. }
  63. --- request
  64. GET /t
  65. --- response_body
  66. passed
  67. --- grep_error_log eval
  68. qr/^.*?\[error\](?!.*process exiting).*/
  69. --- grep_error_log_out
  70. === TEST 2: In case of concurrency only one request can create a checker
  71. --- config
  72. location /t {
  73. content_by_lua_block {
  74. local healthcheck = require("resty.healthcheck")
  75. local test = healthcheck.new
  76. healthcheck.new = function(...)
  77. ngx.sleep(1)
  78. return test(...)
  79. end
  80. local http = require "resty.http"
  81. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  82. .. "/server_port"
  83. local send_request = function()
  84. local httpc = http.new()
  85. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  86. if not res then
  87. ngx.log(ngx.ERR, err)
  88. return
  89. end
  90. end
  91. local t = {}
  92. for i = 1, 10 do
  93. local th = assert(ngx.thread.spawn(send_request))
  94. table.insert(t, th)
  95. end
  96. for i, th in ipairs(t) do
  97. ngx.thread.wait(th)
  98. end
  99. ngx.exit(200)
  100. }
  101. }
  102. --- request
  103. GET /t
  104. --- grep_error_log eval
  105. qr/create new checker/
  106. --- grep_error_log_out
  107. create new checker