|
| 1 | +---@diagnostic disable: inject-field |
| 2 | +local fiber = require 'fiber' |
| 3 | +local clock = require 'clock' |
| 4 | +local xqueue = require 'xqueue' |
| 5 | + |
| 6 | +require 'test.setup' |
| 7 | + |
| 8 | +local t = require 'luatest' --[[@as luatest]] |
| 9 | +local g = t.group('touch') |
| 10 | + |
| 11 | +local queue |
| 12 | +local F |
| 13 | +local default_time_to = 0.25 |
| 14 | + |
| 15 | +g.before_each(function() |
| 16 | + if box.space.queue then |
| 17 | + box.space.queue:truncate() |
| 18 | + for i = #box.space.queue.index, 0, -1 do |
| 19 | + local ind = box.space.queue.index[i] |
| 20 | + ind:drop() |
| 21 | + end |
| 22 | + box.space.queue:drop() |
| 23 | + end |
| 24 | + |
| 25 | + queue = box.schema.space.create('queue', { if_not_exists = true }) --[[@as xqueue.space]] |
| 26 | + ---@class test.xqueue.delayed.tuple: box.tuple |
| 27 | + ---@field id number |
| 28 | + ---@field status string |
| 29 | + ---@field runat number |
| 30 | + ---@field payload any |
| 31 | + queue:format({ |
| 32 | + { name = 'id', type = 'unsigned' }, |
| 33 | + { name = 'status', type = 'string' }, |
| 34 | + { name = 'runat', type = 'number' }, |
| 35 | + { name = 'payload', type = 'any' }, |
| 36 | + }) |
| 37 | + |
| 38 | + F = { id = 1, status = 2, runat = 3, payload = 4 } |
| 39 | + |
| 40 | + queue:create_index('primary', { parts = {'id'} }) |
| 41 | + queue:create_index('status', { parts = {'status', 'id'} }) |
| 42 | + queue:create_index('runat', { parts = {'runat', 'id'} }) |
| 43 | + |
| 44 | + xqueue.upgrade(queue, { |
| 45 | + debug = true, |
| 46 | + fields = { |
| 47 | + runat = 'runat', |
| 48 | + status = 'status', |
| 49 | + }, |
| 50 | + features = { |
| 51 | + id = 'time64', |
| 52 | + keep = true, |
| 53 | + ttr = default_time_to, |
| 54 | + ttl = default_time_to, |
| 55 | + }, |
| 56 | + }) |
| 57 | +end) |
| 58 | + |
| 59 | +function g.test_touch_ttr() |
| 60 | + local puted_task = queue:put({payload = { 'x' }}) --[[@as test.xqueue.delayed.tuple]] |
| 61 | + t.assert_equals(puted_task.status, 'R', 'queue:put(...) must insert task in R status') |
| 62 | + |
| 63 | + local task = queue:take({timeout=0}) |
| 64 | + t.assert_equals(task.id, puted_task.id, 'queue not empty') |
| 65 | + t.assert_le(task.runat, clock.realtime() + default_time_to, 'taked task must have runat le than now+ttr') |
| 66 | + |
| 67 | + -- honest worker |
| 68 | + local begin_at = clock.realtime() |
| 69 | + while clock.realtime() <= begin_at + default_time_to*4 do |
| 70 | + fiber.sleep(default_time_to - 0.01) |
| 71 | + queue:touch(task, {increment=default_time_to}) |
| 72 | + end |
| 73 | + |
| 74 | + local acked_task = queue:ack(task) |
| 75 | + t.assert_equals(acked_task.status, 'D', 'task must have Done status') |
| 76 | +end |
| 77 | + |
| 78 | +function g.test_touch_ttl() |
| 79 | + local puted_task = queue:put({payload = { 'x' }}) --[[@as test.xqueue.delayed.tuple]] |
| 80 | + t.assert_equals(puted_task.status, 'R', 'queue:put(...) must insert task in R status') |
| 81 | + |
| 82 | + local task |
| 83 | + local begin_at = clock.realtime() |
| 84 | + while clock.realtime() <= begin_at + default_time_to*4 do |
| 85 | + fiber.sleep(default_time_to - 0.01) |
| 86 | + task = queue:touch(puted_task, {increment=default_time_to}) |
| 87 | + end |
| 88 | + |
| 89 | + t.assert_equals(task.status, 'R') |
| 90 | + t.assert_gt(task.runat, puted_task.runat + default_time_to*4 - 0.01) |
| 91 | + fiber.sleep(default_time_to*2) |
| 92 | + t.assert_equals(queue:take(), nil, 'task must be removed by ttl') |
| 93 | +end |
0 commit comments