t/lib/Daemon.t: Make the timing tests slightly more forgiving for slow machines

This commit is contained in:
Brian Fraser fraserb@gmail.com
2012-06-06 11:40:03 -03:00
parent 1a40d1301f
commit 85e9996b55

View File

@@ -9,8 +9,9 @@ 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 => 21; use Test::More tests => 22;
use Time::HiRes qw(sleep); use Time::HiRes qw(sleep);
use File::Temp qw( tempfile );
use Daemon; use Daemon;
use OptionParser; use OptionParser;
use PerconaTest; use PerconaTest;
@@ -23,7 +24,8 @@ 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() { sub rm_tmp_files() {
`rm $pid_file $log_file >/dev/null 2>&1` 1 while unlink $pid_file;
1 while unlink $log_file;
} }
# ############################################################################ # ############################################################################
@@ -32,21 +34,22 @@ sub rm_tmp_files() {
rm_tmp_files(); 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 >/dev/null 2>&1"); my $ret_val = system("$cmd 5 --daemonize --pid $pid_file >/dev/null 2>&1");
die 'Cannot test Daemon.pm because t/daemonizes.pl is not working' die 'Cannot test Daemon.pm because t/daemonizes.pl is not working'
unless $ret_val == 0; unless $ret_val == 0;
PerconaTest::wait_for_files($pid_file); PerconaTest::wait_for_files($pid_file);
my $output = `ps wx | grep '$cmd 2' | grep -v grep`; my $output = `ps wx | grep '$cmd 5' | grep -v grep`;
like($output, qr/$cmd/, 'Daemonizes'); like($output, qr/$cmd/, 'Daemonizes');
ok(-f $pid_file, 'Creates PID file'); ok(-f $pid_file, 'Creates PID file');
my ($pid) = $output =~ /\s*(\d+)\s+/; my ($pid) = $output =~ /\s*(\d+)\s+/;
$output = `cat $pid_file`; $output = slurp_file($pid_file);
is($output, $pid, 'PID file has correct PID'); is($output, $pid, 'PID file has correct PID');
sleep 2; # Wait until the process goes away
PerconaTest::wait_until(sub { !kill(0, $pid) });
ok(! -f $pid_file, 'Removes PID file upon exit'); ok(! -f $pid_file, 'Removes PID file upon exit');
# ############################################################################ # ############################################################################
@@ -58,7 +61,7 @@ system("$cmd 0 --daemonize --log $log_file");
PerconaTest::wait_for_files($log_file); PerconaTest::wait_for_files($log_file);
ok(-f $log_file, 'Log file exists'); ok(-f $log_file, 'Log file exists');
$output = `cat $log_file`; $output = slurp_file($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');
my $log_size = -s $log_file; my $log_size = -s $log_file;
@@ -67,7 +70,7 @@ PTDEVDEBUG && PerconaTest::_d('log size', $log_size);
# Check that the log file is appended to. # Check that the log file is appended to.
system("$cmd 0 --daemonize --log $log_file"); system("$cmd 0 --daemonize --log $log_file");
PerconaTest::wait_until(sub { -s $log_file > $log_size }); PerconaTest::wait_until(sub { -s $log_file > $log_size });
$output = `cat $log_file`; $output = slurp_file($log_file);
like( like(
$output, $output,
qr/STDOUT\nSTDERR\nSTDOUT\nSTDERR\n/, qr/STDOUT\nSTDERR\nSTDOUT\nSTDERR\n/,
@@ -106,9 +109,9 @@ SKIP: {
skip 'No /proc', 1 unless -d '/proc'; skip 'No /proc', 1 unless -d '/proc';
skip 'No fd in /proc', 1 unless -l "/proc/$PID/0" || -l "/proc/$PID/fd/0"; skip 'No fd in /proc', 1 unless -l "/proc/$PID/0" || -l "/proc/$PID/fd/0";
system("$cmd 1 --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);
chomp($pid = `cat $pid_file`); chomp($pid = slurp_file($pid_file));
my $proc_fd_0 = -l "/proc/$pid/0" ? "/proc/$pid/0" my $proc_fd_0 = -l "/proc/$pid/0" ? "/proc/$pid/0"
: -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0" : -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0"
: die "Cannot find fd 0 symlink in /proc/$pid"; : die "Cannot find fd 0 symlink in /proc/$pid";
@@ -121,22 +124,25 @@ SKIP: {
'Reopens STDIN to /dev/null' 'Reopens STDIN to /dev/null'
); );
# sleep 1; TODO: {
# system("echo foo | $cmd 1 --daemonize --pid $pid_file --log $log_file"); local $::TODO = "?";
# PerconaTest::wait_for_files($pid_file, $log_file); rm_tmp_files();
# chomp($pid = `cat $pid_file`); system("echo foo | $cmd 5 --daemonize --pid $pid_file --log $log_file");
# $proc_fd_0 = -l "/proc/$pid/0" ? "/proc/$pid/0" PerconaTest::wait_for_files($pid_file, $log_file);
# : -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0" chomp($pid = slurp_file($pid_file));
# : die "Cannot find fd 0 symlink in /proc/$pid"; $proc_fd_0 = -l "/proc/$pid/0" ? "/proc/$pid/0"
# PTDEVDEBUG && PerconaTest::_d('pid_file', $pid_file, : -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0"
# 'pid', $pid, 'proc_fd_0', $proc_fd_0, `ls -l $proc_fd_0`); : die "Cannot find fd 0 symlink in /proc/$pid";
# $stdin = readlink $proc_fd_0; PTDEVDEBUG && PerconaTest::_d('pid_file', $pid_file,
# like( 'pid', $pid, 'proc_fd_0', $proc_fd_0, `ls -l $proc_fd_0`);
# $stdin, $stdin = readlink $proc_fd_0;
# qr/pipe/, like(
# 'Does not reopen STDIN to /dev/null when piped', $stdin,
# ); qr/pipe/,
sleep 1; 'Does not reopen STDIN to /dev/null when piped',
);
rm_tmp_files();
}
}; };
# ########################################################################## # ##########################################################################
@@ -146,7 +152,7 @@ SKIP: {
rm_tmp_files(); rm_tmp_files();
system("$cmd 5 --daemonize --pid $pid_file >/dev/null 2>&1"); system("$cmd 5 --daemonize --pid $pid_file >/dev/null 2>&1");
PerconaTest::wait_for_files($pid_file); PerconaTest::wait_for_files($pid_file);
chomp($pid = `cat $pid_file`); chomp($pid = slurp_file($pid_file));
kill 9, $pid; kill 9, $pid;
sleep 0.25; sleep 0.25;
$output = `ps wx | grep '^[ ]*$pid' | grep -v grep`; $output = `ps wx | grep '^[ ]*$pid' | grep -v grep`;
@@ -160,10 +166,12 @@ ok(
'PID file remains after kill 9 (issue 419)' 'PID file remains after kill 9 (issue 419)'
); );
system("$cmd 1 --daemonize --log $log_file --pid $pid_file 2>/tmp/pre-daemonizes"); my (undef, $tempfile) = tempfile();
system("$cmd 5 --daemonize --log $log_file --pid $pid_file 2>$tempfile");
PerconaTest::wait_for_files($log_file); PerconaTest::wait_for_files($log_file);
$output = `ps wx | grep '$cmd 1' | grep -v grep`; $output = `ps wx | grep '$cmd 5' | grep -v grep`;
chomp(my $new_pid = `cat $pid_file`); chomp(my $new_pid = slurp_file($pid_file));
sleep 1; sleep 1;
like( like(
$output, $output,
@@ -171,7 +179,7 @@ like(
'Runs when PID file exists but old process is dead (issue 419)' 'Runs when PID file exists but old process is dead (issue 419)'
); );
like( like(
`cat /tmp/pre-daemonizes`, slurp_file($tempfile),
qr/$pid, is not running/, qr/$pid, is not running/,
'Says that old PID is not running (issue 419)' 'Says that old PID is not running (issue 419)'
); );
@@ -189,9 +197,9 @@ 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(); rm_tmp_files();
system("$cmd 1 --daemonize --log $log_file --pid $pid_file"); system("$cmd 10 --daemonize --log $log_file --pid $pid_file");
PerconaTest::wait_for_files($pid_file, $log_file); PerconaTest::wait_for_files($pid_file, $log_file);
chomp($pid = `cat $pid_file`); chomp($pid = slurp_file($pid_file));
$output = `$cmd 0 --daemonize --pid $pid_file 2>&1`; $output = `$cmd 0 --daemonize --pid $pid_file 2>&1`;
like( like(
$output, $output,