Commit | Line | Data |
d8985b7d |
1 | use strict; |
2 | use warnings; |
3 | |
45b2dbae |
4 | use Test::More; |
e7d94e6a |
5 | use Test::Fatal; |
d8985b7d |
6 | use Test::Moose; |
380acf65 |
7 | use File::Temp qw(tempdir); |
8 | use File::Spec::Functions; |
9 | |
10 | my $dir = tempdir( CLEANUP => 1 ); |
d8985b7d |
11 | |
12 | BEGIN { |
13 | use_ok('MooseX::Daemonize::Core'); |
2ecc2ccb |
14 | use_ok('MooseX::Daemonize::Pid'); |
d8985b7d |
15 | } |
16 | |
d02fc704 |
17 | use constant DEBUG => 0; |
18 | |
380acf65 |
19 | $ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt'); |
20 | $ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt'); |
d8985b7d |
21 | |
22 | { |
23 | package MyFooDaemon; |
24 | use Moose; |
2ecc2ccb |
25 | |
d8985b7d |
26 | with 'MooseX::Daemonize::Core'; |
2ecc2ccb |
27 | |
d8985b7d |
28 | has 'daemon_pid' => (is => 'rw', isa => 'MooseX::Daemonize::Pid'); |
2ecc2ccb |
29 | |
d8985b7d |
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 | }; |
2ecc2ccb |
40 | |
d8985b7d |
41 | sub start { |
2ecc2ccb |
42 | my $self = shift; |
d8985b7d |
43 | # tell it to ignore zombies ... |
2ecc2ccb |
44 | $self->ignore_zombies( 1 ); |
45 | $self->no_double_fork( 1 ); |
46 | $self->daemonize; |
d8985b7d |
47 | return unless $self->is_daemon; |
48 | # change to our local dir |
49 | # so that we can debug easier |
380acf65 |
50 | chdir $dir; |
d8985b7d |
51 | # make it easy to find with ps |
52 | $0 = 'test-app'; |
2ecc2ccb |
53 | $SIG{INT} = sub { |
54 | print "Got INT! Oh Noes!"; |
d8985b7d |
55 | exit; |
2ecc2ccb |
56 | }; |
d8985b7d |
57 | while (1) { |
2ecc2ccb |
58 | print "Hello from $$\n"; |
59 | sleep(10); |
d8985b7d |
60 | } |
61 | exit; |
62 | } |
63 | } |
64 | |
65 | my $d = MyFooDaemon->new; |
66 | isa_ok($d, 'MyFooDaemon'); |
67 | does_ok($d, 'MooseX::Daemonize::Core'); |
68 | |
e7d94e6a |
69 | is( |
70 | exception { $d->start }, |
71 | undef, |
72 | '... successfully daemonized from (' . $$ . ')', |
73 | ); |
d8985b7d |
74 | |
75 | my $p = $d->daemon_pid; |
76 | isa_ok($p, 'MooseX::Daemonize::Pid'); |
77 | |
78 | ok($p->is_running, '... the daemon process is running (' . $p->pid . ')'); |
79 | |
80 | my $pid = $p->pid; |
d02fc704 |
81 | if (DEBUG) { |
82 | diag `ps $pid`; |
83 | diag "-------"; |
84 | diag `ps -x | grep test-app`; |
85 | diag "-------"; |
86 | diag "killing $pid"; |
87 | } |
d8985b7d |
88 | kill INT => $p->pid; |
d02fc704 |
89 | diag "killed $pid" if DEBUG; |
d8985b7d |
90 | sleep(2); |
d02fc704 |
91 | if (DEBUG) { |
92 | diag `ps $pid`; |
93 | diag "-------"; |
94 | diag `ps -x | grep test-app`; |
95 | } |
d8985b7d |
96 | |
97 | ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')'); |
98 | |
fe0eeebc |
99 | unlink $ENV{MX_DAEMON_STDOUT}; |
100 | unlink $ENV{MX_DAEMON_STDERR}; |
101 | |
45b2dbae |
102 | done_testing; |
d8985b7d |
103 | |