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