repository moved to https://github.com/moose/MooseX-Daemonize
[gitmo/MooseX-Daemonize.git] / t / 21.core-back-compat.t
CommitLineData
2ecc2ccb 1use strict;
2use warnings;
3
45b2dbae 4use Test::More;
e7d94e6a 5use Test::Fatal;
2ecc2ccb 6use Test::Moose;
7use File::Temp qw(tempdir);
8use File::Spec::Functions;
9
10my $dir = tempdir( CLEANUP => 1 );
11
12BEGIN {
13 use_ok('MooseX::Daemonize::Core');
14 use_ok('MooseX::Daemonize::Pid');
15}
16
17use constant DEBUG => 0;
18
19$ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt');
20$ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
21
22{
23 package MyFooDaemon;
24 use Moose;
25
26 with 'MooseX::Daemonize::Core';
27
28 has 'daemon_pid' => (is => 'rw', isa => 'MooseX::Daemonize::Pid');
29
30 # capture the PID from the fork
31 around 'daemon_fork' => sub {
32 my $next = shift;
33 my $self = shift;
34 if (my $pid = $self->$next(@_)) {
35 $self->daemon_pid(
36 MooseX::Daemonize::Pid->new(pid => $pid)
37 );
38 }
39 };
40
41 sub start {
42 my $self = shift;
43 # tell it to ignore zombies ...
44 $self->daemonize(
45 ignore_zombies => 1,
46 no_double_fork => 1,
47 );
48 return unless $self->is_daemon;
49 # change to our local dir
50 # so that we can debug easier
51 chdir $dir;
52 # make it easy to find with ps
53 $0 = 'test-app';
54 $SIG{INT} = sub {
55 print "Got INT! Oh Noes!";
56 exit;
57 };
58 while (1) {
59 print "Hello from $$\n";
60 sleep(10);
61 }
62 exit;
63 }
64}
65
66my $d = MyFooDaemon->new;
67isa_ok($d, 'MyFooDaemon');
68does_ok($d, 'MooseX::Daemonize::Core');
69
e7d94e6a 70is(
71 exception { $d->start },
72 undef,
73 '... successfully daemonized from (' . $$ . ')',
74);
2ecc2ccb 75
76my $p = $d->daemon_pid;
77isa_ok($p, 'MooseX::Daemonize::Pid');
78
79ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
80
81my $pid = $p->pid;
82if (DEBUG) {
83 diag `ps $pid`;
84 diag "-------";
85 diag `ps -x | grep test-app`;
86 diag "-------";
87 diag "killing $pid";
88}
89kill INT => $p->pid;
90diag "killed $pid" if DEBUG;
1474b8d3 91
92# give the process time to be killed on slow/loaded systems
93for (1..10) {
94 last unless kill 0 => $pid;
95 # sleep a little before retrying
96 sleep(2);
97}
98
2ecc2ccb 99if (DEBUG) {
100 diag `ps $pid`;
101 diag "-------";
102 diag `ps -x | grep test-app`;
103}
104
105ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
106
107unlink $ENV{MX_DAEMON_STDOUT};
108unlink $ENV{MX_DAEMON_STDERR};
109
45b2dbae 110done_testing;
2ecc2ccb 111