Skip to content

Commit 056bea5

Browse files
authored
test: make stream traffic-split distribution assertions order-independent (#13630)
1 parent 6333d93 commit 056bea5

1 file changed

Lines changed: 23 additions & 40 deletions

File tree

t/stream-plugin/traffic-split.t

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,23 @@ add_block_preprocessor(sub {
5959
reqs[i] = { "/hit" }
6060
end
6161
local resps = { ngx.location.capture_multi(reqs) }
62-
for i, resp in ipairs(resps) do
63-
ngx.say(resp.body)
62+
-- Count responses per upstream rather than asserting their order:
63+
-- the round-robin picker starts at a random node, so which upstream
64+
-- serves each request varies run to run; only the 9:1 ratio is
65+
-- deterministic.
66+
local count = {}
67+
for _, resp in ipairs(resps) do
68+
local port = resp.body and resp.body:match("port (%d+)")
69+
local key = port and ("port " .. port) or "no response"
70+
count[key] = (count[key] or 0) + 1
71+
end
72+
local keys = {}
73+
for k in pairs(count) do
74+
keys[#keys + 1] = k
75+
end
76+
table.sort(keys)
77+
for _, k in ipairs(keys) do
78+
ngx.say(k, ": ", count[k])
6479
end
6580
}
6681
}
@@ -134,25 +149,9 @@ passed
134149
=== TEST 2: traffic split distribution between two upstreams
135150
--- request
136151
GET /test_multiple_requests
137-
--- response_body_like
138-
hello world from port 1995
139-
140-
hello world from port 1995
141-
142-
hello world from port 1995
143-
144-
hello world from port 1995
145-
146-
hello world from port 1995
147-
148-
hello world from port 1995
149-
150-
hello world from port 1995
151-
152-
hello world from port 1995
153-
154-
155-
hello world from port 1995
152+
--- response_body
153+
no response: 1
154+
port 1995: 9
156155
--- stream_enable
157156
--- error_log
158157
Connection refused
@@ -222,25 +221,9 @@ passed
222221
=== TEST 4: traffic split between plugin upstream and default route upstream
223222
--- request
224223
GET /test_multiple_requests
225-
--- response_body_like
226-
hello world from port 1995
227-
228-
hello world from port 1995
229-
230-
hello world from port 1995
231-
232-
hello world from port 1995
233-
234-
hello world from port 1995
235-
236-
hello world from port 1995
237-
238-
hello world from port 1995
239-
240-
hello world from port 1995
241-
242-
243-
hello world from port 1995
224+
--- response_body
225+
no response: 1
226+
port 1995: 9
244227
--- stream_enable
245228
--- error_log
246229
Connection refused

0 commit comments

Comments
 (0)