10
10
use warnings FATAL => 'all';
11
11
use English qw(-no_match_vars);
13
14
use Time::HiRes qw(sleep);
14
use File::Temp qw( tempfile );
15
use File::Temp qw(tempfile);
18
#plan skip_all => "Hm";
19
20
use constant PTDEVDEBUG => $ENV{PTDEVDEBUG} || 0;
21
my $o = new OptionParser(file => "$trunk/t/lib/samples/daemonizes.pl");
22
my $d = new Daemon(o=>$o);
24
my $pid_file = '/tmp/daemonizes.pl.pid';
25
my $log_file = '/tmp/daemonizes.output';
22
my $cmd = "$trunk/t/lib/samples/daemonizes.pl";
23
my $pid_file = "/tmp/pt-daemon-test.pid.$PID";
24
my $log_file = "/tmp/pt-daemon-test.log.$PID";
26
25
sub rm_tmp_files() {
27
-e $pid_file && (unlink $pid_file || die "Error removing $pid_file");
28
-e $log_file && (unlink $log_file || die "Error removing $log_file");
26
-f $pid_file && (unlink $pid_file || die "Error removing $pid_file");
27
-f $log_file && (unlink $log_file || die "Error removing $log_file");
31
30
# ############################################################################
32
31
# Test that it daemonizes, creates a PID file, and removes that PID file.
33
32
# ############################################################################
36
my $cmd = "$trunk/t/lib/samples/daemonizes.pl";
37
34
my $ret_val = system("$cmd 5 --daemonize --pid $pid_file >/dev/null 2>&1");
38
35
die 'Cannot test Daemon.pm because t/daemonizes.pl is not working'
39
36
unless $ret_val == 0;
41
38
PerconaTest::wait_for_files($pid_file);
43
40
my $output = `ps wx | grep '$cmd 5' | grep -v grep`;
44
like($output, qr/$cmd/, 'Daemonizes');
45
ok(-f $pid_file, 'Creates PID file');
47
my ($pid) = $output =~ /\s*(\d+)\s+/;
53
my ($pid) = $output =~ /^\s*(\d+)\s+/;
48
54
$output = slurp_file($pid_file);
49
is($output, $pid, 'PID file has correct PID');
55
chomp($output) if $output;
60
'PID file has correct PID'
51
63
# Wait until the process goes away
52
64
PerconaTest::wait_until(sub { !kill(0, $pid) });
53
ok(! -f $pid_file, 'Removes PID file upon exit');
67
'Removes PID file upon exit'
55
70
# ############################################################################
56
71
# Check that STDOUT can be redirected
60
75
system("$cmd 0 --daemonize --log $log_file");
61
76
PerconaTest::wait_for_files($log_file);
62
ok(-f $log_file, 'Log file exists');
64
83
$output = slurp_file($log_file);
65
like($output, qr/STDOUT\nSTDERR\n/, 'STDOUT and STDERR went to log file');
88
'STDOUT and STDERR went to log file'
67
91
my $log_size = -s $log_file;
68
92
PTDEVDEBUG && PerconaTest::_d('log size', $log_size);
71
95
system("$cmd 0 --daemonize --log $log_file");
72
96
PerconaTest::wait_until(sub { -s $log_file > $log_size });
73
97
$output = slurp_file($log_file);
76
101
qr/STDOUT\nSTDERR\nSTDOUT\nSTDERR\n/,
90
116
$output = `$cmd 2 --daemonize --pid $pid_file 2>&1`;
93
qr{The PID file $pid_file already exists},
119
qr{PID file $pid_file exists},
94
120
'Dies if PID file already exists'
184
210
slurp_file($tempfile),
185
qr/$pid, is not running/,
211
qr/Overwriting PID file $pid_file because PID $pid is not running/,
186
212
'Says that old PID is not running (issue 419)'
209
235
$output = `$cmd 0 --daemonize --pid $pid_file 2>&1`;
212
qr/$pid, is running/,
238
qr/PID file $pid_file exists and PID $pid is running/,
213
239
'Says that PID is running (issue 419)'
222
249
# #############################################################################
223
250
# Test auto-PID file removal without having to daemonize (for issue 391).
224
251
# #############################################################################
252
my $pid_file2 = "/tmp/pt-daemon-test.pid2.$PID";
226
@ARGV = qw(--pid /tmp/d2.pid);
227
$o->get_specs("$trunk/t/lib/samples/daemonizes.pl");
229
my $d2 = new Daemon(o=>$o);
230
$d2->make_PID_file();
254
my $d2 = Daemon->new(
255
pid_file => $pid_file2,
233
260
'PID file for non-daemon exists'
236
263
# Since $d2 was locally scoped, it should have been destoryed by now.
237
264
# This should have caused the PID file to be automatically removed.
240
267
'PID file auto-removed for non-daemon'
243
270
# We should still die if the PID file already exists,
244
271
# even if we're not a daemon.
247
@ARGV = qw(--pid /tmp/d2.pid);
273
diag(`touch $pid_file2`);
250
my $d2 = new Daemon(o=>$o); # should die here actually
251
$d2->make_PID_file();
275
my $d2 = Daemon->new(
276
pid_file => $pid_file2,
255
qr{PID file /tmp/d2.pid already exists},
282
qr/PID file $pid_file2 exists/,
256
283
'Dies if PID file already exists for non-daemon'
259
diag(`rm -rf /tmp/d2.pid >/dev/null`);
286
unlink $pid_file2 if -f $pid_file2;
262
289
# #############################################################################