whitespace fixes
[gitmo/MooseX-Daemonize.git] / t / 30.with_pid_file.t
CommitLineData
8ac4733f 1use strict;
2use warnings;
3
8ac4733f 4use File::Spec::Functions;
5
45b2dbae 6use Test::More;
e7d94e6a 7use Test::Fatal;
8ac4733f 8use Test::Moose;
380acf65 9use File::Temp qw(tempdir);
10
11my $dir = tempdir( CLEANUP => 1 );
12
8ac4733f 13
14BEGIN {
f3372ec9 15 use_ok('MooseX::Daemonize::Core');
8ac4733f 16}
17
d02fc704 18use constant DEBUG => 0;
19
380acf65 20my $PIDFILE = catfile($dir, 'test-app.pid');
21$ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt');
22$ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
8ac4733f 23
24{
25 package MyFooDaemon;
26 use Moose;
f3372ec9 27
d02fc704 28 with 'MooseX::Daemonize::WithPidFile';
f3372ec9 29
8ac4733f 30 sub init_pidfile {
31 MooseX::Daemonize::Pid::File->new( file => $PIDFILE )
32 }
f3372ec9 33
8ac4733f 34 sub start {
35 my $self = shift;
f3372ec9 36
37 # this tests our bad PID
d02fc704 38 # cleanup functionality.
92cf56b7 39 print "Our parent PID is " . $self->pidfile->pid . "\n" if ::DEBUG;
f3372ec9 40
8ac4733f 41 $self->daemonize;
42 return unless $self->is_daemon;
f3372ec9 43
8ac4733f 44 # make it easy to find with ps
92cf56b7 45 $0 = 'test-app-2';
f3372ec9 46 $SIG{INT} = sub {
47 print "Got INT! Oh Noes!";
8ac4733f 48 $self->pidfile->remove;
49 exit;
f3372ec9 50 };
8ac4733f 51 while (1) {
f3372ec9 52 print "Hello from $$\n";
53 sleep(10);
8ac4733f 54 }
55 exit;
56 }
57}
58
59my $d = MyFooDaemon->new( pidfile => $PIDFILE );
60isa_ok($d, 'MyFooDaemon');
61does_ok($d, 'MooseX::Daemonize::Core');
62does_ok($d, 'MooseX::Daemonize::WithPidFile');
63
64ok($d->has_pidfile, '... we have a pidfile value');
65
66{
67 my $p = $d->pidfile;
68 isa_ok($p, 'MooseX::Daemonize::Pid::File');
69 #diag $p->dump;
70}
71
72ok(!(-e $PIDFILE), '... the PID file does not exist yet');
73
e7d94e6a 74is(
75 exception { $d->start },
76 undef,
77 '... successfully daemonized from (' . $$ . ')',
78);
8ac4733f 79
80my $p = $d->pidfile;
81isa_ok($p, 'MooseX::Daemonize::Pid::File');
82#diag $p->dump;
83
84sleep(2);
85
86ok($p->does_file_exist, '... the PID file exists');
87ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
88
89my $pid = $p->pid;
d02fc704 90if (DEBUG) {
91 diag `ps $pid`;
92 diag "-------";
93 diag `ps -x | grep test-app`;
94 diag "-------";
95 diag "killing $pid";
96}
8ac4733f 97kill INT => $p->pid;
d02fc704 98diag "killed $pid" if DEBUG;
1474b8d3 99
100# give the process time to be killed on slow/loaded systems
101for (1..10) {
102 last unless kill 0 => $pid;
103 # sleep a little before retrying
104 sleep(2);
105}
106
107
d02fc704 108if (DEBUG) {
109 diag `ps $pid`;
110 diag "-------";
111 diag `ps -x | grep test-app`;
112}
8ac4733f 113
114ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
115ok(!(-e $PIDFILE), '... the PID file has been removed');
116
fe0eeebc 117unlink $ENV{MX_DAEMON_STDOUT};
118unlink $ENV{MX_DAEMON_STDERR};
45b2dbae 119
120done_testing;
121