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