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