DR-TarantoolQueue

 view release on metacpan or  search on metacpan

t/010-tarantool-1.5/000-queue.t  view on Meta::CPAN

                            'task',
                        ],
                        indexes => {
                            0 => 'uuid',
                            1 => {
                                name => 'event',
                                fields => [qw(tube status event pri)]
                            }
                        }
                    }
                },
            };
            note $t->log unless $tnt;
    }
    $tnt;
};

ok tnt->ping, 'ping tarantool';
diag $t->log unless
    ok $t->started, 'Tarantool was started';
diag $t->log unless
    ok eval { tnt }, 'Client connected to';

my $sno = tnt->space('queue')->number;

my $task1 = tnt->call_lua('queue.put',
    [
        $sno,
        'tube_name',
        0,
        10,
        20,
        30,
        'task', 1 .. 10
    ]
)->raw;



is tnt->call_lua('queue.meta', [ $sno, $task1->[0] ])->raw(2), 'ready',
    'task1 is ready';

my $meta =  tnt->call_lua('queue.meta', [ $sno, $task1->[0] ],
    fields => [
        { type => 'STR', name => 'id' },
        { type => 'STR', name => 'tube' },
        { type => 'STR', name => 'status' },
        { type => 'NUM64', name => 'event' },
        { type => 'STR', name => 'ipri' },
        { type => 'STR', name => 'pri' },
        { type => 'NUM', name => 'cid' },
        { type => 'NUM64', name => 'created' },
        { type => 'NUM64', name => 'ttl' },
        { type => 'NUM64', name => 'ttr' },
        { type => 'NUM', name => 'cbury' },
        { type => 'NUM', name => 'ctaken' },
        { type => 'NUM64', name => 'now' },
    ]
);

cmp_ok $meta->now, '<', $meta->ttl + $meta->created, 'task is alive';
is $meta->ttl, 10000000, 'ttl';
is $meta->ttr, 20000000, 'ttr';

is_deeply $task1, [ $task1->[0], 'tube_name', 'ready', 'task', 1 .. 10 ],
    'task 1';


my $started = time;
my $task2 = tnt->call_lua('queue.put',
    [
        $sno,
        'tube_name',
        .5,
        10,
        20,
        30,
        'task', 10 .. 20
    ]
)->raw;

is tnt->call_lua('queue.meta', [ $sno, $task2->[0] ])->raw(2), 'delayed',
    'task2 is delayed';

is_deeply tnt->call_lua('queue.peek', [ $sno, $task2->[0] ])->raw, $task2,
    'task2.get';

is_deeply $task2, [ $task2->[0], 'tube_name',
    'delayed', 'task', 10 .. 20 ], 'task 2';

my $task1_t = tnt->call_lua('queue.take', [ $sno, 'tube_name', 5 ])->raw;
$task1->[2] = 'taken';
is_deeply $task1_t, $task1, 'task1 taken';
is tnt->call_lua('queue.meta', [ $sno, $task1->[0] ])->raw(2), 'taken',
    'task1 is taken';


my $task2_t = eval {tnt->call_lua('queue.take', [ $sno, 'tube_name', 5 ])->raw};
$task2->[2] = 'taken';
is_deeply $task2_t, $task2, 'task2 taken';
cmp_ok time - $started, '>=', .5, 'delay more than 0.5 second';
cmp_ok time - $started, '<=', .7, 'delay less than 0.7 second';

is_deeply tnt->call_lua('queue.peek', [ $sno, $task2->[0] ])->raw, $task2,
    'queue.peek';

my $task_ack = tnt->call_lua('queue.ack', [ $sno, $task2->[0] ])->raw;
is_deeply $task_ack, $task2, 'task was ack';


is_deeply scalar eval { tnt->call_lua('queue.peek', [ $sno, $task2->[0] ]) },
    undef, 'queue.peek';
like $@, qr{Task not found}, 'task not found';

$task_ack = eval { tnt->call_lua('queue.ack', [ $sno, $task2->[0] ]) };
like $@, qr{task not found}i, 'error message';
is $task_ack, undef, 'repeat ack';

$task1->[2] = 'ready';
is_deeply  tnt->call_lua('queue.release', [ $sno, $task1->[0] ])->raw, $task1,
    'task1 release';



( run in 1.011 second using v1.01-cache-2.11-cpan-39bf76dae61 )