Commit | Line | Data |
d8985b7d |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Cwd; |
8ac4733f |
7 | use File::Spec::Functions; |
d8985b7d |
8 | |
9 | use Test::More no_plan => 1; |
10 | use Test::Exception; |
11 | use Test::Moose; |
12 | |
13 | BEGIN { |
14 | use_ok('MooseX::Daemonize::Core'); |
15 | use_ok('MooseX::Daemonize::Pid'); |
16 | } |
17 | |
8ac4733f |
18 | my $CWD = Cwd::cwd; |
19 | $ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt'); |
20 | $ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt'); |
d8985b7d |
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 $CWD; |
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 | |
70 | lives_ok { |
71 | $d->start; |
72 | } '... successfully daemonized from (' . $$ . ')'; |
73 | |
74 | my $p = $d->daemon_pid; |
75 | isa_ok($p, 'MooseX::Daemonize::Pid'); |
76 | |
77 | ok($p->is_running, '... the daemon process is running (' . $p->pid . ')'); |
78 | |
79 | my $pid = $p->pid; |
8ac4733f |
80 | diag `ps $pid`; |
81 | diag "-------"; |
82 | diag `ps -x | grep test-app`; |
83 | diag "-------"; |
84 | diag "killing $pid"; |
d8985b7d |
85 | kill INT => $p->pid; |
8ac4733f |
86 | diag "killed $pid"; |
d8985b7d |
87 | sleep(2); |
8ac4733f |
88 | diag `ps $pid`; |
89 | diag "-------"; |
90 | diag `ps -x | grep test-app`; |
d8985b7d |
91 | |
92 | ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')'); |
93 | |
94 | |