Commit | Line | Data |
8ac4733f |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Cwd; |
7 | use File::Spec::Functions; |
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 | } |
16 | |
17 | my $CWD = Cwd::cwd; |
18 | my $PIDFILE = catfile($CWD, 'test-app.pid'); |
19 | $ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt'); |
20 | $ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt'); |
21 | |
22 | { |
23 | package MyFooDaemon; |
24 | use Moose; |
25 | |
26 | with 'MooseX::Daemonize::Core', |
27 | 'MooseX::Daemonize::WithPidFile'; |
28 | |
29 | sub init_pidfile { |
30 | MooseX::Daemonize::Pid::File->new( file => $PIDFILE ) |
31 | } |
32 | |
33 | sub start { |
34 | my $self = shift; |
35 | |
36 | $self->daemonize; |
37 | return unless $self->is_daemon; |
38 | |
39 | $self->pidfile->write; |
40 | |
41 | # make it easy to find with ps |
42 | $0 = 'test-app'; |
43 | $SIG{INT} = sub { |
44 | print "Got INT! Oh Noes!"; |
45 | $self->pidfile->remove; |
46 | exit; |
47 | }; |
48 | while (1) { |
49 | print "Hello from $$\n"; |
50 | sleep(10); |
51 | } |
52 | exit; |
53 | } |
54 | } |
55 | |
56 | my $d = MyFooDaemon->new( pidfile => $PIDFILE ); |
57 | isa_ok($d, 'MyFooDaemon'); |
58 | does_ok($d, 'MooseX::Daemonize::Core'); |
59 | does_ok($d, 'MooseX::Daemonize::WithPidFile'); |
60 | |
61 | ok($d->has_pidfile, '... we have a pidfile value'); |
62 | |
63 | { |
64 | my $p = $d->pidfile; |
65 | isa_ok($p, 'MooseX::Daemonize::Pid::File'); |
66 | #diag $p->dump; |
67 | } |
68 | |
69 | ok(!(-e $PIDFILE), '... the PID file does not exist yet'); |
70 | |
71 | lives_ok { |
72 | $d->start; |
73 | } '... successfully daemonized from (' . $$ . ')'; |
74 | |
75 | my $p = $d->pidfile; |
76 | isa_ok($p, 'MooseX::Daemonize::Pid::File'); |
77 | #diag $p->dump; |
78 | |
79 | sleep(2); |
80 | |
81 | ok($p->does_file_exist, '... the PID file exists'); |
82 | ok($p->is_running, '... the daemon process is running (' . $p->pid . ')'); |
83 | |
84 | my $pid = $p->pid; |
85 | diag `ps $pid`; |
86 | diag "-------"; |
87 | diag `ps -x | grep test-app`; |
88 | diag "-------"; |
89 | diag "killing $pid"; |
90 | kill INT => $p->pid; |
91 | diag "killed $pid"; |
92 | sleep(2); |
93 | diag `ps $pid`; |
94 | diag "-------"; |
95 | diag `ps -x | grep test-app`; |
96 | |
97 | ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')'); |
98 | ok(!(-e $PIDFILE), '... the PID file has been removed'); |
99 | |