Commit | Line | Data |
2ecc2ccb |
1 | use strict; |
2 | use warnings; |
3 | |
45b2dbae |
4 | use Test::More; |
e7d94e6a |
5 | use Test::Fatal; |
2ecc2ccb |
6 | use Test::Moose; |
7 | use File::Temp qw(tempdir); |
8 | use File::Spec::Functions; |
9 | |
10 | my $dir = tempdir( CLEANUP => 1 ); |
11 | |
12 | BEGIN { |
13 | use_ok('MooseX::Daemonize::Core'); |
14 | use_ok('MooseX::Daemonize::Pid'); |
15 | } |
16 | |
17 | use 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 | |
66 | my $d = MyFooDaemon->new; |
67 | isa_ok($d, 'MyFooDaemon'); |
68 | does_ok($d, 'MooseX::Daemonize::Core'); |
69 | |
e7d94e6a |
70 | is( |
71 | exception { $d->start }, |
72 | undef, |
73 | '... successfully daemonized from (' . $$ . ')', |
74 | ); |
2ecc2ccb |
75 | |
76 | my $p = $d->daemon_pid; |
77 | isa_ok($p, 'MooseX::Daemonize::Pid'); |
78 | |
79 | ok($p->is_running, '... the daemon process is running (' . $p->pid . ')'); |
80 | |
81 | my $pid = $p->pid; |
82 | if (DEBUG) { |
83 | diag `ps $pid`; |
84 | diag "-------"; |
85 | diag `ps -x | grep test-app`; |
86 | diag "-------"; |
87 | diag "killing $pid"; |
88 | } |
89 | kill INT => $p->pid; |
90 | diag "killed $pid" if DEBUG; |
1474b8d3 |
91 | |
92 | # give the process time to be killed on slow/loaded systems |
93 | for (1..10) { |
94 | last unless kill 0 => $pid; |
95 | # sleep a little before retrying |
96 | sleep(2); |
97 | } |
98 | |
2ecc2ccb |
99 | if (DEBUG) { |
100 | diag `ps $pid`; |
101 | diag "-------"; |
102 | diag `ps -x | grep test-app`; |
103 | } |
104 | |
105 | ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')'); |
106 | |
107 | unlink $ENV{MX_DAEMON_STDOUT}; |
108 | unlink $ENV{MX_DAEMON_STDERR}; |
109 | |
45b2dbae |
110 | done_testing; |
2ecc2ccb |
111 | |