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