s/no_plan => 1/'no_plan'/g
[gitmo/MooseX-Daemonize.git] / t / 31.with_pid_file_and_poe.t
CommitLineData
24b6fd77 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
24b6fd77 6use File::Spec::Functions;
7
8use Test::More;
9use Test::Exception;
10use Test::Moose;
11
23b6ee85 12use File::Temp qw(tempdir);
13
14my $dir = tempdir( CLEANUP => 1 );
15
24b6fd77 16BEGIN {
17 eval 'use POE::Kernel;';
18 plan skip_all => "POE required for this test" if $@;
10769ed3 19 plan 'no_plan';
24b6fd77 20 use_ok('MooseX::Daemonize::Core');
21
22}
23
24use constant DEBUG => 0;
25
23b6ee85 26my $PIDFILE = catfile($dir, 'test-app.pid');
27$ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt');
28$ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
24b6fd77 29
b38ab84f 30unlink $PIDFILE; # clean up anythinf leftover by last run
31
24b6fd77 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];
b38ab84f 70 $self->pidfile->remove if $self->pidfile->pid == $$;
24b6fd77 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
84my $d = MyFooDaemon->new( pidfile => $PIDFILE );
85isa_ok($d, 'MyFooDaemon');
86does_ok($d, 'MooseX::Daemonize::Core');
87does_ok($d, 'MooseX::Daemonize::WithPidFile');
88
89ok($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
97ok(!(-e $PIDFILE), '... the PID file does not exist yet');
98
99lives_ok {
100 $d->start;
101} '... successfully daemonized from (' . $$ . ')';
102
103my $p = $d->pidfile;
104isa_ok($p, 'MooseX::Daemonize::Pid::File');
105#diag $p->dump;
106
107sleep(2);
108
109ok($p->does_file_exist, '... the PID file exists');
110ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
111
112my $pid = $p->pid;
113if (DEBUG) {
114 diag `ps $pid`;
115 diag "-------";
116 diag `ps -x | grep test-app`;
117 diag "-------";
118 diag "killing $pid";
119}
120kill INT => $p->pid;
121diag "killed $pid" if DEBUG;
122sleep(2);
123if (DEBUG) {
124 diag `ps $pid`;
125 diag "-------";
126 diag `ps -x | grep test-app`;
127}
128
129ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
130ok(!(-e $PIDFILE), '... the PID file has been removed');
131
132unlink $ENV{MX_DAEMON_STDOUT};
133unlink $ENV{MX_DAEMON_STDERR};