Skip to content

Commit

Permalink
chore(test): fix flaky tests (#11451)
Browse files Browse the repository at this point in the history
chore(test): flakiness caused by assuming order
Generally, if the API definition does not require data ordering, we should not make assumptions about the ordering. Otherwise, the test could be flaky.

fix(test): flakiness due to time
We should always update the time before a time-sensitive test.
sleep implicitly updates time, but we need an accurate start time before sleep.

Thanks to @ADD-SP's help.

Fix KAG-2417
  • Loading branch information
StarlightIbuki authored Aug 24, 2023
1 parent 05c3f3b commit cef643f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
30 changes: 19 additions & 11 deletions spec/02-integration/04-admin_api/08-targets_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,21 @@ describe("Admin API #" .. strategy, function()

describe("GET", function()
local apis = {}
local api_map

local upstream

local function target_list_to_map(list)
local map = {}
for _, t in ipairs(list) do
map[t.target] = t
if t.tags == ngx.null then
t.tags = nil
end
end
return map
end

before_each(function()
upstream = bp.upstreams:insert {}

Expand All @@ -267,10 +279,12 @@ describe("Admin API #" .. strategy, function()
weight = 10,
upstream = { id = upstream.id },
}

api_map = target_list_to_map(apis)
end)

it("shows all targets", function()
for _, append in ipairs({ "", "/" }) do
for _, append in ipairs({ "", "/" }) do
it("shows all targets with " .. (append == "" and "no" or "") .. " ending slash", function()
local res = assert(client:send {
method = "GET",
path = "/upstreams/" .. upstream.name .. "/targets" .. append,
Expand All @@ -281,15 +295,9 @@ describe("Admin API #" .. strategy, function()
-- we got four active targets for this upstream
assert.equal(4, #json.data)

-- when multiple active targets are present, we only see the last one
assert.equal(apis[4].id, json.data[1].id)

-- validate the remaining returned targets
assert.equal(apis[3].target, json.data[2].target)
assert.equal(apis[2].target, json.data[3].target)
assert.equal(apis[1].target, json.data[4].target)
end
end)
assert.same(api_map, target_list_to_map(json.data))
end)
end

describe("empty results", function()
it("data property is an empty array", function()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ for _, strategy in helpers.each_strategy() do

assert(cluster_events_1:subscribe("nbf_channel", cb, false)) -- false to not start auto polling

-- we need accurate time, otherwise the test would be flaky
ngx.update_time()
assert(cluster_events_2:broadcast("nbf_channel", "hello world"))

assert(cluster_events_1:poll())
Expand Down

0 comments on commit cef643f

Please sign in to comment.