Permalink
Browse files

add test and using _execute() method

  • Loading branch information...
1 parent 6f39222 commit 3e073e6632fb71377566b5e4884c1fb2fe8495db @masartz masartz committed Oct 8, 2010
View
285 lib/Qudo/Driver/DBI.pm
@@ -36,6 +36,12 @@ sub _connect {
);
}
+sub dbh{
+ my $self = shift;
+
+ return $self->{dbh};
+}
+
sub job_status_list {
my ($self, $args) = @_;
@@ -86,26 +92,35 @@ sub job_status_list {
sub job_count {
my ($self , $funcs) = @_;
+ my @bind;
+ if( ref $funcs eq 'ARRAY' ){
+ @bind = @{$funcs};
+ }
+ elsif( defined $funcs ){
+ push @bind , $funcs;
+ }
+
my $sql = q{
SELECT
COUNT(job.id) AS count
FROM
- job, func
- WHERE
- job.func_id = func.id
+ job
};
- if( $funcs ){
- $sql .= q{ AND }. $self->_join_func_name($funcs);
+
+ if( scalar @bind ){
+ $sql .= q{
+ INNER JOIN
+ func ON job.func_id = func.id
+ WHERE
+ };
+ $sql .= $self->_join_func_name( \@bind );
}
- my $sth = $self->{dbh}->prepare( $sql );
+ my $sth = $self->_execute(
+ $sql,
+ \@bind
+ );
- eval{
- $sth->execute( @{$funcs} );
- };
- if( my $e = $@ ){
- croak 'job_count ERROR'.$e;
- }
my $ret = $sth->fetchrow_hashref();
return $ret->{count};
}
@@ -133,14 +148,10 @@ sub job_list {
$sql .= q{LIMIT ?};
push @bind , $limit;
- my $sth = $self->{dbh}->prepare( $sql );
-
- eval{
- $sth->execute( @bind );
- };
- if( my $e = $@ ){
- croak 'job_list ERROR'.$e;
- }
+ my $sth = $self->_execute(
+ $sql,
+ \@bind
+ );
my $code = $self->_get_job_data( $sth );
@@ -198,13 +209,11 @@ sub exception_list {
push @bind , $offset;
}
- my $sth = $self->{dbh}->prepare( $sql );
- eval{
- $sth->execute( @bind );
- };
- if( my $e = $@ ){
- croak 'exception_list ERROR'.$e;
- }
+ my $sth = $self->_execute(
+ $sql,
+ \@bind
+ );
+
my @exception_list;
while (my $row = $sth->fetchrow_hashref) {
push @exception_list, $row;
@@ -228,14 +237,10 @@ sub lookup_job {
# limit
$sql .= q{ LIMIT 1};
- my $sth = $self->{dbh}->prepare( $sql );
-
- eval{
- $sth->execute( @bind );
- };
- if( my $e = $@ ){
- croak 'lookup_job ERROR'.$e;
- }
+ my $sth = $self->_execute(
+ $sql,
+ \@bind
+ );
return $self->_get_job_data( $sth );
}
@@ -266,14 +271,10 @@ sub find_job {
$sql .= q{ LIMIT ? };
push @bind , $limit;
- my $sth = $self->{dbh}->prepare( $sql );
-
- eval{
- $sth->execute( @bind );
- };
- if( my $e = $@ ){
- croak 'find_job ERROR'.$e;
- }
+ my $sth = $self->_execute(
+ $sql,
+ \@bind
+ );
return $self->_get_job_data( $sth );
}
@@ -313,60 +314,54 @@ sub _get_job_data {
sub grab_a_job {
my ($self, %args) = @_;
- my $sth = $self->{dbh}->prepare(
- q{
- UPDATE
- job
- SET
- grabbed_until = ?
- WHERE
- id = ?
- AND
- grabbed_until = ?
- }
+ my $sql = q{
+ UPDATE
+ job
+ SET
+ grabbed_until = ?
+ WHERE
+ id = ?
+ AND
+ grabbed_until = ?
+ };
+
+ my @bind = (
+ $args{grabbed_until},
+ $args{job_id},
+ $args{old_grabbed_until},
);
- my $rows;
- eval{
- $rows = $sth->execute(
- $args{grabbed_until},
- $args{job_id},
- $args{old_grabbed_until},
- );
- };
- if( my $e = $@ ){
- croak 'grab_a_job ERROR'.$e;
- return;
- }
+ my $sth = $self->_execute(
+ $sql,
+ \@bind
+ );
- return $rows;
+ return $sth->rows;
}
sub logging_exception {
my ($self, $args) = @_;
- my $sth = $self->{dbh}->prepare(
- q{
- INSERT INTO exception_log
- ( func_id , message , uniqkey, arg, exception_time, retried)
- VALUES
- ( ? , ? , ?, ?, ?, ?)
- }
+ my $sql = q{
+ INSERT INTO exception_log
+ ( func_id , message , uniqkey, arg, exception_time, retried)
+ VALUES
+ ( ? , ? , ?, ?, ?, ?)
+ };
+ my @bind = (
+ $args->{func_id} ,
+ $args->{message} ,
+ $args->{uniqkey} ,
+ $args->{arg} ,
+ time(),
+ 0,
+ );
+
+ my $sth = $self->_execute(
+ $sql,
+ \@bind
);
- eval{
- $sth->execute(
- $args->{func_id} ,
- $args->{message} ,
- $args->{uniqkey} ,
- $args->{arg} ,
- time(),
- 0,
- );
- };
- if( my $e = $@ ){
- croak 'logging_exception ERROR'.$e;
- }
return;
}
@@ -415,103 +410,87 @@ sub enqueue {
'job',
\@column
);
-
- my $sth_ins = $self->{dbh}->prepare( $sql );
my @bind = map {$args->{$_}} @column;
- eval{
- $sth_ins->execute( @bind );
- };
- if( $@ ){
- croak 'enqueue ERROR'.$@;
- }
+
+ my $sth_ins = $self->_execute(
+ $sql,
+ \@bind
+ );
+
my $id = $self->{dbd}->last_insert_id($self->{dbh}, $sth_ins);
- my $sth_sel = $self->{dbh}->prepare(
- q{SELECT * FROM job WHERE id = ?}
+ my $sth_sel = $self->_execute(
+ q{SELECT * FROM job WHERE id = ?} ,
+ [ $id ]
);
- $sth_sel->execute( $id );
my $ret_sel = $sth_sel->fetchrow_hashref();
return $ret_sel ? $ret_sel->{id} : undef;
}
sub reenqueue {
my ($self, $job_id, $args) = @_;
- my $sth = $self->{dbh}->prepare(
- q{
- UPDATE
- job
- SET
- enqueue_time = ?,
- run_after = ?,
- retry_cnt = ?,
- grabbed_until = ?
- WHERE
- id = ?
- }
+ my $sql = q{
+ UPDATE
+ job
+ SET
+ enqueue_time = ?,
+ run_after = ?,
+ retry_cnt = ?,
+ grabbed_until = ?
+ WHERE
+ id = ?
+ };
+
+ my @bind = (
+ time,
+ (time + ($args->{retry_delay}||0) ),
+ $args->{retry_cnt},
+ $args->{grabbed_until},
+ $job_id,
);
- my $row;
- eval{
- $row = $sth->execute(
- time,
- (time + ($args->{retry_delay}||0) ),
- $args->{retry_cnt},
- $args->{grabbed_until},
- $job_id,
- );
- };
- if( my $e = $@ ){
- croak 'reenqueue ERROR'.$e;
- return;
- }
+ my $sth = $self->_execute(
+ $sql,
+ \@bind
+ );
- return $row;
+ return $sth->rows;
}
sub dequeue {
my ($self, $args) = @_;
- my $sth = $self->{dbh}->prepare(
- q{DELETE FROM job WHERE id = ?}
- );
- my $row;
- eval{
- $row = $sth->execute( $args->{id} );
- };
- if( my $e = $@ ){
- croak 'dequeue ERROR'.$e;
- }
+ my $sth = $self->_execute(
+ q{DELETE FROM job WHERE id = ?} ,
+ [ $args->{id} ]
+ );
- return $row;
+ return $sth->rows;
}
sub get_func_id {
my ($self, $funcname) = @_;
- my $sth_sel = $self->{dbh}->prepare(
- q{SELECT * FROM func WHERE name = ?}
+ my $sth_sel = $self->_execute(
+ q{SELECT * FROM func WHERE name = ?} ,
+ [ $funcname ]
);
- $sth_sel->execute( $funcname );
my $func_id;
my $ret_hashref = $sth_sel->fetchrow_hashref();
if ( $ret_hashref ){
$func_id = $ret_hashref->{id};
}
else{
- my $sth_ins = $self->{dbh}->prepare(
- q{INSERT INTO func ( name ) VALUES ( ? )}
+ my $sth_ins = $self->_execute(
+ q{INSERT INTO func ( name ) VALUES ( ? )} ,
+ [ $funcname ]
);
- eval{
- $sth_ins->execute( $funcname );
- };
- if( my $e = $@ ){
- croak $e;
- }
+
$sth_sel->execute( $funcname );
my $ret_hashref = $sth_sel->fetchrow_hashref();
if ( $ret_hashref ){
@@ -525,14 +504,10 @@ sub get_func_id {
sub get_func_name {
my ($self, $funcid) = @_;
- my $sth;
- eval {
- $sth = $self->{dbh}->prepare(
- q{SELECT * FROM func WHERE id = ?}
- );
- $sth->execute( $funcid );
- };
- if ($@) { croak $@ }
+ my $sth = $self->_execute(
+ q{SELECT * FROM func WHERE id = ?} ,
+ [ $funcid ]
+ );
my $ret_hashref = $sth->fetchrow_hashref();
return $ret_hashref ? $ret_hashref->{name} : undef;
View
37 t/003manager/can_do.t
@@ -0,0 +1,37 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use Test::Output;
+
+run_tests(1, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+ $manager->enqueue("Worker::Test2", { arg => 'oops', uniqkey => 'uniqkey'});
+ is $manager->work_once, undef;
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ print STDOUT $job->arg;
+ $job->completed;
+}
+
+package Worker::Test2;
+use base 'Qudo::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ print STDOUT $job->arg;
+ $job->completed;
+}
+
View
16 t/003manager/dose_not_has_abilities.t
@@ -0,0 +1,16 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use Test::Output;
+
+run_tests(1, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ ok ! $master->manager->has_abilities;
+
+ teardown_dbs;
+});
+
View
23 t/003manager/enqueue.t
@@ -0,0 +1,23 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+
+run_tests(3, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+ my $job = $manager->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+
+ is $job->id, 1;
+ is $job->arg, 'arg';
+ is $job->uniqkey, 'uniqkey';
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
View
52 t/003manager/enqueue_from_failed_job.t
@@ -0,0 +1,52 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use Test::Output;
+
+run_tests(11, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+ my $job = $manager->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+
+ is $job->id, 1;
+ is $job->arg, 'arg';
+ is $job->uniqkey, 'uniqkey';
+
+ $manager->work_once; # worker failed.
+
+ my $exception = $master->exception_list;
+ my ($db, $rows) = each %$exception;
+
+ is $rows->[0]->{retried}, 0;
+ $job = $manager->enqueue_from_failed_job($rows->[0], $db);
+
+ is $job->id, 2;
+ is $job->arg, 'arg';
+ is $job->uniqkey, 'uniqkey';
+ is $job->db, $db;
+
+ $exception = $master->exception_list;
+ ($db, $rows) = each %$exception;
+ is $rows->[0]->{retried}, 1;
+
+ stderr_like( sub {$manager->enqueue_from_failed_job($rows->[0], $db)}, qr/this exception is already retried/);
+
+ my $row = $master->job_count([qw/Worker::Test/]);
+ my ($dsn, $count) = each %$row;
+ is $count, 1;
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+
+sub grab_for { 0 }
+sub work {
+ die 'failed';
+}
View
22 t/003manager/funcname.t
@@ -0,0 +1,22 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+
+run_tests(2, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $db = $master->shuffled_databases;
+ my $manager = $master->manager;
+
+ my $func_id = $manager->funcname_to_id( 'Worker::Test', $db );
+ is $func_id, 1;
+
+ my $func_name = $manager->funcid_to_name($func_id, $db);
+ is $func_name, 'Worker::Test';
+
+ teardown_dbs;
+});
+
View
29 t/003manager/grab_a_job.t
@@ -0,0 +1,29 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+
+run_tests(4, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+
+ $manager->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+ my $job = $manager->find_job;
+
+ is $job->id, 1;
+ is $job->arg, 'arg';
+ is $job->uniqkey, 'uniqkey';
+
+ ok ! $manager->find_job;
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+sub grab_for { 10 }
+
View
17 t/003manager/has_abilities.t
@@ -0,0 +1,17 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use Test::Output;
+
+run_tests(1, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ manager_abilities => [qw/Worker::Test/],
+ );
+
+ ok $master->manager->has_abilities;
+
+ teardown_dbs;
+});
+
View
45 t/003manager/priority.t
@@ -0,0 +1,45 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use List::Util qw/shuffle/;
+
+my $expected_priority;
+run_tests(10, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+
+ my @job_priority = (1..10);
+
+ for my $priority (shuffle @job_priority) {
+ $manager->enqueue(
+ "Worker::Test",
+ {
+ arg => 'arg'.$priority,
+ priority => $priority,
+ }
+ );
+ }
+
+ for (reverse @job_priority) {
+ $expected_priority = $_;
+ $manager->work_once;
+ }
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+use Test::More;
+
+sub work {
+ my ($class, $job,) = @_;
+ is $job->priority, $expected_priority;
+ $job->completed;
+}
+
View
32 t/004worker/abort_worker.t
@@ -0,0 +1,32 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use Test::Output;
+
+run_tests(3, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+ $manager->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+ $manager->work_once; # abort worker
+
+ my $exception = $master->exception_list;
+ my ($db, $rows) = %$exception;
+ like $rows->[0]->{message}, qr/^abort!!/;
+ is $rows->[0]->{arg}, 'arg';
+ is scalar(@$rows), 1;
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+sub set_job_status { 1 }
+sub work {
+ my ($class, $job) = @_;
+ $job->abort;
+}
View
27 t/004worker/basic.t
@@ -0,0 +1,27 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::Output;
+
+run_tests(1, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+ $master->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+ stdout_is( sub { $manager->work_once } , "arg");
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ print STDOUT $job->arg;
+ $job->completed;
+}
+
View
32 t/004worker/failed_worker.t
@@ -0,0 +1,32 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use Test::Output;
+
+run_tests(3, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+ $manager->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+ $manager->work_once; # failed worker
+
+ my $exception = $master->exception_list;
+ my ($db, $rows) = %$exception;
+ like $rows->[0]->{message}, qr/^failed worker/;
+ is $rows->[0]->{arg}, 'arg';
+ is scalar(@$rows), 1;
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ die "failed worker";
+}
View
93 t/004worker/job_status.t
@@ -0,0 +1,93 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use Test::Output;
+
+run_tests(8, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ {
+ $manager->can_do('Worker::Test');
+ $manager->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+ $manager->work_once; # not logging job_status
+
+ my $job_status = $master->job_status_list;
+ my ($dsn, $rows) = each %$job_status;
+ is scalar(@$rows), 0;
+ }
+ {
+ $manager->can_do('Worker::Test2');
+ my $job = $manager->enqueue("Worker::Test2", { arg => 'arg', uniqkey => 'uniqkey'});
+ $manager->work_once; # logging job_status
+
+ my $job_status = $master->job_status_list;
+ my ($dsn, $rows) = each %$job_status;
+ is $rows->[0]->{func_id}, $job->func_id;
+ is $rows->[0]->{status}, 'completed';
+ is $rows->[0]->{arg}, 'arg';
+ is $rows->[0]->{uniqkey}, 'uniqkey';
+ is scalar(@$rows), 1;
+ }
+ {
+ $manager->can_do('Worker::Test3');
+ my $job = $manager->enqueue("Worker::Test3", { arg => 'arg', uniqkey => 'uniqkey'});
+ $manager->work_once; # logging job_status
+
+ my $job_status = $master->job_status_list({ funcs => ['Worker::Test2','Worker::Test3'] });
+ my ($dsn, $rows) = each %$job_status;
+ is scalar(@$rows), 2;
+
+ }
+ {
+ $manager->can_do('Worker::Test4');
+ $manager->enqueue("Worker::Test4", { arg => 'arg', uniqkey => 'uniqkey'});
+ my $job = $manager->work_once; # logging failed job_status
+
+ my $job_status = $master->job_status_list({ funcs => ['Worker::Test2','Worker::Test3','Worker::Test4'] });
+ my ($dsn, $rows) = each %$job_status;
+ is scalar(@$rows), 3;
+
+ }
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+
+sub set_job_status { 0 }
+sub work {
+ my ($class, $job) = @_;
+ $job->completed;
+}
+
+package Worker::Test2;
+use base 'Qudo::Worker';
+
+sub set_job_status { 1 }
+sub work {
+ my ($class, $job) = @_;
+ $job->completed;
+}
+
+package Worker::Test3;
+use base 'Qudo::Worker';
+
+sub set_job_status { 1 }
+sub work {
+ my ($class, $job) = @_;
+ $job->completed;
+}
+
+package Worker::Test4;
+use base 'Qudo::Worker';
+
+sub set_job_status { 1 }
+sub work {
+ my ($class, $job) = @_;
+ die 'ooops worker::test4 is failed';
+}
+
View
28 t/004worker/memory_leak.t
@@ -0,0 +1,28 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::Output;
+use Test::Requires 'Test::Memory::Cycle';
+
+run_tests(1, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+ $master->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+
+ memory_cycle_ok($master, 'master');
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ $job->completed;
+}
+
View
34 t/004worker/reenqueue.t
@@ -0,0 +1,34 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use Test::Output;
+
+run_tests(2, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+ $manager->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+
+ stdout_is( sub {$manager->work_once}, 0 ); # fail job
+
+ sleep(1);
+
+ stdout_is( sub {$manager->work_once}, 1 ); # check job
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+
+sub max_retries { 1 }
+sub grab_for { 0 }
+sub work {
+ my ($class, $job) = @_;
+
+ print STDOUT $job->retry_cnt;
+}
View
47 t/004worker/replace.t
@@ -0,0 +1,47 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+
+run_tests(4, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my ($dsn, $cnt);
+
+ ($dsn, $cnt) = each %{$master->job_count('Worker::Test2')};
+ is $cnt, 0;
+ ($dsn, $cnt) = each %{$master->job_count('Worker::Test3')};
+ is $cnt, 0;
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+ $master->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+ $manager->work_once;
+
+ ($dsn, $cnt) = each %{$master->job_count('Worker::Test2')};
+ is $cnt, 1;
+ ($dsn, $cnt) = each %{$master->job_count('Worker::Test3')};
+ is $cnt, 1;
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ $job->replace(
+ [
+ 'Worker::Test2',
+ {arg => 'arg'}
+ ],
+ [
+ 'Worker::Test3',
+ {arg => 'arg'}
+ ],
+ );
+}
+
View
37 t/004worker/retry_delay.t
@@ -0,0 +1,37 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use Test::Output;
+
+run_tests(3, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+ $manager->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
+
+ stdout_is( sub {$manager->work_once}, 0 ); # fail job
+
+ ok not $manager->find_job;
+
+ sleep(5);
+
+ stdout_is( sub {$manager->work_once}, 1 ); # check job
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+
+sub max_retries { 1 }
+sub retry_delay { 5 } # 5 sec retry wait.
+sub grab_for { 0 }
+sub work {
+ my ($class, $job) = @_;
+
+ print STDOUT $job->retry_cnt;
+}
View
31 t/004worker/run_after.t
@@ -0,0 +1,31 @@
+use Qudo::Test;
+use Mock::Qudo::Driver::DBI;
+use Test::More;
+use Test::Output;
+
+run_tests(2, sub {
+ my $driver = shift;
+ my $master = test_master(
+ driver_class => $driver,
+ );
+
+ my $manager = $master->manager;
+ $manager->can_do('Worker::Test');
+ $manager->enqueue("Worker::Test", { run_after => 5, arg => 'do job', uniqkey => 'uniqkey' });
+
+ ok not $manager->find_job;
+
+ sleep(5);
+
+ stdout_is( sub {$manager->work_once}, 'do job' );
+
+ teardown_dbs;
+});
+
+package Worker::Test;
+use base 'Qudo::Worker';
+sub work {
+ my ($class, $job) = @_;
+
+ print STDOUT $job->arg;
+}

0 comments on commit 3e073e6

Please sign in to comment.