740090f1eae4a94f7150fbb61eac4328dab25f0b
[gitmo/MooseX-Daemonize.git] / t / 31.with_pid_file_and_poe.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Cwd;
7 use File::Spec::Functions;
8
9 use Test::More;
10 use Test::Exception;
11 use Test::Moose;
12
13 BEGIN {
14     eval 'use POE::Kernel;';
15     plan skip_all => "POE required for this test" if $@;
16     plan no_plan => 1;
17     use_ok('MooseX::Daemonize::Core');
18     
19 }
20
21 use constant DEBUG => 0;
22
23 my $CWD                = Cwd::cwd;
24 my $PIDFILE            = catfile($CWD, 'test-app.pid');
25 $ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt');
26 $ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt');
27
28 {
29     package MyFooDaemon;
30     use Moose;
31     use POE;
32     
33     with 'MooseX::Daemonize::WithPidFile';
34          
35     sub init_pidfile {
36         MooseX::Daemonize::Pid::File->new( file => $PIDFILE )
37     }
38     
39     sub start {
40         my $self = shift;
41         
42         # this tests our bad PID 
43         # cleanup functionality.
44         print "Our parent PID is " . $self->pidfile->pid . "\n" if ::DEBUG;
45         
46         $self->daemonize;
47         return unless $self->is_daemon;
48
49         my $session = POE::Session->create(
50           inline_states => {
51             say_hello => sub { 
52               my ($kernel, $heap) = @_[KERNEL, HEAP];
53
54               print "Hello from $$\n";
55
56               $heap->[0] = $kernel->delay_set('say_hello', 10);
57             },
58             _start => sub {
59               my ($kernel, $heap) = @_[KERNEL, HEAP];
60               $kernel->sig( INT => 'terminate');
61
62               $kernel->yield('say_hello');
63             },
64             terminate => sub {
65               my ($kernel, $heap) = @_[KERNEL, HEAP];
66             }
67           },
68           heap => [ 0 ]
69         );
70         
71         
72         # make it easy to find with ps
73         $0 = 'test-app-2';
74         POE::Kernel->run;
75         exit;
76     }
77 }
78
79 my $d = MyFooDaemon->new( pidfile => $PIDFILE );
80 isa_ok($d, 'MyFooDaemon');
81 does_ok($d, 'MooseX::Daemonize::Core');
82 does_ok($d, 'MooseX::Daemonize::WithPidFile');
83
84 ok($d->has_pidfile, '... we have a pidfile value');
85
86 {
87     my $p = $d->pidfile;
88     isa_ok($p, 'MooseX::Daemonize::Pid::File');
89     #diag $p->dump;
90 }
91
92 ok(!(-e $PIDFILE), '... the PID file does not exist yet');
93
94 lives_ok {
95     $d->start;
96 } '... successfully daemonized from (' . $$ . ')';
97
98 my $p = $d->pidfile;
99 isa_ok($p, 'MooseX::Daemonize::Pid::File');
100 #diag $p->dump;
101
102 sleep(2);
103
104 ok($p->does_file_exist, '... the PID file exists');
105 ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
106
107 my $pid = $p->pid;
108 if (DEBUG) {
109     diag `ps $pid`;
110     diag "-------";
111     diag `ps -x | grep test-app`;
112     diag "-------";
113     diag "killing $pid";
114 }
115 kill INT => $p->pid;
116 diag "killed $pid" if DEBUG;
117 sleep(2);
118 if (DEBUG) {
119     diag `ps $pid`;
120     diag "-------";
121     diag `ps -x | grep test-app`;
122 }
123
124 ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
125 ok(!(-e $PIDFILE), '... the PID file has been removed');
126
127 unlink $ENV{MX_DAEMON_STDOUT};
128 unlink $ENV{MX_DAEMON_STDERR};