Skip to content

Commit e56023d

Browse files
oleg-jukovecLeonidVas
authored andcommitted
test: fix flaky 050-ttl.t
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
1 parent a30feac commit e56023d

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

t/050-ttl.t

+26-8
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,31 @@ local ttl = 0.1
1717

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

20+
local function test_take_after_ttl(test, tube, ttl)
21+
local attempts, max_attempts = 0, 2
22+
local before = fiber.time64()
23+
local after = before
24+
25+
while after - before < ttl * 1000000 and attempts < max_attempts do
26+
attempts = attempts + 1
27+
fiber.sleep(ttl)
28+
after = fiber.time64()
29+
end
30+
31+
if after - before < ttl * 1000000 then
32+
test:fail('failed to sleep ttl, is system clock changed?')
33+
else
34+
test:isnil(tube:take(.1), 'no task is taken')
35+
end
36+
end
37+
2038
test:test('one message per queue ffttl', function (test)
2139
test:plan(20)
2240
local tube = queue.create_tube('ompq_ffttl', 'fifottl', { engine = engine })
2341
for i = 1, 20 do
2442
tube:put('ompq_' .. i, {ttl=ttl})
25-
fiber.sleep(ttl)
26-
test:is(#{tube:take(.1)}, 0, 'no task is taken')
43+
44+
test_take_after_ttl(test, tube, ttl)
2745
end
2846
end)
2947

@@ -32,8 +50,8 @@ test:test('one message per queue utttl', function (test)
3250
local tube = queue.create_tube('ompq_utttl', 'utubettl', { engine = engine })
3351
for i = 1, 20 do
3452
tube:put('ompq_' .. i, {ttl=ttl})
35-
fiber.sleep(ttl)
36-
test:is(#{tube:take(.1)}, 0, 'no task is taken')
53+
54+
test_take_after_ttl(test, tube, ttl)
3755
end
3856
end)
3957

@@ -42,8 +60,8 @@ test:test('many messages, one queue ffttl', function (test)
4260
for i = 1, 20 do
4361
local tube = queue.create_tube('mmpq_ffttl_' .. i, 'fifottl', { engine = engine })
4462
tube:put('mmpq_' .. i, {ttl=ttl})
45-
fiber.sleep(ttl)
46-
test:is(#{tube:take(.1)}, 0, 'no task is taken')
63+
64+
test_take_after_ttl(test, tube, ttl)
4765
end
4866
end)
4967

@@ -52,8 +70,8 @@ test:test('many messages, one queue utttl', function (test)
5270
for i = 1, 20 do
5371
local tube = queue.create_tube('mmpq_utttl_' .. i, 'utubettl', { engine = engine })
5472
tube:put('mmpq_' .. i, {ttl=ttl})
55-
fiber.sleep(ttl)
56-
test:is(#{tube:take(.1)}, 0, 'no task is taken')
73+
74+
test_take_after_ttl(test, tube, ttl)
5775
end
5876
end)
5977

0 commit comments

Comments
 (0)