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