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