least_conn.t 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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->yaml_config) {
  26. my $yaml_config = <<_EOC_;
  27. apisix:
  28. node_listen: 1984
  29. deployment:
  30. role: data_plane
  31. role_data_plane:
  32. config_provider: yaml
  33. _EOC_
  34. $block->set_value("yaml_config", $yaml_config);
  35. }
  36. my $route = <<_EOC_;
  37. routes:
  38. - upstream_id: 1
  39. uris:
  40. - /mysleep
  41. #END
  42. _EOC_
  43. $block->set_value("apisix_yaml", $block->apisix_yaml . $route);
  44. if (!$block->request) {
  45. $block->set_value("request", "GET /mysleep?seconds=0.1");
  46. }
  47. });
  48. run_tests();
  49. __DATA__
  50. === TEST 1: select highest weight
  51. --- apisix_yaml
  52. upstreams:
  53. - id: 1
  54. type: least_conn
  55. nodes:
  56. "127.0.0.1:1980": 2
  57. "127.0.0.1:1981": 1
  58. --- grep_error_log eval
  59. qr/proxy request to \S+ while connecting to upstream/
  60. --- grep_error_log_out
  61. proxy request to 127.0.0.1:1980 while connecting to upstream
  62. === TEST 2: select least conn
  63. --- apisix_yaml
  64. upstreams:
  65. - id: 1
  66. type: least_conn
  67. nodes:
  68. "127.0.0.1:1980": 3
  69. "0.0.0.0:1980": 2
  70. --- config
  71. location /t {
  72. content_by_lua_block {
  73. local http = require "resty.http"
  74. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  75. .. "/mysleep?seconds=0.1"
  76. local t = {}
  77. for i = 1, 3 do
  78. local th = assert(ngx.thread.spawn(function(i)
  79. local httpc = http.new()
  80. local res, err = httpc:request_uri(uri..i, {method = "GET"})
  81. if not res then
  82. ngx.log(ngx.ERR, err)
  83. return
  84. end
  85. end, i))
  86. table.insert(t, th)
  87. end
  88. for i, th in ipairs(t) do
  89. ngx.thread.wait(th)
  90. end
  91. }
  92. }
  93. --- request
  94. GET /t
  95. --- grep_error_log eval
  96. qr/proxy request to \S+ while connecting to upstream/
  97. --- grep_error_log_out
  98. proxy request to 127.0.0.1:1980 while connecting to upstream
  99. proxy request to 0.0.0.0:1980 while connecting to upstream
  100. proxy request to 127.0.0.1:1980 while connecting to upstream
  101. === TEST 3: retry
  102. --- apisix_yaml
  103. upstreams:
  104. - id: 1
  105. type: least_conn
  106. nodes:
  107. "127.0.0.1:1999": 2
  108. "127.0.0.1:1980": 1
  109. --- error_log
  110. connect() failed
  111. --- grep_error_log eval
  112. qr/proxy request to \S+ while connecting to upstream/
  113. --- grep_error_log_out
  114. proxy request to 127.0.0.1:1999 while connecting to upstream
  115. proxy request to 127.0.0.1:1980 while connecting to upstream
  116. === TEST 4: retry all nodes, failed
  117. --- apisix_yaml
  118. upstreams:
  119. - id: 1
  120. type: least_conn
  121. nodes:
  122. "127.0.0.1:1999": 2
  123. "0.0.0.0:1999": 1
  124. --- error_log
  125. connect() failed
  126. --- error_code: 502
  127. --- grep_error_log eval
  128. qr/proxy request to \S+ while connecting to upstream/
  129. --- grep_error_log_out
  130. proxy request to 127.0.0.1:1999 while connecting to upstream
  131. proxy request to 0.0.0.0:1999 while connecting to upstream