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