Commit | Line | Data |
5b9ebe08 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use Cwd; |
6 | use File::Spec::Functions; |
7 | |
8 | use Test::More tests => 9; |
3543c999 |
9 | use MooseX::Daemonize; |
a4952679 |
10 | |
5b9ebe08 |
11 | use constant DEBUG => 0; |
12 | |
13 | my $CWD = Cwd::cwd; |
14 | my $FILENAME = "$CWD/im_alive"; |
15 | $ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt'); |
16 | $ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt'); |
a4952679 |
17 | |
18 | { |
19 | |
20 | package FileMaker; |
21 | use Moose; |
22 | with qw(MooseX::Daemonize); |
23 | |
7d7115e5 |
24 | has filename => ( isa => 'Str', is => 'ro' ); |
2eced271 |
25 | |
43d819aa |
26 | after start => sub { |
27 | my $self = shift; |
5b9ebe08 |
28 | if ($self->is_daemon) { |
29 | $self->create_file( $self->filename ); |
30 | } |
43d819aa |
31 | }; |
7d7115e5 |
32 | |
a4952679 |
33 | sub create_file { |
34 | my ( $self, $file ) = @_; |
3543c999 |
35 | open( my $FILE, ">$file" ) || die $!; |
36 | close($FILE); |
a4952679 |
37 | } |
a4952679 |
38 | } |
39 | |
3543c999 |
40 | my $app = FileMaker->new( |
5b9ebe08 |
41 | pidbase => $CWD, |
42 | filename => $FILENAME, |
3543c999 |
43 | ); |
5b9ebe08 |
44 | |
45 | ok(!$app->status, '... the daemon is running'); |
46 | |
47 | diag $$ if DEBUG; |
48 | |
49 | ok($app->start, '... daemon started'); |
50 | sleep(1); # give it a second ... |
51 | |
52 | ok($app->status, '... the daemon is running'); |
53 | |
54 | my $pid = $app->pidfile->pid; |
55 | isnt($pid, $$, '... the pid in our pidfile is correct (and not us)'); |
56 | |
57 | if (DEBUG) { |
58 | diag `ps $pid`; |
59 | diag "Status is: " . $app->status_message; |
60 | } |
61 | |
b916501e |
62 | ok( -e $app->filename, "file exists" ); |
5b9ebe08 |
63 | ok($app->status, '... the daemon is still running'); |
64 | |
65 | if (DEBUG) { |
66 | diag `ps $pid`; |
67 | diag "Status is: " . $app->status_message; |
68 | } |
69 | |
70 | ok( $app->stop, 'app stopped' ); |
71 | ok(!$app->status, '... the daemon is no longer running'); |
72 | |
73 | if (DEBUG) { |
74 | diag `ps $pid`; |
75 | diag "Status is: " . $app->status_message; |
76 | } |
77 | |
78 | ok( not(-e $app->pidfile->file) , 'pidfile gone' ); |
79 | |
80 | unlink $FILENAME; |
81 | unlink $ENV{MX_DAEMON_STDOUT}; |
82 | unlink $ENV{MX_DAEMON_STDERR}; |