POE signal test
[gitmo/MooseX-Daemonize.git] / t / 31.with_pid_file_and_poe.t
CommitLineData
24b6fd77 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Cwd;
7use File::Spec::Functions;
8
9use Test::More;
10use Test::Exception;
11use Test::Moose;
12
13BEGIN {
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
21use constant DEBUG => 0;
22
23my $CWD = Cwd::cwd;
24my $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
79my $d = MyFooDaemon->new( pidfile => $PIDFILE );
80isa_ok($d, 'MyFooDaemon');
81does_ok($d, 'MooseX::Daemonize::Core');
82does_ok($d, 'MooseX::Daemonize::WithPidFile');
83
84ok($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
92ok(!(-e $PIDFILE), '... the PID file does not exist yet');
93
94lives_ok {
95 $d->start;
96} '... successfully daemonized from (' . $$ . ')';
97
98my $p = $d->pidfile;
99isa_ok($p, 'MooseX::Daemonize::Pid::File');
100#diag $p->dump;
101
102sleep(2);
103
104ok($p->does_file_exist, '... the PID file exists');
105ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
106
107my $pid = $p->pid;
108if (DEBUG) {
109 diag `ps $pid`;
110 diag "-------";
111 diag `ps -x | grep test-app`;
112 diag "-------";
113 diag "killing $pid";
114}
115kill INT => $p->pid;
116diag "killed $pid" if DEBUG;
117sleep(2);
118if (DEBUG) {
119 diag `ps $pid`;
120 diag "-------";
121 diag `ps -x | grep test-app`;
122}
123
124ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
125ok(!(-e $PIDFILE), '... the PID file has been removed');
126
127unlink $ENV{MX_DAEMON_STDOUT};
128unlink $ENV{MX_DAEMON_STDERR};