Skip to content

Commit

Permalink
test: fix flaky 050-ttl.t
Browse files Browse the repository at this point in the history
We use system clock in the internal implementation for TTL, not
monotonic clock. The test may fail when changing the system clock. In
addition, fiber.sleep() may wake up earlier than planned by the
system clock.

We should to handle these situations in the test.

Closes #187
  • Loading branch information
oleg-jukovec authored and LeonidVas committed Sep 12, 2022
1 parent a30feac commit e56023d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions t/050-ttl.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,31 @@ local ttl = 0.1

test:ok(queue, 'queue is loaded')

local function test_take_after_ttl(test, tube, ttl)
local attempts, max_attempts = 0, 2
local before = fiber.time64()
local after = before

while after - before < ttl * 1000000 and attempts < max_attempts do
attempts = attempts + 1
fiber.sleep(ttl)
after = fiber.time64()
end

if after - before < ttl * 1000000 then
test:fail('failed to sleep ttl, is system clock changed?')
else
test:isnil(tube:take(.1), 'no task is taken')
end
end

test:test('one message per queue ffttl', function (test)
test:plan(20)
local tube = queue.create_tube('ompq_ffttl', 'fifottl', { engine = engine })
for i = 1, 20 do
tube:put('ompq_' .. i, {ttl=ttl})
fiber.sleep(ttl)
test:is(#{tube:take(.1)}, 0, 'no task is taken')

test_take_after_ttl(test, tube, ttl)
end
end)

Expand All @@ -32,8 +50,8 @@ test:test('one message per queue utttl', function (test)
local tube = queue.create_tube('ompq_utttl', 'utubettl', { engine = engine })
for i = 1, 20 do
tube:put('ompq_' .. i, {ttl=ttl})
fiber.sleep(ttl)
test:is(#{tube:take(.1)}, 0, 'no task is taken')

test_take_after_ttl(test, tube, ttl)
end
end)

Expand All @@ -42,8 +60,8 @@ test:test('many messages, one queue ffttl', function (test)
for i = 1, 20 do
local tube = queue.create_tube('mmpq_ffttl_' .. i, 'fifottl', { engine = engine })
tube:put('mmpq_' .. i, {ttl=ttl})
fiber.sleep(ttl)
test:is(#{tube:take(.1)}, 0, 'no task is taken')

test_take_after_ttl(test, tube, ttl)
end
end)

Expand All @@ -52,8 +70,8 @@ test:test('many messages, one queue utttl', function (test)
for i = 1, 20 do
local tube = queue.create_tube('mmpq_utttl_' .. i, 'utubettl', { engine = engine })
tube:put('mmpq_' .. i, {ttl=ttl})
fiber.sleep(ttl)
test:is(#{tube:take(.1)}, 0, 'no task is taken')

test_take_after_ttl(test, tube, ttl)
end
end)

Expand Down

0 comments on commit e56023d

Please sign in to comment.