AnyEvent-DBI
view release on metacpan or search on metacpan
t/01_fake_mysql.t view on Meta::CPAN
my $ran = 0;
my $fin = 0;
my $errs = [];
while ($ran++ < $run) {
$dbh->exec (
"select d,e,f,g from rows5 where num=?", 10, sub {
my ($dbh,$rows, $metadata) = @_;
if (!$rows) {
push @$errs, $@;
}
if (++$fin == $run) {
$cv->send();
}
}
);
}
$cv->recv();
ok(AnyEvent->now -$start < 0.0001,'invalid db handle returns from multiple queries immediately');
is (scalar @$errs, 10, 'invalid db handle returned error for all enqueued queries');
is($errs->[0],'no database connection','invalid db handle returns correct error');
undef $dbh;
# check for server process leakage
eval {
require Proc::Exists;
import Proc::Exists qw(pexists);
};
my $has_pe = ! $@;
SKIP: {
skip ( 'This test requires Proc::Exists',4) unless $has_pe;
# connect three handles
$cv = AnyEvent->condvar;
my @handles;
my @handle_errors;
my $connected =0;
for (0..2) {
my $dbh3 = new AnyEvent::DBI(
"dbi:mysql:database=database;host=127.0.0.1;port=23306",'','',
PrintError => 0,
timeout => 2,
on_error => sub { },
on_connect => sub {
if (!$_[1]) {
push @handle_errors, $@;
}
if (++$connected == 3) {
$cv->send();
}
},
);
push @handles, $dbh3;
}
$cv->recv();
is(scalar @handles,3,'created three handles');
is(scalar @handle_errors,0,'no errors during handle creation');
my @pids = map {$_->_server_pid} @handles;
ok( defined pexists(@pids, {all=>1}),'Found three slave processes');
undef @handles;
$cv = AnyEvent->condvar;
my $cleanup = AnyEvent->timer(after=>0.5,cb=>sub {$cv->send()});
$cv->recv();
ok(!defined pexists(@pids, {any=>1}),'All slave processes exited');
}
# connect to the server again
$cv = AnyEvent->condvar;
$dbh = new AnyEvent::DBI(
"dbi:mysql:database=database;host=127.0.0.1;port=23306",'','',
PrintError => 0,
timeout => 2,
on_error => sub { },
on_connect => sub {
if (!$_[1]) {
$cv->send($@);
} else {
$cv->send();
}
},
);
$connect_error = $cv->recv();
is($connect_error,undef,'on_connect() called without error, fake mysql server is re-connected');
# End the server and reap it
$cv = AnyEvent->condvar;
my $server_process_watcher = AnyEvent->child(
pid => $server_pid,
cb => sub {
$cv->send(@_);
}
);
kill 2, $server_pid; # 2 is SIGINT, usually
my ($dead_pid,$dead_status)=$cv->recv();
is ($dead_pid,$server_pid,'MySQL Server processess exited');
is ($dead_status,2,'Server exited on our signal');
if (0) {
# does not seem tor eliably kill all children
sleep 2;
# try to make another query with a down MYSQL server
# issue a query
$cv = AnyEvent->condvar;
$dbh->exec (
"select x from rows1 where num=?", 10, sub {
my ($dbh, $rows, $metadata) = @_;
if (!$rows) {
$cv->send($@);
} else {
$cv->send(undef,$rows);
}
}
);
($error, $rows) = $cv->recv();
is($error,'timeout','mysql query to dead server times out');
undef $dbh;
}
END {
if ($server_pid) {
( run in 3.693 seconds using v1.01-cache-2.11-cpan-98e64b0badf )