least_conn2.t 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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(2);
  19. log_level('info');
  20. no_root_location();
  21. worker_connections(1024);
  22. no_shuffle();
  23. add_block_preprocessor(sub {
  24. my ($block) = @_;
  25. if (!$block->request) {
  26. $block->set_value("request", "GET /t");
  27. }
  28. });
  29. run_tests();
  30. __DATA__
  31. === TEST 1: upstream across multiple routes should not share the same version
  32. --- config
  33. location /t {
  34. content_by_lua_block {
  35. local t = require("lib.test_admin").test
  36. local code, body = t('/apisix/admin/upstreams/1',
  37. ngx.HTTP_PUT,
  38. [[{
  39. "type": "least_conn",
  40. "nodes": {
  41. "127.0.0.1:1980": 3,
  42. "0.0.0.0:1980": 2
  43. }
  44. }]]
  45. )
  46. assert(code < 300, body)
  47. local code, body = t('/apisix/admin/routes/1',
  48. ngx.HTTP_PUT,
  49. [[{
  50. "host": "1.com",
  51. "uri": "/mysleep",
  52. "upstream_id": "1"
  53. }]]
  54. )
  55. assert(code < 300, body)
  56. local code, body = t('/apisix/admin/routes/2',
  57. ngx.HTTP_PUT,
  58. [[{
  59. "host": "2.com",
  60. "uri": "/mysleep",
  61. "upstream_id": "1"
  62. }]]
  63. )
  64. assert(code < 300, body)
  65. }
  66. }
  67. === TEST 2: hit
  68. --- config
  69. location /t {
  70. content_by_lua_block {
  71. local http = require "resty.http"
  72. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  73. .. "/mysleep?seconds=0.1"
  74. local t = {}
  75. for i = 1, 2 do
  76. local th = assert(ngx.thread.spawn(function(i)
  77. local httpc = http.new()
  78. local res, err = httpc:request_uri(uri, {headers = {Host = i..".com"}})
  79. if not res then
  80. ngx.log(ngx.ERR, err)
  81. return
  82. end
  83. end, i))
  84. table.insert(t, th)
  85. end
  86. for i, th in ipairs(t) do
  87. ngx.thread.wait(th)
  88. end
  89. }
  90. }
  91. --- grep_error_log eval
  92. qr/proxy request to \S+ while connecting to upstream/
  93. --- grep_error_log_out
  94. proxy request to 127.0.0.1:1980 while connecting to upstream
  95. proxy request to 0.0.0.0:1980 while connecting to upstream