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