Skip to content

Commit

Permalink
tests(helpers): make wait_for_file_contents less flaky (#10815)
Browse files Browse the repository at this point in the history
This updates the test to use assert.near() with 25% leeway and adds
ngx.update_time() calls when calculating duration in order to get a
more accurate result.

(cherry picked from commit f20d961)
  • Loading branch information
flrgh authored and locao committed May 24, 2023
1 parent 9503eaa commit 5065f4a
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions spec/02-integration/01-helpers/01-helpers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -942,18 +942,20 @@ describe("helpers: utilities", function()
local fname = assert(helpers.path.tmpname())
assert(os.remove(fname))

local timeout = 1
local delay = 0.25
local timeout = 2
local delay = 1
local start, duration

local sema = require("ngx.semaphore").new()

local ok, res
ngx.timer.at(0, function()
ngx.update_time()
start = time()

ok, res = pcall(helpers.wait_for_file_contents, fname, timeout)

ngx.update_time()
duration = time() - start
sema:post(1)
end)
Expand All @@ -967,16 +969,9 @@ describe("helpers: utilities", function()
assert.truthy(ok, "timer raised an error: " .. tostring(res))
assert.equals("test", res)

-- allow for some jitter
timeout = timeout * 1.25

assert.truthy(duration <= timeout,
"expected to finish in <" .. tostring(timeout) .. "s" ..
" but took " .. tostring(duration) .. "s")

assert.truthy(duration >= delay,
"expected to finish in >=" .. tostring(delay) .. "s" ..
" but took " .. tostring(duration) .. "s")
assert.near(delay, duration, delay * 0.25,
"expected wait_for_file_contents to return in " ..
"~" .. delay .. " seconds")
end)

it("doesn't wait longer than the timeout in the failure case", function()
Expand All @@ -989,10 +984,12 @@ describe("helpers: utilities", function()

local ok, err
ngx.timer.at(0, function()
ngx.update_time()
start = time()

ok, err = pcall(helpers.wait_for_file_contents, fname, timeout)

ngx.update_time()
duration = time() - start
sema:post(1)
end)
Expand All @@ -1003,10 +1000,9 @@ describe("helpers: utilities", function()
assert.falsy(ok, "expected wait_for_file_contents to fail")
assert.not_nil(err)

local diff = math.abs(duration - timeout)
assert.truthy(diff < 0.5,
"expected to finish in about " .. tostring(timeout) .. "s" ..
" but took " .. tostring(duration) .. "s")
assert.near(timeout, duration, timeout * 0.25,
"expected wait_for_file_contents to timeout after " ..
"~" .. timeout .. " seconds")
end)


Expand Down

0 comments on commit 5065f4a

Please sign in to comment.