Handle slurp_file error specifically. Fix Daemon.t test plans.

This commit is contained in:
Daniel Nichter
2012-10-30 16:57:06 -06:00
parent a0a42eebe8
commit c8b31431b0
2 changed files with 16 additions and 8 deletions

View File

@@ -125,8 +125,15 @@ sub check_PID_file {
PTDEBUG && _d('Checking PID file', $PID_file); PTDEBUG && _d('Checking PID file', $PID_file);
if ( $PID_file && -f $PID_file ) { if ( $PID_file && -f $PID_file ) {
my $pid; my $pid;
eval { chomp($pid = slurp_file($PID_file)); }; eval {
die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; chomp($pid = (slurp_file($PID_file) || ''));
};
if ( $EVAL_ERROR ) {
# Be safe and die if we can't check that a process is
# or is not already running.
die "The PID file $PID_file already exists but it cannot be read: "
. $EVAL_ERROR;
}
PTDEBUG && _d('PID file exists; it contains PID', $pid); PTDEBUG && _d('PID file exists; it contains PID', $pid);
if ( $pid ) { if ( $pid ) {
my $pid_is_alive = kill 0, $pid; my $pid_is_alive = kill 0, $pid;
@@ -223,7 +230,8 @@ sub DESTROY {
sub slurp_file { sub slurp_file {
my ($file) = @_; my ($file) = @_;
open my $fh, "<", $file or die "Couldn't slurp file: $!"; return unless $file;
open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR";
return do { local $/; <$fh> }; return do { local $/; <$fh> };
} }

View File

@@ -9,7 +9,7 @@ BEGIN {
use strict; use strict;
use warnings FATAL => 'all'; use warnings FATAL => 'all';
use English qw(-no_match_vars); use English qw(-no_match_vars);
use Test::More tests => 22; use Test::More;
use Time::HiRes qw(sleep); use Time::HiRes qw(sleep);
use File::Temp qw( tempfile ); use File::Temp qw( tempfile );
use Daemon; use Daemon;
@@ -106,8 +106,8 @@ unlike(
# ########################################################################## # ##########################################################################
rm_tmp_files(); rm_tmp_files();
SKIP: { SKIP: {
skip 'No /proc', 1 unless -d '/proc'; skip 'No /proc', 2 unless -d '/proc';
skip 'No fd in /proc', 1 unless -l "/proc/$PID/0" || -l "/proc/$PID/fd/0"; skip 'No fd in /proc', 2 unless -l "/proc/$PID/0" || -l "/proc/$PID/fd/0";
system("$cmd 5 --daemonize --pid $pid_file --log $log_file"); system("$cmd 5 --daemonize --pid $pid_file --log $log_file");
PerconaTest::wait_for_files($pid_file); PerconaTest::wait_for_files($pid_file);
@@ -253,11 +253,11 @@ ok(
'Dies if PID file already exists for non-daemon' 'Dies if PID file already exists for non-daemon'
); );
`rm -rf /tmp/d2.pid`; diag(`rm -rf /tmp/d2.pid >/dev/null`);
} }
# ############################################################################# # #############################################################################
# Done. # Done.
# ############################################################################# # #############################################################################
rm_tmp_files(); rm_tmp_files();
exit; done_testing;