From 6badeb8bf4c149a6b39f33c0155d5522be0f642a Mon Sep 17 00:00:00 2001 From: Kong Team Gateway Bot <98048765+team-gateway-bot@users.noreply.github.com> Date: Tue, 26 Sep 2023 08:00:35 -0700 Subject: [PATCH] chore(test): fix flaky tests (#11451) (#11634) 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 (cherry picked from commit cef643f1af89c486439862b3907ccacb6ab28b69) Co-authored-by: Xumin <100666470+StarlightIbuki@users.noreply.github.com> --- .../04-admin_api/08-targets_routes_spec.lua | 30 ++++++++++++------- .../01-cluster_events_spec.lua | 2 ++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/spec/02-integration/04-admin_api/08-targets_routes_spec.lua b/spec/02-integration/04-admin_api/08-targets_routes_spec.lua index 5676ec08a8b5..3f58cf6024e0 100644 --- a/spec/02-integration/04-admin_api/08-targets_routes_spec.lua +++ b/spec/02-integration/04-admin_api/08-targets_routes_spec.lua @@ -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 {} @@ -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, @@ -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() diff --git a/spec/02-integration/06-invalidations/01-cluster_events_spec.lua b/spec/02-integration/06-invalidations/01-cluster_events_spec.lua index b2831e7ebfbb..a931160da38a 100644 --- a/spec/02-integration/06-invalidations/01-cluster_events_spec.lua +++ b/spec/02-integration/06-invalidations/01-cluster_events_spec.lua @@ -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())