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