Commit | Line | Data |
24b6fd77 |
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 | |
b38ab84f |
28 | unlink $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 | |
82 | my $d = MyFooDaemon->new( pidfile => $PIDFILE ); |
83 | isa_ok($d, 'MyFooDaemon'); |
84 | does_ok($d, 'MooseX::Daemonize::Core'); |
85 | does_ok($d, 'MooseX::Daemonize::WithPidFile'); |
86 | |
87 | ok($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 | |
95 | ok(!(-e $PIDFILE), '... the PID file does not exist yet'); |
96 | |
97 | lives_ok { |
98 | $d->start; |
99 | } '... successfully daemonized from (' . $$ . ')'; |
100 | |
101 | my $p = $d->pidfile; |
102 | isa_ok($p, 'MooseX::Daemonize::Pid::File'); |
103 | #diag $p->dump; |
104 | |
105 | sleep(2); |
106 | |
107 | ok($p->does_file_exist, '... the PID file exists'); |
108 | ok($p->is_running, '... the daemon process is running (' . $p->pid . ')'); |
109 | |
110 | my $pid = $p->pid; |
111 | if (DEBUG) { |
112 | diag `ps $pid`; |
113 | diag "-------"; |
114 | diag `ps -x | grep test-app`; |
115 | diag "-------"; |
116 | diag "killing $pid"; |
117 | } |
118 | kill INT => $p->pid; |
119 | diag "killed $pid" if DEBUG; |
120 | sleep(2); |
121 | if (DEBUG) { |
122 | diag `ps $pid`; |
123 | diag "-------"; |
124 | diag `ps -x | grep test-app`; |
125 | } |
126 | |
127 | ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')'); |
128 | ok(!(-e $PIDFILE), '... the PID file has been removed'); |
129 | |
130 | unlink $ENV{MX_DAEMON_STDOUT}; |
131 | unlink $ENV{MX_DAEMON_STDERR}; |