Try to make Daemon.t more reliable.

This commit is contained in:
Daniel Nichter
2011-08-24 11:04:47 -06:00
parent f9bfb641ca
commit e134aa665b

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 => 23; use Test::More tests => 22;
use Daemon; use Daemon;
use OptionParser; use OptionParser;
@@ -20,14 +20,19 @@ my $d = new Daemon(o=>$o);
my $pid_file = '/tmp/daemonizes.pl.pid'; my $pid_file = '/tmp/daemonizes.pl.pid';
my $log_file = '/tmp/daemonizes.output'; my $log_file = '/tmp/daemonizes.output';
sub rm_tmp_files() {
`rm $pid_file $log_file >/dev/null 2>&1`
}
isa_ok($d, 'Daemon'); # ############################################################################
# Test that it daemonizes, creates a PID file, and removes that PID file.
# ############################################################################
rm_tmp_files();
my $cmd = "$trunk/t/lib/samples/daemonizes.pl"; my $cmd = "$trunk/t/lib/samples/daemonizes.pl";
my $ret_val = system("$cmd 2 --daemonize --pid $pid_file"); my $ret_val = system("$cmd 2 --daemonize --pid $pid_file");
SKIP: { die 'Cannot test Daemon.pm because t/daemonizes.pl is not working'
skip 'Cannot test Daemon.pm because t/daemonizes.pl is not working', unless $ret_val == 0;
19 unless $ret_val == 0;
my $output = `ps wx | grep '$cmd 2' | grep -v grep`; my $output = `ps wx | grep '$cmd 2' | grep -v grep`;
like($output, qr/$cmd/, 'Daemonizes'); like($output, qr/$cmd/, 'Daemonizes');
@@ -40,28 +45,31 @@ SKIP: {
sleep 2; sleep 2;
ok(! -f $pid_file, 'Removes PID file upon exit'); ok(! -f $pid_file, 'Removes PID file upon exit');
# ############################################################################
# Check that STDOUT can be redirected # Check that STDOUT can be redirected
system("$cmd 2 --daemonize --log /tmp/mk-daemon.log"); # ############################################################################
ok(-f '/tmp/mk-daemon.log', 'Log file exists'); rm_tmp_files();
system("$cmd 2 --daemonize --log $log_file");
ok(-f $log_file, 'Log file exists');
sleep 2; sleep 2;
$output = `cat /tmp/mk-daemon.log`; $output = `cat $log_file`;
like($output, qr/STDOUT\nSTDERR\n/, 'STDOUT and STDERR went to log file'); like($output, qr/STDOUT\nSTDERR\n/, 'STDOUT and STDERR went to log file');
# Check that the log file is appended to. # Check that the log file is appended to.
system("$cmd 0 --daemonize --log /tmp/mk-daemon.log"); system("$cmd 0 --daemonize --log $log_file");
$output = `cat /tmp/mk-daemon.log`; $output = `cat $log_file`;
like( like(
$output, $output,
qr/STDOUT\nSTDERR\nSTDOUT\nSTDERR\n/, qr/STDOUT\nSTDERR\nSTDOUT\nSTDERR\n/,
'Appends to log file' 'Appends to log file'
); );
`rm -f /tmp/mk-daemon.log`;
# ########################################################################## # ##########################################################################
# Issue 383: mk-deadlock-logger should die if --pid file specified exists # Issue 383: mk-deadlock-logger should die if --pid file specified exists
# ########################################################################## # ##########################################################################
rm_tmp_files();
diag(`touch $pid_file`); diag(`touch $pid_file`);
ok( ok(
-f $pid_file, -f $pid_file,
@@ -71,7 +79,7 @@ SKIP: {
$output = `MKDEBUG=1 $cmd 0 --daemonize --pid $pid_file 2>&1`; $output = `MKDEBUG=1 $cmd 0 --daemonize --pid $pid_file 2>&1`;
like( like(
$output, $output,
qr{The PID file /tmp/daemonizes\.pl\.pid already exists}, qr{The PID file $pid_file already exists},
'Dies if PID file already exists' 'Dies if PID file already exists'
); );
@@ -82,11 +90,10 @@ SKIP: {
'Does not daemonizes' 'Does not daemonizes'
); );
diag(`rm -rf $pid_file`);
# ########################################################################## # ##########################################################################
# Issue 417: --daemonize doesn't let me log out of terminal cleanly # Issue 417: --daemonize doesn't let me log out of terminal cleanly
# ########################################################################## # ##########################################################################
rm_tmp_files();
SKIP: { SKIP: {
skip 'No /proc', 2 unless -d '/proc'; skip 'No /proc', 2 unless -d '/proc';
skip 'No fd in /proc', 2 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";
@@ -115,17 +122,18 @@ SKIP: {
qr/pipe/, qr/pipe/,
'Does not reopen STDIN to /dev/null when piped', 'Does not reopen STDIN to /dev/null when piped',
); );
sleep 1;
}; };
# ########################################################################## # ##########################################################################
# Issue 419: Daemon should check wether process with pid obtained from # Issue 419: Daemon should check wether process with pid obtained from
# pid-file is still running # pid-file is still running
# ########################################################################## # ##########################################################################
rm_tmp_files();
system("$cmd 5 --daemonize --pid $pid_file 2>&1"); system("$cmd 5 --daemonize --pid $pid_file 2>&1");
chomp($pid = `cat $pid_file`); chomp($pid = `cat $pid_file`);
kill 9, $pid; kill 9, $pid;
$output = `ps wax | grep $pid | grep -v grep`; $output = `ps wx | grep '^[ ]*$pid' | grep -v grep`;
unlike( unlike(
$output, $output,
qr/daemonize/, qr/daemonize/,
@@ -136,7 +144,6 @@ SKIP: {
'PID file remains after kill 9 (issue 419)' 'PID file remains after kill 9 (issue 419)'
); );
diag(`rm -rf $log_file`);
system("$cmd 1 --daemonize --log $log_file --pid $pid_file 2>/tmp/pre-daemonizes"); system("$cmd 1 --daemonize --log $log_file --pid $pid_file 2>/tmp/pre-daemonizes");
$output = `ps wx | grep '$cmd 1' | grep -v grep`; $output = `ps wx | grep '$cmd 1' | grep -v grep`;
chomp(my $new_pid = `cat $pid_file`); chomp(my $new_pid = `cat $pid_file`);
@@ -159,8 +166,12 @@ SKIP: {
!-f $pid_file, !-f $pid_file,
'Re-used PID file still removed (issue 419)' 'Re-used PID file still removed (issue 419)'
); );
diag(`rm -rf /tmp/pre-daemonizes`);
# ############################################################################
# Check that it actually checks the running process. # Check that it actually checks the running process.
# ############################################################################
rm_tmp_files();
system("$cmd 1 --daemonize --log $log_file --pid $pid_file"); system("$cmd 1 --daemonize --log $log_file --pid $pid_file");
chomp($pid = `cat $pid_file`); chomp($pid = `cat $pid_file`);
$output = `$cmd 0 --daemonize --pid $pid_file 2>&1`; $output = `$cmd 0 --daemonize --pid $pid_file 2>&1`;
@@ -171,12 +182,7 @@ SKIP: {
); );
sleep 1; sleep 1;
rm_tmp_files();
# Make sure PID file is gone to make subsequent tests happy.
diag(`rm -rf $pid_file`);
diag(`rm -rf $log_file`);
diag(`rm -rf /tmp/pre-daemonizes`);
}
# ############################################################################# # #############################################################################
# Test auto-PID file removal without having to daemonize (for issue 391). # Test auto-PID file removal without having to daemonize (for issue 391).
@@ -221,5 +227,5 @@ ok(
# ############################################################################# # #############################################################################
# Done. # Done.
# ############################################################################# # #############################################################################
diag(`rm -rf /tmp/daemonizes.*`); rm_tmp_files();
exit; exit;