- Notifications
You must be signed in to change notification settings - Fork 55
Add limited fifo ttl queue #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
889d7f7 72ef440 ac0ece4 91a2777 ab618aa 5cd138d a604ea9 789c7e7 76ebb7b 1604e39 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| local fiber = require('fiber') | ||
| local fifottl = require('queue.abstract.driver.fifottl') | ||
| | ||
| local tube = {} | ||
| local methods = {} | ||
| | ||
| tube.create_space = fifottl.create_space | ||
| | ||
| -- start tube on space | ||
| function tube.new(space, on_task_change, opts) | ||
| local state = { | ||
| capacity = opts.capacity or 0, | ||
| parent = fifottl.new(space, on_task_change, opts) | ||
| } | ||
| | ||
| -- put task in space | ||
| local put = function (self, data, opts) | ||
| local timeout = opts.timeout or 0 | ||
| timeout = timeout * 1000000 | ||
| | ||
| while true do | ||
| local tube_size = self.space:count() | ||
| ||
| if tube_size < state.capacity or state.capacity == 0 then | ||
| return state.parent.put(self, data, opts) | ||
| else | ||
| if timeout == 0 then | ||
| return nil | ||
| end | ||
| | ||
| local started = fiber.time64() | ||
| ||
| fiber.yield() | ||
| ||
| local elapsed = fiber.time64() - started | ||
| | ||
| timeout = timeout > elapsed and timeout - elapsed or 0 | ||
| end | ||
| end | ||
| end | ||
| | ||
| return setmetatable({put = put}, {__index = state.parent}) | ||
| end | ||
| | ||
| return tube | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/usr/bin/env tarantool | ||
| local yaml = require('yaml') | ||
| local fiber = require('fiber') | ||
| | ||
| local test = require('tap').test() | ||
| test:plan(8) | ||
| | ||
| local queue = require('queue') | ||
| local state = require('queue.abstract.state') | ||
| | ||
| local engine = os.getenv('ENGINE') or 'memtx' | ||
| | ||
| local tnt = require('t.tnt') | ||
| tnt.cfg{} | ||
| | ||
| test:ok(rawget(box, 'space'), 'box started') | ||
| test:ok(queue, 'queue is loaded') | ||
| | ||
| local tube = queue.create_tube('lim3_tube', 'limfifottl', { engine = engine, capacity = 3 }) | ||
| local unlim_tube = queue.create_tube('unlim_tube', 'limfifottl', { engine = engine }) | ||
| | ||
| test:ok(tube, 'test tube created') | ||
| test:is(tube.name, 'lim3_tube', 'tube.name') | ||
| test:is(tube.type, 'limfifottl', 'tube.type') | ||
| | ||
| test:test('Put timeout is reached', function(test) | ||
| test:plan(4) | ||
| | ||
| test:ok(tube:put{1}, 'task 1 was put') | ||
| test:ok(tube:put{2}, 'task 2 was put') | ||
| test:ok(tube:put{3}, 'task 3 was put') | ||
| test:is(tube:put({4}, {timeout = 0.1}), nil, 'task 4 wasn\'t put cause timeout') | ||
| end) | ||
| | ||
| test:test('Put after freeing up space', function(test) | ||
| test:plan(3) | ||
| local put_fiber = fiber.create(function() | ||
| test:ok(tube:put({4}, {timeout = 1}), 'task 4 was put') | ||
| end) | ||
| | ||
| local task = tube:take() | ||
| test:ok(task, 'task 3 was taken') | ||
| test:is(tube:ack(task[1])[2], state.DONE, 'task 3 is done') | ||
| | ||
| while put_fiber:status() ~= 'dead' do | ||
| fiber.yield() | ||
| end | ||
| end) | ||
| | ||
| test:test('Unlimited tube put', function(test) | ||
| test:plan(3) | ||
| | ||
| test:is(unlim_tube:take(0), nil, 'tube is empty') | ||
| test:ok(unlim_tube:put{1}, 'task 1 was put') | ||
| test:ok(unlim_tube:put{2}, 'task 2 was put') | ||
| end) | ||
| | ||
| tnt.finish() | ||
| os.exit(test:check() == true and 0 or -1) | ||
| -- vim: set ft=lua : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If fiber.time64() will be changed on fiber.time(), then this multiplication will be not needed.