spacewander commented on a change in pull request #4965:
URL: https://github.com/apache/apisix/pull/4965#discussion_r701706770
##########
File path: t/plugin/proxy-mirror.t
##########
@@ -445,3 +445,143 @@ GET /t
passed
--- no_error_log
[error]
+
+
+
+=== TEST 13: sanity check (invalid percentage)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "proxy-mirror": {
+ "host": "http://127.0.0.1:1986",
+ "percentage": 10
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.print(body)
+ }
+ }
+--- request
+GET /t
+--- error_code: 400
+--- response_body
+{"error_msg":"failed to check the configuration of plugin proxy-mirror err:
property \"percentage\" validation failed: expected 10 to be smaller than 1"}
+--- no_error_log
+[error]
+
+
+
+=== TEST 14: set mirror requests percentage to 1
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "proxy-mirror": {
+ "host": "http://127.0.0.1:1986",
+ "percentage": 1
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- error_code: 200
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 15: hit route with percentage 1
+--- request
+GET /hello?percentage=1
+--- error_code: 200
+--- response_body
+hello world
+--- error_log_like eval
+qr/uri: \/hello\?percentage=1/
+
+
+
+=== TEST 16: set mirror requests percentage to 0.5
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "proxy-mirror": {
+ "host": "http://127.0.0.1:1986",
+ "percentage": 0.5
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- error_code: 200
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 17: hit route with percentage 0.5
Review comment:
TO design the almost range, I wrote a python script:
```
#!/usr/bin/env python3
# coding: utf-8
curr = [1, 1]
turns = 180
for i in range(1, turns+1):
prev = curr
curr = []
curr.append(1)
for j in range(1, i):
curr.append(prev[j-1] + prev[j])
curr.append(1)
n = sum(curr)
for i in range(turns+1):
print("%d / %d is %f" % (i, turns, curr[i] / float(n)))
exp_per = 0.999
mid = int(turns / 2)
for i in range(1, mid):
n_per = 0
for j in range(mid - i, mid + i + 1):
n_per += curr[j] / float(n)
if n_per > exp_per:
print(mid - i, j)
break
if n_per > exp_per:
break
```
So we can expect 99.9% of the result hit from 68 to 112.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]