Commit | Line | Data |
5b9ebe08 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5b9ebe08 |
5 | use File::Spec::Functions; |
6 | |
92cf56b7 |
7 | use Test::More tests => 29; |
8 | use Test::Moose; |
9 | |
23b6ee85 |
10 | use File::Temp qw(tempdir); |
11 | |
12 | my $dir = tempdir( CLEANUP => 1 ); |
13 | |
92cf56b7 |
14 | BEGIN { |
15 | use_ok('MooseX::Daemonize'); |
16 | } |
a4952679 |
17 | |
5b9ebe08 |
18 | use constant DEBUG => 0; |
19 | |
23b6ee85 |
20 | my $FILENAME = catfile($dir, "im_alive"); |
21 | $ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt'); |
22 | $ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt'); |
a4952679 |
23 | |
24 | { |
25 | |
26 | package FileMaker; |
27 | use Moose; |
28 | with qw(MooseX::Daemonize); |
29 | |
7d7115e5 |
30 | has filename => ( isa => 'Str', is => 'ro' ); |
2eced271 |
31 | |
43d819aa |
32 | after start => sub { |
33 | my $self = shift; |
5b9ebe08 |
34 | if ($self->is_daemon) { |
35 | $self->create_file( $self->filename ); |
36 | } |
43d819aa |
37 | }; |
7d7115e5 |
38 | |
a4952679 |
39 | sub create_file { |
40 | my ( $self, $file ) = @_; |
3543c999 |
41 | open( my $FILE, ">$file" ) || die $!; |
42 | close($FILE); |
a4952679 |
43 | } |
a4952679 |
44 | } |
45 | |
3543c999 |
46 | my $app = FileMaker->new( |
23b6ee85 |
47 | pidbase => $dir, |
5b9ebe08 |
48 | filename => $FILENAME, |
3543c999 |
49 | ); |
92cf56b7 |
50 | isa_ok($app, 'FileMaker'); |
51 | does_ok($app, 'MooseX::Daemonize'); |
52 | does_ok($app, 'MooseX::Daemonize::WithPidFile'); |
53 | does_ok($app, 'MooseX::Daemonize::Core'); |
54 | |
55 | isa_ok($app->pidfile, 'MooseX::Daemonize::Pid::File'); |
56 | |
23b6ee85 |
57 | is($app->pidfile->file, catfile($dir, "filemaker.pid"), '... got the right PID file path'); |
92cf56b7 |
58 | ok(not(-e $app->pidfile->file), '... our pidfile does not exist'); |
5b9ebe08 |
59 | |
60 | ok(!$app->status, '... the daemon is running'); |
92cf56b7 |
61 | is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code'); |
62 | |
63 | ok($app->stop, '... the app will stop cause its not running'); |
64 | is($app->status_message, "Not running", '... got the correct status message'); |
65 | is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); |
5b9ebe08 |
66 | |
67 | diag $$ if DEBUG; |
68 | |
69 | ok($app->start, '... daemon started'); |
92cf56b7 |
70 | is($app->status_message, "Start succeeded", '... got the correct status message'); |
71 | is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); |
72 | |
5b9ebe08 |
73 | sleep(1); # give it a second ... |
74 | |
92cf56b7 |
75 | ok(-e $app->pidfile->file, '... our pidfile exists' ); |
5b9ebe08 |
76 | |
77 | my $pid = $app->pidfile->pid; |
78 | isnt($pid, $$, '... the pid in our pidfile is correct (and not us)'); |
79 | |
92cf56b7 |
80 | ok($app->status, '... the daemon is running'); |
81 | is($app->status_message, "Daemon is running with pid ($pid)", '... got the correct status message'); |
82 | is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); |
83 | |
5b9ebe08 |
84 | if (DEBUG) { |
85 | diag `ps $pid`; |
86 | diag "Status is: " . $app->status_message; |
87 | } |
88 | |
b916501e |
89 | ok( -e $app->filename, "file exists" ); |
5b9ebe08 |
90 | |
91 | if (DEBUG) { |
92 | diag `ps $pid`; |
93 | diag "Status is: " . $app->status_message; |
94 | } |
95 | |
92cf56b7 |
96 | ok( $app->stop, '... app stopped' ); |
97 | is($app->status_message, "Stop succeeded", '... got the correct status message'); |
98 | is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); |
99 | |
5b9ebe08 |
100 | ok(!$app->status, '... the daemon is no longer running'); |
92cf56b7 |
101 | is($app->status_message, "Daemon is not running with pid ($pid)", '... got the correct status message'); |
102 | is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code'); |
5b9ebe08 |
103 | |
104 | if (DEBUG) { |
105 | diag `ps $pid`; |
106 | diag "Status is: " . $app->status_message; |
107 | } |
108 | |
92cf56b7 |
109 | ok( not(-e $app->pidfile->file) , '... pidfile gone' ); |
5b9ebe08 |
110 | |
111 | unlink $FILENAME; |
112 | unlink $ENV{MX_DAEMON_STDOUT}; |
113 | unlink $ENV{MX_DAEMON_STDERR}; |