App-Basis-Queue
view release on metacpan or search on metacpan
t/01_tasks.t view on Meta::CPAN
}
# ----------------------------------------------------------------------------
# process and pass queue item after a small sleep
sub pass_delay_item
{
state $count ;
$count = 1 + ( ( $count++ ) % 3 ) ; # delay 1, 2, 3 seconds
my ( $self, $qname, $record ) = @_ ;
sleep($count) ;
return 1 ;
}
# ----------------------------------------------------------------------------
# process and fail queue item
sub fail_item
{
my ( $self, $qname, $record, $params ) = @_ ;
return 0 ;
}
# ----------------------------------------------------------------------------
# move an item onto another queue
sub move_q2
{
my ( $self, $qname, $record, $params ) = @_ ;
$self->delete_record($record) ;
$self->add( queue => $params->{queue}, data => $record->{data} ) ;
return 1 ;
}
# ----------------------------------------------------------------------------
# reset an item on the queue
sub reset_q2
{
my ( $self, $qname, $record ) = @_ ;
$self->reset_record($record) ;
return 1 ;
}
# ----------------------------------------------------------------------------
# Testing starts here
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# author testing can use sql, automated CPAN testing may not be able to
if ( $ENV{AUTHOR_TESTING} ) {
# $add_items = ( $add_items / 10 ) if ( $dsn =~ /:SQLite:/ );
# set PrintError off otherwise it will tell us that tables do not exist, we know that!
$dbh
= DBI->connect( $dsn, $user, $passwd,
{ RaiseError => 1, PrintError => 0, AutoCommit => 1 } )
or die "Could not connect to DB $dsn" ;
note "Testing against $dsn" ;
if ( $dsn =~ /:SQLite:/ ) {
# $dbh->do("PRAGMA journal_mode = WAL");
# $dbh->do("PRAGMA synchronous = NORMAL");
}
# -----------------------------------------------------------------------------
subtest "check clean start\n" => sub {
# remove all entries from the tables to make sure we are starting clean
my $table_name = $test_q . "_queue" ;
my ( $ret, $err ) = query_db( $dbh, "DROP TABLE $table_name;" ) ;
# check the table does not exist before we start
( $ret, $err )
= query_db( $dbh, "SELECT * from $table_name LIMIT 1;" ) ;
ok( !$ret && !$err, "Table $table_name does not exist" ) ;
} ;
# -----------------------------------------------------------------------------
subtest "create queue object\n" => sub {
$queue = App::Basis::Queue->new(
dbh => $dbh,
prefix => $test_q,
debug => $ENV{DEBUG}
) ;
isa_ok( $queue, 'App::Basis::Queue' ) ;
# new should have created the various database tables, lets check if this is the case
# we know that the tables will start with $test_q and the be 'queue_names' and 'queue_info'
my $table_name = $test_q . "_queue" ;
my ( $ret, $err )
= query_db( $dbh, "SELECT * from $table_name LIMIT 1;" ) ;
ok( $ret, "Table $table_name exists" ) ;
my $queue_list = $queue->list_queues() ;
ok( !scalar(@$queue_list), 'No queues listed' ) ;
} ;
# -----------------------------------------------------------------------------
subtest "adding to queue\n" => sub {
my ( $stats, $old_stats ) ;
# add 1 thing to the queue
my $resp = $queue->add(
queue => $qname,
data => { number => 17, desc => "test data" }
) ;
ok( $resp, "added 1 item to $qname" ) ;
my $count = 0 ;
note "delay processing" ;
foreach my $i ( 1 .. 10 ) {
my $resp = $queue->add(
queue => $qname,
data => { number => $i, desc => "test data", delay => 3 }
) ;
$count++ if ($resp) ;
}
ok( $count == 10, '10 items add to queue ' . $qname ) ;
my $queue_list = $queue->list_queues() ;
( run in 0.652 second using v1.01-cache-2.11-cpan-39bf76dae61 )