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. |
39 | print "Our parent PID is " . $self->pidfile->pid . "\n"; |
40 | |
8ac4733f |
41 | $self->daemonize; |
42 | return unless $self->is_daemon; |
43 | |
44 | $self->pidfile->write; |
45 | |
46 | # make it easy to find with ps |
47 | $0 = 'test-app'; |
48 | $SIG{INT} = sub { |
49 | print "Got INT! Oh Noes!"; |
50 | $self->pidfile->remove; |
51 | exit; |
52 | }; |
53 | while (1) { |
54 | print "Hello from $$\n"; |
55 | sleep(10); |
56 | } |
57 | exit; |
58 | } |
59 | } |
60 | |
61 | my $d = MyFooDaemon->new( pidfile => $PIDFILE ); |
62 | isa_ok($d, 'MyFooDaemon'); |
63 | does_ok($d, 'MooseX::Daemonize::Core'); |
64 | does_ok($d, 'MooseX::Daemonize::WithPidFile'); |
65 | |
66 | ok($d->has_pidfile, '... we have a pidfile value'); |
67 | |
68 | { |
69 | my $p = $d->pidfile; |
70 | isa_ok($p, 'MooseX::Daemonize::Pid::File'); |
71 | #diag $p->dump; |
72 | } |
73 | |
74 | ok(!(-e $PIDFILE), '... the PID file does not exist yet'); |
75 | |
76 | lives_ok { |
77 | $d->start; |
78 | } '... successfully daemonized from (' . $$ . ')'; |
79 | |
80 | my $p = $d->pidfile; |
81 | isa_ok($p, 'MooseX::Daemonize::Pid::File'); |
82 | #diag $p->dump; |
83 | |
84 | sleep(2); |
85 | |
86 | ok($p->does_file_exist, '... the PID file exists'); |
87 | ok($p->is_running, '... the daemon process is running (' . $p->pid . ')'); |
88 | |
89 | my $pid = $p->pid; |
d02fc704 |
90 | if (DEBUG) { |
91 | diag `ps $pid`; |
92 | diag "-------"; |
93 | diag `ps -x | grep test-app`; |
94 | diag "-------"; |
95 | diag "killing $pid"; |
96 | } |
8ac4733f |
97 | kill INT => $p->pid; |
d02fc704 |
98 | diag "killed $pid" if DEBUG; |
8ac4733f |
99 | sleep(2); |
d02fc704 |
100 | if (DEBUG) { |
101 | diag `ps $pid`; |
102 | diag "-------"; |
103 | diag `ps -x | grep test-app`; |
104 | } |
8ac4733f |
105 | |
106 | ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')'); |
107 | ok(!(-e $PIDFILE), '... the PID file has been removed'); |
108 | |